description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, array: List[int], hour: int) -> int: if len(array) == hour: return max(array) low = 1 high = max(array) ans = high while low <= high: mid = (low + high) // 2 h = hour for i in range(len(array)): if array[i] <= mid: h -= 1 elif array[i] % mid == 0: h -= array[i] // mid else: h -= array[i] // mid + 1 if h < 0: break if h >= 0: ans = mid high = mid - 1 else: low = mid + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def condition(k) -> bool: count = 0 for v in piles: count += int(math.ceil(v / k)) if count > H: return False return True left, right = 1, max(piles) while left < right: mid = left + (right - left) // 2 if condition(mid): right = mid else: left = mid + 1 return left
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: l = 1 r = max(piles) def findTime(count): hr = 0 for p in piles: hr += (p - 1) // count + 1 if hr > H: return False return True while l < r: mid = l + (r - l) // 2 if findTime(mid): r = mid else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: lo = sum(piles) // H hi = max(piles) if len(piles) == 1: return ceil(piles[0] / H) while lo <= hi: mid = (lo + hi) // 2 count = sum(map(lambda x: ceil(x / mid), piles)) if count > H: lo = mid + 1 else: hi = mid - 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def sop(A, H, K): if K == 0: return False count = 0 for i in A: if i % K == 0: count += i // K else: count += i // K + 1 if count <= H: return True return False l = sum(piles) // H r = max(piles) print((l, r)) print(sop(piles, H, l)) if len(piles) == 1: return l + 1 m = (l + r) // 2 while l <= r: if sop(piles, H, l) == False and sop(piles, H, l + 1) == True: return l + 1 if sop(piles, H, r - 1) == False and sop(piles, H, r) == True: return r if sop(piles, H, m) == False: l = m m = (l + r) // 2 else: r = m m = (l + r) // 2
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: maxPile = 0 for i in piles: if i > maxPile: maxPile = i low = 1 high = maxPile minVal = maxPile while low <= high: middle = (low + high) // 2 numHours = 0 for j in piles: numHours += j // middle if j % middle > 0: numHours += 1 if numHours <= H: minVal = middle high = middle - 1 else: low = middle + 1 return minVal
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: l = 1 r = max(piles) while l < r: m = l + (r - l) // 2 h = 0 for i in piles: h += math.ceil(i / m) if h > H: l = m + 1 else: r = m return l
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def count(k): hours = 0 for i in range(len(piles)): hours += (piles[i] + k - 1) // k return hours l = 1 r = 1000000000 while l < r: m = l + (r - l) // 2 if count(m) > H: l = m + 1 else: r = m return l
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: lo = 1 hi = max(piles) ans = 0 while hi >= lo: mid = (lo + hi) // 2 if self.check(mid, piles, H): ans = mid hi = mid - 1 else: lo = mid + 1 return ans def check(self, mid, piles, H): i = 0 count = 0 new = piles[:] while i < len(new): if count > H: return False if mid >= new[i]: count += 1 i += 1 else: count += new[i] // mid + 1 i += 1 if count > H: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: left = 1 right = max(piles) def can(k): return sum((p - 1) // k + 1 for p in piles) <= H while left < right: mid = (left + right) // 2 if can(mid): right = mid else: left = mid + 1 return right
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: mini = 1 maxi = max(piles) ans = 0 while mini <= maxi: mid = mini + (maxi - mini) // 2 value = sum((p - 1) // mid + 1 for p in piles) if value <= H: ans = mid maxi = mid - 1 else: mini = mid + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: N = len(piles) start = 1 end = max(piles) while start + 1 < end: mid = start + (end - start) // 2 if self._can_complete(piles, mid, H): end = mid else: start = mid if self._can_complete(piles, start, H): return start else: return end def _can_complete(self, piles, K, H): i = 0 N = len(piles) c = 0 for d in piles: c += math.ceil(d / K) return c <= H
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def cnt(k, piles): ans = 0 for p in piles: ans += math.ceil(p / float(k)) return int(ans) piles.sort() l = 1 h = piles[-1] mid = (l + h) // 2 while l <= h: s = cnt(mid, piles) if s <= H: h = mid - 1 else: l = mid + 1 mid = (l + h) // 2 return l
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: l, h = 1, max(piles) def canFinish(k): hoursTaken = sum([ceil(p / k) for p in piles]) return hoursTaken <= H while l < h: mid = l + (h - l) // 2 if canFinish(mid): h = mid else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def can(K): cur = H for a in piles: cur -= a // K cur -= a % K != 0 return cur >= 0 l, r = 1, sum([(a + 1) for a in piles]) while l < r: mid = (l + r) // 2 if can(mid): r = mid else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: l, r = 1, max(piles) while l < r: m = (l + r) // 2 if sum((p + m - 1) // m for p in piles) > H: l = m + 1 else: r = m return l
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: if not piles: return 0 l, r = 1, max(piles) def canDo(pace): ans = 0 for v in piles: ans -= -v // pace return ans <= H while l < r: m = (l + r) // 2 if canDo(m): r = m else: l = m + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def canEat(self, piles, bananas, H): cur_H = 0 for pile in piles: if pile % bananas == 0: cur_H += pile // bananas else: cur_H += pile // bananas + 1 if cur_H > H: return False return True def minEatingSpeed(self, piles: List[int], H: int) -> int: min_speed = 1 max_speed = max(piles) while min_speed < max_speed: mid = (min_speed + max_speed) // 2 if self.canEat(piles, mid, H): max_speed = mid else: min_speed = mid + 1 return min_speed
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def possible(K): count = 0 for banana in piles: if banana % K == 0: count += banana / K else: count += banana // K + 1 if count <= H: return True else: return False left, right = 1, max(piles) while left <= right: mid = (left + right) // 2 if possible(mid): right = mid - 1 else: left = mid + 1 return left
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def time_needed(K): return sum(p // K + (1 if p % K else 0) for p in piles) lower = max(1, sum(piles) // H) upper = max(piles) while lower < upper: if time_needed(lower) <= H: return lower lower += 1 mid = (lower + upper) // 2 if time_needed(mid) <= H: upper = mid else: lower = mid + 1 return lower
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def possible(k): return sum((num - 1) // k + 1 for num in piles) <= H left, right = 1, max(piles) while left < right: mid = (left + right) // 2 if not possible(mid): left = mid + 1 else: right = mid return left
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: low, high = 1, max(piles) while low < high: K = low + (high - low) // 2 h = sum([((pile - 1) // K + 1) for pile in piles]) if h <= H: high = K else: low = K + 1 return low
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: if H == len(piles): return max(piles) l = 1 r = max(piles) + 1 while l < r: m = l + (r - l) // 2 if self.minHours(piles, m) <= H: r = m elif self.minHours(piles, m) > H: l = m + 1 return l def minHours(self, piles, k): h = 0 for p in piles: if p % k == 0: h += p // k else: h = h + p // k + 1 return h
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: l = max(1, sum(piles) // H) r = max(piles) // (H // len(piles)) + 1 def hours(rate): count = 0 for x in piles: count += (x + rate - 1) // rate return count while l <= r: mid = (l + r) // 2 h = hours(mid) if h > H: l = mid + 1 else: ans = mid r = mid - 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: if H == len(piles): return max(piles) elif sum(piles) <= H: return 1 cntr = collections.Counter(piles) lst = set() for key in cntr.keys(): lst.add((key, cntr[key])) s = 2 e = max(piles) - 1 while s <= e: K = int((s + e) / 2) total = 0 for pile, n in lst: q, r = divmod(pile, K) if r: total += (q + 1) * n else: total += q * n if total > H: break if total <= H: e = K - 1 else: s = K + 1 return s
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def eatingPossible(cap, piles, H): h = 0 for pile in piles: h += pile // cap if pile % cap != 0: h += 1 if h > H: return False return True piles.sort() left = 1 right = max(piles) while left < right: mid = left + (right - left) // 2 if eatingPossible(mid, piles, H): right = mid else: left = mid + 1 return left
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def useDays(pile, speed): days = pile // speed return days if pile % speed == 0 else days + 1 def CanDone(piles, speed, H): numDays = 0 for num in piles: numDays += useDays(num, speed) return numDays <= H maxEat = max(piles) left, right = 1, maxEat + 1 while left < right: mid = left + (right - left >> 1) if CanDone(piles, mid, H): right = mid else: left = mid + 1 return left
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: left = 1 right = max(piles) while left < right: mid = (left + right) // 2 if self.get_total_days(piles, mid) > H: left = mid + 1 else: right = mid return left def get_total_days(self, piles: List[int], num_eat: int) -> int: if num_eat == 0: return 0 total_days = 0 for pile in piles: total_days += math.ceil(float(pile) / num_eat) return total_days
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def hours(self, piles, k): cnt = 0 for p in piles: if p <= k: cnt = cnt + 1 else: cnt = cnt + p // k + 1 return cnt def minEatingSpeed(self, piles: List[int], H: int) -> int: if len(piles) == 1: return 1 + piles[0] // H l = 0 r = max(piles) while l <= r: mid = (l + r) // 2 hrs = self.hours(piles, mid) if hrs <= H: ans = mid r = mid - 1 else: l = mid + 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def time(val): t = len(piles) for pile in piles: t += (pile - 1) // val return t s = sum(piles) l, r = math.ceil(s / H), math.ceil(s / (H - len(piles) + 1)) while l <= r: mid = l + (r - l) // 2 t = time(mid) if t <= H: r = mid - 1 else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: s, e = 1, max(piles) minK = float("inf") while s <= e: m = (s + e) // 2 if self.eat(piles, m) > H: s = m + 1 else: minK = min(minK, m) e = m - 1 return minK def eat(self, piles, K): ret = 0 for p in piles: ret += math.ceil(p / K) return ret
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def possible(K): return sum(ceil(pile / K) for pile in piles) <= H total = sum(piles) lo, hi = ceil(total / H), ceil(total / (H - len(piles) + 1)) while lo < hi: mid = lo + (hi - lo) // 2 if possible(mid): hi = mid else: lo = mid + 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def ifeatK(k): hour = 0 for pile in piles: hour += math.ceil(pile / k) return hour start = 1 end = sum(piles) while start + 1 < end: mid = (start + end) // 2 if ifeatK(mid) < H: end = mid elif ifeatK(mid) > H: start = mid else: end = mid if ifeatK(start) > H: return end if ifeatK(start) == H: return start if ifeatK(end) == H: return end return start
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: k = sum(piles) // H if k == 0: return 1 while True: h = 0 prev = k for p in piles: print(p, k) if p > 1: h += (p - 1) // k + 1 else: h += 1 if h > H: break if h <= H: return k k += 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR RETURN VAR VAR NUMBER VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def is_valid(speed): curr = 0 for n in piles: q, r = divmod(n, speed) if r: q += 1 curr += q if curr > H: return False return True l, r = 1, max(piles) res = -1 while l <= r: m = l + (r - l) // 2 if is_valid(m): res = m r = m - 1 else: l = m + 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles, H): start = sum(piles) // H + 1 while start <= max(piles): suplus = sum(map(lambda x: (start - x % start) % start, piles)) if suplus + sum(piles) <= start * H: return start else: start += 1 return max(piles)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: if H == len(piles): return max(piles) else: if sum(piles) % H == 0: tempMin = sum(piles) // H else: tempMin = sum(piles) // H + 1 tempMax = max(piles) while tempMin != tempMax: tempMid = (tempMax - tempMin) // 2 + tempMin if self.ifOk(piles, H, tempMid): tempMax = tempMid else: tempMin = tempMid + 1 return tempMin def ifOk(self, piles, H, K): hourNeed = 0 for pile in piles: if pile % K == 0: hourNeed = hourNeed + pile // K else: hourNeed = hourNeed + pile // K + 1 if hourNeed > H: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: if len(piles) == 1: return piles[0] // H if piles[0] % H == 0 else piles[0] // H + 1 l = sum(piles) // H h = max(piles) * len(piles) // H + 1 def feasible(k): return sum((p - 1) // k + 1 for p in piles) <= H while l < h: mid = (l + h) // 2 if feasible(mid): h = mid else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: n = len(piles) if not n: return -1 def binary_search(l, r): nonlocal piles, H while l < r: mid = (l + r) // 2 if num_hours(mid) <= H: r = mid else: l = mid + 1 return l def num_hours(bananas_per_hour): nonlocal piles res = 0 for i in piles: if bananas_per_hour >= i: res += 1 continue res += i // bananas_per_hour res += 1 if i % bananas_per_hour else 0 return res return binary_search(1, max(piles)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def time_taken(self, piles, n): return sum(v // n + (1 if v % n else 0) for v in piles) def minEatingSpeed(self, piles: List[int], H: int) -> int: lo = max(1, sum(piles) // H) hi = max(piles) t_hi = self.time_taken(piles, hi) while lo < hi: mid = (lo + hi) // 2 t_lo = self.time_taken(piles, lo) t_mid = self.time_taken(piles, mid) if t_lo <= H: return lo elif t_mid <= H: hi, t_hi = mid, t_mid lo += 1 else: lo = mid + 1 return lo
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: lo, hi = 1, max(piles) ans = hi while lo <= hi: k = (lo + hi) // 2 time = 0 for i in piles: t = int(ceil(i / k)) if not t: t += 1 time += t if time <= H: hi = k - 1 ans = k else: lo = k + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: if len(piles) == 1: div, rem = divmod(piles[0], H) if rem == 0: return div else: return div + 1 left = 0 right = sum(piles) while left < right: mid = (left + right) // 2 if self.CalcTime(piles, mid, H): right = mid else: left = mid + 1 return left def CalcTime(self, piles, K, H): h = 0 for p in piles: div, rem = divmod(p, K) h += div if rem > 0: h += 1 if h > H: return False return h <= H
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def get_hours(self, piles, k): ans = 0 for x in piles: if x % k == 0: ans += x // k else: ans += int(x // k) + 1 return ans def minEatingSpeed(self, piles: List[int], H: int) -> int: n, max_val = len(piles), max(piles) if H < n: return 1 << 31 start, end = 1, max_val while start <= end: mid = (end - start) // 2 + start h = self.get_hours(piles, mid) if h <= H: end = mid - 1 else: start = mid + 1 return start if start <= max_val else start - 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR BIN_OP VAR NUMBER VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: lenth = len(piles) s = (min(piles) - 1) * lenth // H + 1 m = (max(piles) - 1) * lenth // H + 1 if int(m) < m: m = int(m) + 1 if s == m: return s while s < m: mid = (s + m) // 2 count = sum([((i - 1) // mid + 1) for i in piles]) if count > H: s = mid + 1 else: m = mid return s
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def can_finish(k): h = 0 for p in piles: d, r = divmod(p, k) if r: d += 1 h += d return h <= H l = 1 r = max(piles) while l < r: m = (l + r) // 2 if can_finish(m): r = m else: l = m + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: max_k = 0 for value in piles: max_k = max(max_k, value) def possible(k): hours = 0 for item in piles: hours += math.ceil(item / k) if hours > H: return False if hours <= H: return True return False def binary_search(low, high): mid = low + (high - low) // 2 if possible(low): return low elif possible(mid): return binary_search(low, mid) else: return binary_search(mid + 1, high) def binary_search_iter(low, high): pass return binary_search(1, max_k)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: lo, hi = 1, 1000000001 ans = -1 N = len(piles) def poss(val): cnt = 0 for n in range(N): cnt += math.ceil(piles[n] / val) if cnt <= H: return True return False while lo <= hi: mid = lo + (hi - lo) // 2 if poss(mid): ans = mid hi = mid - 1 else: lo = mid + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: ceiling = lambda x, y: (x + y - 1) // y def timeTaken(v): totalTime = 0 for p in piles: totalTime += ceiling(p, v) return totalTime def binarySearch(s, e, up_bound): if s == e: return s m = (s + e) // 2 if timeTaken(m) <= up_bound: return binarySearch(s, m, up_bound) else: return binarySearch(m + 1, e, up_bound) pmax = 1 for p in piles: pmax = max(pmax, p) return binarySearch(1, pmax, H)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def possible(K): return sum(math.ceil(p / K) for p in piles) <= H N = len(piles) if H == N: return max(piles) sm = sum(piles) lo, hi = int(sm / H) + 1, math.ceil(sm / (H - N)) while lo < hi: mi = (lo + hi) // 2 if not possible(mi): lo = mi + 1 else: hi = mi return hi
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def is_ok(piles, H, mid): sum = 0 for i in range(len(piles)): if piles[i] <= mid: sum += 1 else: sum += math.ceil(piles[i] / mid) return sum <= H ok = sum(piles) ng = 0 while abs(ok - ng) > 1: mid = (ok + ng) // 2 if is_ok(piles, H, mid): ok = mid else: ng = mid return ok
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: right = 10**9 left = 1 piles.sort() while left + 1 < right: mid = (left + right) // 2 if self.canEatAll(piles, mid, H): right = mid else: left = mid return left if self.canEatAll(piles, left, H) else right def canEatAll(self, piles, speed, H): res = 0 for pile in piles: res += (pile - 1) // speed + 1 return res <= H
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: n = len(piles) piles.sort() def finish(k): return sum((p - 1) // k + 1 for p in piles) <= H r = 0 while r < n: if finish(piles[r]): break else: r += 1 start = 1 if r == 0 else piles[0] end = piles[r] while start < end: mid = (start + end) // 2 if finish(mid): end = mid else: start = mid + 1 return start
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def simulate(k): hours = 0 for pile in piles: hours += ceil(pile / k) return hours lower = 1 upper = 1 for pile in piles: upper = max(upper, pile) while lower < upper: midpoint = (upper + lower) // 2 if simulate(midpoint) > H: lower = midpoint + 1 else: upper = midpoint return upper
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: bananas = sum(piles) K = bananas // H + (bananas % H != 0) while True: hours_needed = 0 for pile in piles: hours_needed += pile // K if pile % K != 0: hours_needed += 1 if hours_needed <= H: return K K += 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR VAR NUMBER VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def is_feasible(rate, H, piles): count = 0 for i in range(len(piles)): count += piles[i] // rate if piles[i] % rate != 0: count += 1 if count > H: return False return True l = 1 h = sum(piles) ans = float("inf") while l <= h: mid = (l + h) // 2 if is_feasible(mid, H, piles): ans = min(ans, mid) h = mid - 1 else: l = mid + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: piles.sort() l, r = 1, max(piles) while l <= r: m = (l + r) // 2 if sum([((x - 1) // m + 1) for x in piles]) > H: l = m + 1 elif sum([math.ceil(x / m) for x in piles]) < H: r = m - 1 else: return m return l
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: low = 1 high = max(piles) def possible(k): time = 0 for pile in piles: time += (pile - 1) // k + 1 return time <= H while low <= high: mid = low + (high - low) // 2 outcome = possible(mid) if possible(mid): high = mid - 1 else: low = mid + 1 return low
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def eats(piles, h, k): hours = 0 for pile in piles: hours += (pile - 1) // k + 1 return hours <= h lo = 1 hi = 10**9 while lo < hi: mid = lo + (hi - lo) // 2 if eats(piles, H, mid): hi = mid else: lo = mid + 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: n = len(piles) if n >= H: return max(piles) def check(m): ret = 0 for p in piles: ret += p // m + (p % m != 0) return ret upper = max(piles) lo, hi = 1, upper while lo <= hi: mid = (lo + hi) // 2 if check(mid) <= H: hi = mid - 1 else: lo = mid + 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: self.piles = piles l, r = 1, max(piles) while l <= r: K = (l + r) // 2 if self.eat_time(K) <= H: r = K - 1 else: l = K + 1 return l def eat_time(self, K): cnt = 0 for banana in self.piles: q, mod = divmod(banana, K) cnt += q if mod != 0: cnt += 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: lo, hi = 1, max(piles) while lo < hi: mid = (lo + hi) // 2 cur_cap, num_hrs = 0, 0 for p in piles: cur_cap = p if cur_cap <= mid: num_hrs += 1 else: num_hrs += cur_cap // mid + 1 if num_hrs > H: lo = mid + 1 else: hi = mid return lo
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: n = len(piles) if not n: return -1 def binary_search(l, r): nonlocal piles, H while l < r: mid = (l + r) // 2 if num_hours(mid) <= H: r = mid else: l = mid + 1 return l def num_hours(k): nonlocal n, piles res = 0 for i in range(n): num = piles[i] if k >= num: res += 1 continue res += num // k res += 1 if num % k else 0 return res return binary_search(1, max(piles))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def helper(speed): h = 0 for p in piles: h += math.ceil(p / speed) return h if not piles or len(piles) == 0: return 0 start, end = 1, max(piles) while start + 1 < end: mid = start + (end - start) // 2 h = helper(mid) if h <= H: end = mid else: start = mid return start if helper(start) <= H else end
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour. Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. Return the minimum integer K such that she can eat all the bananas within H hours.   Example 1: Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], H = 6 Output: 23   Constraints: 1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: def valid(mid): if mid == 0: return False hours = 0 for i in piles: if i < mid: hours += 1 else: hours += math.ceil(i / mid) return hours <= H if not piles: return 0 piles = sorted(piles) s, e = 0, sum(piles) ans = -1 while s <= e: mid = (s + e) // 2 if valid(mid): ans = mid e = mid - 1 else: s = mid + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array a of length n and array b of length m. He introduced a function f(j) which is defined for integers j, which satisfy 0 ≤ j ≤ m - n. Suppose, c_{i} = a_{i} - b_{i} + j. Then f(j) = |c_1 - c_2 + c_3 - c_4... c_{n}|. More formally, $f(j) =|\sum_{i = 1}^{n}(- 1)^{i - 1} *(a_{i} - b_{i + j})|$. Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid j. They found it a bit easy, so Dr. Evil made their task harder. He will give them q update queries. During each update they should add an integer x_{i} to all elements in a in range [l_{i};r_{i}] i.e. they should add x_{i} to a_{l}_{i}, a_{l}_{i} + 1, ... , a_{r}_{i} and then they should calculate the minimum value of f(j) for all valid j. Please help Mahmoud and Ehab. -----Input----- The first line contains three integers n, m and q (1 ≤ n ≤ m ≤ 10^5, 1 ≤ q ≤ 10^5) — number of elements in a, number of elements in b and number of queries, respectively. The second line contains n integers a_1, a_2, ..., a_{n}. ( - 10^9 ≤ a_{i} ≤ 10^9) — elements of a. The third line contains m integers b_1, b_2, ..., b_{m}. ( - 10^9 ≤ b_{i} ≤ 10^9) — elements of b. Then q lines follow describing the queries. Each of them contains three integers l_{i} r_{i} x_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n, - 10^9 ≤ x ≤ 10^9) — range to be updated and added value. -----Output----- The first line should contain the minimum value of the function f before any update. Then output q lines, the i-th of them should contain the minimum value of the function f after performing the i-th update . -----Example----- Input 5 6 3 1 2 3 4 5 1 2 3 4 5 6 1 1 10 1 1 -9 1 5 -1 Output 0 9 0 0 -----Note----- For the first example before any updates it's optimal to choose j = 0, f(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0. After the first update a becomes {11, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9. After the second update a becomes {2, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(2 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6)| = |0| = 0. After the third update a becomes {1, 1, 2, 3, 4} and it's optimal to choose j = 0, f(0) = |(1 - 1) - (1 - 2) + (2 - 3) - (3 - 4) + (4 - 5)| = |0| = 0.
def read(): return list(map(int, input().split(" "))) n, m, q = read() aa = read() bb = read() reqs = [read() for _ in range(q)] asum = 0 bsum = 0 for i, (a, b) in enumerate(zip(aa, bb)): asum += a if i % 2 == 0 else -a bsum += b if i % 2 == 0 else -b bpos = [bsum] for i in range(len(aa), len(bb)): b = bb[i] rempos = i - len(aa) bsum += b if i % 2 == 0 else -b bsum -= bb[rempos] if rempos % 2 == 0 else -bb[rempos] bpos += [bsum if rempos % 2 == 1 else -bsum] bpos = sorted(set(bpos)) def closest(arr, value): l = 0 r = len(arr) while l + 1 < r: m = (l + r) // 2 if arr[m] <= value: l = m else: r = m res = arr[l] if l + 1 < len(arr) and abs(arr[l + 1] - value) < abs(arr[l] - value): res = arr[l + 1] return res print(abs(asum - closest(bpos, asum))) for req in reqs: l, r, x = req l -= 1 if (r - l) % 2 != 0: asum += x if l % 2 == 0 else -x print(abs(asum - closest(bpos, asum)))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,4,5,1,2] Output: 1 Example 2: Input: [4,5,6,7,0,1,2] Output: 0
class Solution: def findMin(self, nums): if len(nums) == 0: return -1 if len(nums) == 2: return min(nums) low = 0 high = len(nums) - 1 mid = (low + high) // 2 while low < high: if nums[mid] < nums[high]: high = mid elif nums[mid] > nums[high]: low = mid + 1 mid = (low + high) // 2 return nums[low]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,4,5,1,2] Output: 1 Example 2: Input: [4,5,6,7,0,1,2] Output: 0
class Solution: def findMin(self, nums): if not nums or len(nums) == 0: return 0 start, end = 0, len(nums) - 1 target = nums[-1] while start + 1 < end: mid = (start + end) // 2 if nums[mid] < target: end = mid else: start = mid if nums[start] < target: return nums[start] return nums[end]
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR VAR RETURN VAR VAR
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,4,5,1,2] Output: 1 Example 2: Input: [4,5,6,7,0,1,2] Output: 0
class Solution: def findMin(self, nums): if len(nums) == 0: return None if nums[-1] > nums[0]: return nums[0] boundary = nums[0] return self.binary_search_min(nums, boundary) def binary_search_min(self, nums, maximum): left_boundary = 0 right_boundary = len(nums) - 1 while True: current_index = left_boundary + (right_boundary - left_boundary) // 2 value = nums[current_index] if left_boundary == right_boundary: return value if value >= maximum: left_boundary = current_index + 1 else: right_boundary = current_index
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,4,5,1,2] Output: 1 Example 2: Input: [4,5,6,7,0,1,2] Output: 0
class Solution: def findMin(self, nums): if len(nums) == 1: return nums[0] for i in range(1, len(nums)): if nums[i - 1] > nums[i]: return nums[i] return nums[0]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR RETURN VAR NUMBER
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,4,5,1,2] Output: 1 Example 2: Input: [4,5,6,7,0,1,2] Output: 0
class Solution: def findMin(self, nums): low, high = 0, len(nums) - 1 while low < high: mid = (low + high) // 2 if nums[mid] <= nums[high]: high = mid else: low = mid + 1 return nums[low]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,4,5,1,2] Output: 1 Example 2: Input: [4,5,6,7,0,1,2] Output: 0
class Solution: def findMin(self, nums): if len(nums) == 1: return nums[0] l = 0 r = len(nums) - 1 while l < r: m = (l + r) // 2 v = nums[m] if nums[m - 1] < v: if nums[r] > v: r = m - 1 else: l = m + 1 else: l = m + 1 if l == r: if m < l: if nums[m] < nums[l]: return nums[m] else: return nums[l] if l < m: if nums[l - 1] < nums[m]: return nums[l - 1] return nums[l] elif r < l: return nums[r] else: return nums[m] def findMin_PASSED(self, nums): if len(nums) == 1: return nums[0] retval = float("inf") l, r = 0, len(nums) - 1 while l < r: m = (l + r) // 2 v = nums[m] if v < nums[m + 1]: if v < nums[r]: r = m elif v > nums[r]: l = m + 1 elif v > nums[m + 1]: l = m + 1 if nums[m] < nums[m + 1]: return nums[m] else: return nums[m + 1]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR RETURN VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR VAR RETURN VAR VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN VAR BIN_OP VAR NUMBER
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,4,5,1,2] Output: 1 Example 2: Input: [4,5,6,7,0,1,2] Output: 0
class Solution: def findMin(self, nums): left, right = 0, len(nums) - 1 mid = 0 while left < right: mid = left + (right - left) // 2 if nums[right] < nums[mid]: left = mid + 1 elif nums[right] > nums[mid]: if nums[mid - 1] > nums[mid]: return nums[mid] else: right = mid - 1 return nums[left]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,4,5,1,2] Output: 1 Example 2: Input: [4,5,6,7,0,1,2] Output: 0
class Solution: def findMin(self, nums): if len(nums) <= 3: return min(nums) lo = 0 hi = len(nums) - 1 mid = (hi + lo) // 2 if nums[mid] < nums[mid - 1] and nums[mid] < nums[mid + 1]: return nums[mid] if nums[mid] > nums[lo] and nums[mid] > nums[hi]: return self.findMin(nums[mid:]) else: return self.findMin(nums[: mid + 1])
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): if nums == []: return [] dct = {} for el in nums: if el in dct: dct[el] += 1 else: dct[el] = 1 fin = [] for key in dct: if dct[key] > len(nums) // 3: fin.append(key) return fin
CLASS_DEF FUNC_DEF IF VAR LIST RETURN LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): length = len(nums) ans = [] for i in set(nums): if nums.count(i) > length / 3: ans.append(i) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): ans = [] if not nums: return [] a1 = a2 = None c1 = c2 = 0 for num in nums: if a1 == num: c1 += 1 elif a2 == num: c2 += 1 elif c1 == 0: a1, c1 = num, 1 elif c2 == 0: a2, c2 = num, 1 else: c1 -= 1 c2 -= 1 c1 = c2 = 0 for num in nums: if num == a1: c1 += 1 elif num == a2: c2 += 1 for a, c in ((a1, c1), (a2, c2)): if c > len(nums) // 3: ans.append(a) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR RETURN LIST ASSIGN VAR VAR NONE ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): d = dict() results = [] for num in nums: d[num] = d.get(num, 0) + 1 if d[num] > len(nums) // 3: if num not in results: results.append(num) return results
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): if len(nums) <= 2: return list(set(nums)) can, can2 = 0, 0 num, num2 = 0, 1 for i in range(len(nums)): if nums[i] == can: num += 1 elif nums[i] == can2: num2 += 2 elif num == 0: num = 1 can = nums[i] elif num2 == 0: num2 = 1 can2 = nums[i] else: num, num2 = num - 1, num2 - 1 res = [] if nums.count(can) > int(len(nums) / 3): res.append(can) if nums.count(can2) > int(len(nums) / 3) and can2 != can: res.append(can2) return res
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): n1 = n2 = None c1 = c2 = 0 for i in nums: if n1 == i: c1 += 1 elif n2 == i: c2 += 1 elif c1 == 0: n1, c1 = i, 1 elif c2 == 0: n2, c2 = i, 1 else: c1, c2 = c1 - 1, c2 - 1 return [n for n in [n1, n2] if n is not None and nums.count(n) > len(nums) // 3]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NONE ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR LIST VAR VAR VAR NONE FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): N = len(nums) N3 = N / 3 nums.sort() result = [] if N < 1: return nums _cnt = 0 _num = nums[0] for n in nums: if n != _num: if _cnt > N3: result.append(_num) _cnt = 1 _num = n else: _cnt += 1 else: if _cnt > N3: result.append(_num) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): ret = [] least = len(nums) / 3 h = {} for n in nums: if n in list(h.keys()): h[n] += 1 else: h[n] = 1 if h[n] > least and n not in ret: ret.append(n) return ret
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): dic = {} res = [] n = len(nums) for i in range(n): dic[nums[i]] = dic.get(nums[i], 0) + 1 print(dic) for num, cnt in dic.items(): if cnt > n / 3: res.append(num) return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): ctr = collections.Counter() for n in nums: ctr[n] += 1 if len(ctr) == 3: ctr -= collections.Counter(set(ctr)) return [n for n in ctr if nums.count(n) > len(nums) // 3]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): if not nums: return [] l = len(nums) candidate1 = candidate2 = 0 count1 = count2 = 0 for i in nums: if candidate1 == i: count1 += 1 elif candidate2 == i: count2 += 1 elif count1 == 0: candidate1 = i count1 = 1 elif count2 == 0: candidate2 = i count2 = 1 else: count1 -= 1 count2 -= 1 c1 = nums.count(candidate1) c2 = nums.count(candidate2) res = [] if c1 > l / 3: res.append(candidate1) if c2 > l / 3 and candidate1 != candidate2: res.append(candidate2) return res
CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2]
class Solution: def majorityElement(self, nums): d = {} for i in nums: if i in d: d[i] += 1 else: d[i] = 1 l = [] for i in list(d.keys()): if d[i] > len(nums) // 3: l.append(i) return l
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, n, a): l = [] for i in range(n): flag = 0 for j in range(n - 1, i, -1): if a[j] < a[i]: flag = 1 l.append(j) break if flag == 0: l.append(-1) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): l = [] x = 0 m = 0 for i in range(N): x = N - 1 m = 0 while x > i: if Arr[i] > Arr[x]: l.append(x) m = 1 break x = x - 1 if m == 0: l.append(-1) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): for i in range(len(Arr)): flag = 0 for j in reversed(range(len(Arr))): if Arr[i] > Arr[j] and i < j: Arr[i] = j flag = 1 break if flag == 0: Arr[i] = -1 return Arr
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, n, arr): arr2 = sorted(enumerate(arr), key=lambda x: [x[1], x[0]]) list1 = [0] * n list1[arr2[0][0]] = -1 index_max = arr2[0][0] for i in range(1, n): if arr2[i][0] > index_max: list1[arr2[i][0]] = -1 index_max = arr2[i][0] else: list1[arr2[i][0]] = index_max return list1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): ans = [] for i in range(0, N - 1): sm = -1 se = Arr[i] for j in range(N - 1, i, -1): if Arr[j] < se: sm = j break ans.append(sm) ans.append(-1) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, n, a): pref = [0] * n pref[n - 1] = a[n - 1] for i in range(n - 2, -1, -1): pref[i] = min(a[i], pref[i + 1]) res = [] for i in range(n): low = i + 1 high = n - 1 ans = -1 while low <= high: mid = (low + high) // 2 if a[i] <= pref[mid]: high = mid - 1 else: ans = mid low = mid + 1 res.append(ans) return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): lista = [-1] * N for i in range(N): cn = 0 j = N - 1 flag = 0 while j > i and flag != 1: if Arr[j] < Arr[i] and j >= cn: cn = j lista[i] = cn flag = 1 else: j = j - 1 return lista
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def findLower(self, searchArray, ele, N, i): n = len(searchArray) l = i + 1 r = n - 1 val = -1 while l <= r: m = (l + r) // 2 if searchArray[m] < ele: val = m l = m + 1 else: r = m - 1 return val def farNumber(self, N, arr): minArray, res = [0] * N, [-1] * N res[-1] = -1 minArray[-1], minEle = arr[-1], arr[-1] for i in range(N - 2, -1, -1): if arr[i] < minEle: minEle = arr[i] res[i] = -1 else: res[i] = self.findLower(minArray, arr[i], N, i) minArray[i] = minEle return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def SuffixArrayOfMin(self, arr): suffix_arr = [0] * len(arr) suffix_arr[-1] = arr[-1] for i in range(len(arr) - 1, 0, -1): suffix_arr[i - 1] = min(arr[i - 1], suffix_arr[i]) return suffix_arr def BinarySearchInSuffixArray(self, arr): result = [] suffix_arr = self.SuffixArrayOfMin(arr) for i in range(len(arr)): start = i + 1 end = len(arr) - 1 ans = -1 while start <= end: mid = (start + end) // 2 if suffix_arr[mid] < arr[i]: ans = mid start = mid + 1 else: end = mid - 1 result.append(ans) return result def farNumber(self, N, Arr): return self.BinarySearchInSuffixArray(Arr)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): a = [] for i in range(N): for j in range(N - 1, i, -1): if Arr[j] < Arr[i]: a.append(j) break else: a.append(-1) return a
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): new = [-1] * N suf = [float("inf")] * N suf[N - 1] = Arr[N - 1] p = N - 1 mins = float("inf") while p >= 0: mins = min(mins, Arr[p]) suf[p] = mins p -= 1 for i in range(N - 2, -1, -1): l = i + 1 r = N - 1 while l <= r: m = (l + r) // 2 if suf[m] < Arr[i]: new[i] = m l = m + 1 else: r = m - 1 return new
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): lst = [-1] * N for i in range(N): for j in range(N - 1, i, -1): if Arr[j] < Arr[i]: lst[i] = j break return lst
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, n, arr): ans = [] for i in range(n): check = -1 for j in range(n - 1, i - 1, -1): if arr[j] < arr[i]: ans.append(j) check = 1 break if check < 0: ans.append(-1) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): r = 0 l = N - 1 while r < N: if r == l: Arr[r] = -1 l = N - 1 r = r + 1 elif Arr[r] > Arr[l]: Arr[r] = l l = N - 1 r = r + 1 else: l = l - 1 return Arr
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, n, a): l = [] res = [-1] * n for i in range(n): l.append([a[i], i]) l.sort() mind = None for i in range(n): if mind == None or mind < l[i][1]: mind = l[i][1] else: res[l[i][1]] = mind return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NONE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR RETURN VAR