Algorithm(53)
-
3100. Water Bottles II
numBottles와 numExchange가 주어짐 numBottles 개수 만큼의 병을 모두 마시고 empty 개수를 마신만큼 증가 시키며 마신 것의 개수를 증가 시킬 수 있음 혹은 empty 개수가 numExchange 이상인 경우 numExchange 개수 만큼의 empty 개수로 numBottles 하나를 증가하고 numExchange도 하나 증가할 수 있음 이렇게 해서 마신 개수가 최대가 되는 값을 찾기 class Solution: def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int: def dfs(full, empty, exchange, drunk, mx): if full == 0 and empty < exchange: mx..
2024.04.05 -
2717. Semi-Ordered Permutation
func swap(val1 *int, val2 *int) { temp := *val1 *val1 = *val2 *val2 = temp } func semiOrderedPermutation(nums []int) int { i := 0 one_idx := -1 n_idx := -1 cnt := 0 for idx, val := range nums { if val == 1 { one_idx = idx } } i = one_idx for i > 0 { swap(&nums[i - 1], &nums[i]) cnt += 1 i -= 1 } for idx, val := range nums { if val == len(nums) { n_idx = idx } } i = n_idx for i < len(nums) - 1 { ..
2023.06.05 -
2541. Minimum Operations to Make Array Equal II
two arrays and integer value k are provided. the thing you need to do is to figure out the minumum number of changes by applying operation. the operation is as follows. in the first array, nums1 pick two indices i, j apply nums1[i] + k, nums1[j] - k and check the nums1 and the nums2 are equal, if so, then return the number of operations until then. Python class Solution: def minOperations(self, ..
2023.01.29 -
2545. Sort the Students by Their Kth Score
Python class Solution: def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]: return sorted(score, key=lambda p: p[k], reverse=True) Kotlin class Solution { fun sortTheStudents(score: Array, k: Int): Array { val res = score.sortedByDescending { it[k] } return res.toTypedArray() } }
2023.01.29 -
2544. Alternating Digit Sum
Kotlin class Solution { fun alternateDigitSum(n: Int): Int { val s = n.toString() var sum: Int = 0 var toggle: Boolean = true for (i in 0 until s.length) { val digit = Character.getNumericValue(s[i]) if (true == toggle) { sum += digit } else { sum += -1 * digit } toggle = !toggle } return sum } }
2023.01.28 -
2255. Count Prefixes of a Given String
Kotlin class Solution { fun countPrefixes(words: Array, s: String): Int { var cnt = 0 for (word in words) { if (word.length > s.length) { continue } var is_same = true for (i in 0 .. word.length - 1) { if (word[i] != s[i]) { is_same = false break } } if (true == is_same) { cnt += 1 } } return cnt } } Python class Solution: def countPrefixes(self, words: List[str], s: str) -> int: cnt = 0 for wor..
2023.01.26