description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: lo, hi = 1, max(nums) while lo <= hi: mid = (lo + hi) // 2 divisor_sum = sum([math.ceil(x / mid) for x in nums]) next_sum = sum([math.ceil(x / (mid + 1)) for x in nums]) if next_sum <= threshold < divisor_sum: return mid + 1 elif next_sum > threshold: lo = mid + 1 elif divisor_sum <= threshold: hi = mid - 1 return 1
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 FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def is_valid(select_num: int): res = sum([math.ceil(num / select_num) for num in nums]) return None if res > threshold else res if not nums: return 0 left, right = 1, 1000000000 while left < right: middle = (left + right) // 2 if is_valid(middle): right = middle else: left = middle + 1 return left if is_valid(left) else right
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR NONE VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER 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 RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: a = 1 b = max(nums) i = (a + b) // 2 ans = 10000000000000 while b - a > 1: sum = 0 for j in nums: if j % i == 0: sum += j // i else: sum += j // i + 1 if sum <= threshold: ans = min(i, ans) b = i else: a = i i = (a + b) // 2 for i in (a, b): sum = 0 for j in nums: if j % i == 0: sum += j // i else: sum += j // i + 1 if sum <= threshold: ans = min(i, ans) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: L, R = 1, max(nums) + 1 def solve(divisor): sums = 0 for num in nums: sums += num // divisor sums += 0 if num % divisor == 0 else 1 return sums > threshold while L < R: mid = L + (R - L) // 2 if solve(mid): L = mid + 1 else: R = mid return R
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER 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 BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: left = 1 right = max(nums) while left <= right: mid = (right + left) // 2 acc = 0 for n in nums: acc += math.ceil(n / mid) if acc > threshold: left = mid + 1 elif acc <= threshold: right = mid - 1 return left
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 ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def find(self, nums, div): ans = 0 for i in nums: ans += i // div if i % div != 0: ans += 1 return ans def smallestDivisor(self, nums: List[int], threshold: int) -> int: low = 1 high = max(nums) while low < high: mid = (low + high) // 2 cur = self.find(nums, mid) if mid > 1: prev = self.find(nums, mid - 1) else: prev = 0 if mid == 1 and cur <= threshold: return 1 if cur <= threshold and prev > threshold: return mid elif cur > threshold: low = mid + 1 elif prev <= threshold: high = mid - 1 return (low + high) // 2
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def check(div): return threshold >= sum( [(elem / div if elem % div == 0 else elem // div + 1) for elem in nums] ) def bst(low, high): mid = (low + high) // 2 if low > high: return -1 elif low == high: if check(low): return low else: return -1 elif check(mid): ambition = bst(low, mid - 1) if ambition != -1: return ambition else: return mid else: return bst(mid + 1, high) return bst(1, 10**9)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR IF FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: start = 1 end = max(nums) while start <= end: mid = (start + end) // 2 result = self.satistfy(nums, mid) if result <= threshold: end = mid - 1 else: start = mid + 1 return start def satistfy(self, nums, mid): return sum(list([math.ceil(x / mid) for x in nums]))
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 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 FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: s = sum(nums) l = len(nums) L = max((s + threshold - 1) // threshold, 1) - 1 t_ = max(threshold - l, 1) U = (s + t_ - 1) // t_ while L + 1 < U: m = (L + U) // 2 if sum((x + m - 1) // m for x in nums) <= threshold: U = m else: L = m return U
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR WHILE BIN_OP VAR NUMBER 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 VAR ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: val = sum(nums) n = len(nums) if val <= threshold: return 1 p = val // threshold + int(val % threshold != 0) q = val // max(1, threshold - n - 1) q = q + 4 p = max(2, p) check = 1 while p < q: mid = (p + q) // 2 count1 = 0 count2 = 0 for i in nums: count1 = count1 + (i // mid + int(i % mid != 0)) count2 = count2 + (i // (mid - 1) + int(i % (mid - 1) != 0)) if count1 <= threshold and count2 > threshold: return mid elif count1 <= threshold and count2 <= threshold: q = mid elif count1 > threshold: p = mid + 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def thres(div): return sum([ceil(val / div) for val in nums]) l, h = 1, max(nums) while l <= h: mid = l + (h - l >> 1) if thres(mid) <= threshold: h = mid - 1 else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR 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 IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: l = 1 r = max(nums) while l + 1 < r: mid = l + (r - l) // 2 if self.search(nums, mid, threshold) > 0: r = mid elif self.search(nums, mid, threshold) < 0: l = mid if self.search(nums, l, threshold) >= 0: return l else: return r def search(self, nums, i, threshold): insum = sum([math.ceil(el / i) for el in nums]) if insum <= threshold: return 1 else: return -1
CLASS_DEF FUNC_DEF VAR 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 NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: low = 1 high = 2 * max(nums) ans = float("inf") while low <= high: mid = (low + high) // 2 m = sum(math.ceil(x / mid) for x in nums) if m <= threshold: ans = min(ans, mid) high = mid - 1 else: low = mid + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: left = 1 right = max(nums) while left <= right: mid = left + (right - left) // 2 mid_left_result = self.divide_and_sum(mid - 1, nums) mid_result = self.divide_and_sum(mid, nums) if mid_left_result > threshold and mid_result <= threshold: return mid if mid_result > threshold: left = mid + 1 if mid_left_result <= threshold: right = mid - 1 def divide_and_sum(self, divisor, nums): if divisor == 0: return math.inf result = 0 for num in nums: result += ceil(num / divisor) return result
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 FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def satisfyThreshold(self, nums, threshold, divisor): return sum((n - 1) // divisor + 1 for n in nums) <= threshold def smallestDivisor(self, nums: List[int], threshold: int) -> int: left = 1 right = max(nums) while left < right: mid = left + (right - left) // 2 if self.satisfyThreshold(nums, threshold, mid): right = mid else: left = mid + 1 return left
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR 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 IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def isThres(m): tsum = 0 for n in nums: tsum += n // m if n % m > 0: tsum += 1 if tsum <= threshold: return True l, r = 1, sum(nums) while l < r: m = l + r >> 1 if isThres(m): r = m else: l = m + 1 return l
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 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 VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: l = 1 r = max(nums) def helper(arr, mid, threshold): return sum([math.ceil(num / mid) for num in arr]) <= threshold while l <= r: mid = (l + r) // 2 if helper(nums, mid, threshold): r = mid - 1 else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR 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 BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: l = 1 r = max(nums) while l < r: m = l + (r - l) // 2 s = sum([ceil(x / m) for x in nums]) if s > threshold: 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 FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def helper(nums, ans): return sum([((i + ans - 1) // ans) for i in nums]) l = 1 r = max(nums) while l < r: mid = l + (r - l) // 2 sumv = helper(nums, mid) if sumv <= threshold: r = mid else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER 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 FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: l = 0 r = nums[-1] res = [] while r - l > 1: c = (l + r) // 2 tmp_sum = [math.ceil(i / c) for i in nums] tmp_sum = sum(tmp_sum) if tmp_sum > threshold: l = c elif tmp_sum <= threshold: r = c res.append(c) return min(res)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: high = sum(nums) if high <= threshold: return 1 def sum_divide(divisor): su = 0 for num in nums: su += math.ceil(num / divisor) if su > threshold: return False return True low = 1 while low < high: mid = low + (high - low) // 2 if sum_divide(mid): high = mid else: low = mid + 1 return low
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER 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 RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def calculate(divisor): _sum = 0 for val in nums: _sum += val // divisor + int(val % divisor != 0) return _sum start, end = 1, sum(nums) while start < end: mid = start + (end - start) // 2 _sum = calculate(mid) if _sum > threshold: start = mid + 1 else: end = mid return start
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN 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 VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: nums.sort() n = len(nums) start, end = 1, nums[-1] while start < end: mid = (start + end) // 2 j = n - 1 s = n while j >= 0 and mid < nums[j]: s -= 1 s += math.ceil(nums[j] / mid) j -= 1 if s > threshold: start = mid + 1 else: end = mid return start
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def divide(self, num, divisor): return -(-num // divisor) def getQuotient(self, nums, divisor): output = 0 for num in nums: output += ceil(num / divisor) return output def getResult(self, results, divisor, nums): return self.getQuotient(nums, divisor) def helper(self, results, nums, i, j, threshold): if i == j: return i mid = (i + j) // 2 if mid == 1: return mid elif ( self.getResult(results, mid, nums) <= threshold and self.getResult(results, mid - 1, nums) > threshold ): return mid elif self.getResult(results, mid, nums) <= threshold: return self.helper(results, nums, i, mid - 1, threshold) else: return self.helper(results, nums, mid + 1, j, threshold) def smallestDivisor(self, nums: List[int], threshold: int) -> int: maxDivisor = ceil(2 * sum(nums) / threshold) results = dict() return self.helper(results, nums, 1, maxDivisor, threshold)
CLASS_DEF FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: low, high = math.ceil(sum(nums) / threshold), math.ceil( sum(nums) / (threshold - len(nums)) ) def binary_search(low, high): if high > low: mid = (high + low) // 2 tot = 0 for num in nums: tot += math.ceil(num / mid) if tot <= threshold: return binary_search(low, mid) else: return binary_search(mid + 1, high) else: return high return binary_search(low, high)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR ASSIGN 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 RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def getSum(k): res = 0 for n in nums: if n % k == 0: res += n // k else: res += n // k + 1 return res l, r = max(sum(nums) // threshold, 1), max(1, sum(nums)) while l < r: m = (l + r) // 2 if getSum(m) > threshold: l = m + 1 else: r = m return l
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 RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL 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 VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def get_sum(d): ans = 0 for n in nums: ans += math.ceil(n / d) return ans l, r = 1, max(nums) ans = 0 while l < r: m = (l + r) // 2 test = get_sum(m) if test <= threshold: ans = m r = m else: l = m + 1 return l
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 VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: n, j = len(nums), sum(nums) i = (j + n) // threshold i = 1 if i == 0 else i while i < j - 1: k = (i + j) // 2 ss = sum([math.ceil(n / k) for n in nums]) if ss > threshold: i = k else: j = k if sum([math.ceil(n / i) for n in nums]) <= threshold: return i else: return j
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: end = 1 start = end def findSum(div): return sum([math.ceil(n / div) for n in nums]) while True: if findSum(end) > threshold: start = end end *= 2 else: break while end > start: mid = (start + end) // 2 if findSum(mid) > threshold: start = mid + 1 else: end = mid return end
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER 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
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: mx = max(nums) low = 1 high = mx best = mx while low < high: med = low + (high - low) // 2 total = 0 for num in nums: total += ceil(num / med) if total <= threshold: high = med best = min(best, med) else: low = med + 1 return best
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN 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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def BS(p, r): if r - p <= 1: if sum([((n - 1) // p + 1) for n in nums]) <= threshold: return p return r q = (r + p) // 2 e = sum([((n - 1) // q + 1) for n in nums]) if e <= threshold: return BS(p, q) else: return BS(q, r) r = max(nums) p = 1 return BS(p, r)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR RETURN VAR RETURN 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 RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: r = max(nums) l = 1 def calc(div): s = sum([((num + div - 1) // div) for num in nums]) return s <= threshold while l < r: mid = (l + r) // 2 val = calc(mid) if calc(mid): r = mid else: l = mid + 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: l, r = 1, max(nums) while l < r: m = (l + r) // 2 sum_num = 0 for i in nums: sum_num += (i + m - 1) // m if sum_num > threshold: 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 ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def helper(div): output = 0 for n in nums: if n % div == 0: output += n // div else: output += n // div + 1 if output <= threshold: return True return False nums.sort() l, r = 1, max(nums) while l < r: mid = l + (r - l) // 2 if helper(mid): r = mid else: l = mid + 1 return l
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 EXPR FUNC_CALL 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
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language. Note: Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned. Example 1: Input: N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"} Output: 1 Explanation: Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders. Example 2: Input: N = 3, K = 3 dict = {"caa","aaa","aab"} Output: 1 Explanation: Here order of characters is 'c', 'a', 'b' Note that words are sorted and in the given language "caa" comes before "aaa", therefore 'c' is before 'a' in output. Similarly we can find other orders. Your Task: You don't need to read or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language. Expected Time Complexity: O(N * |S| + K) , where |S| denotes maximum length. Expected Space Compelxity: O(K) Constraints: 1 ≤ N, M ≤ 300 1 ≤ K ≤ 26 1 ≤ Length of words ≤ 50
class Solution: def findOrder(self, alien_dict, N, M): adj = [set() for _ in range(M)] status = [(0) for _ in range(M)] S = max([(0) for _ in range(N)], key=lambda x: len(alien_dict[x])) match = alien_dict[0][0] for i in range(len(alien_dict[S])): for j in range(N): curr = alien_dict[j][: i + 1] if curr != match: if curr[:-1] == match[:-1]: adj[ord(match[-1]) - ord("a")].add(ord(curr[-1]) - ord("a")) match = curr ans = [] def dfs(node): if status[node]: return status[node] == 1 status[node] = 1 for neigh in adj[node]: if dfs(neigh): return True status[node] = 2 ans.append(chr(node + ord("a"))) return False for i in range(M): if not status[i] and dfs(i): return "" ans = ans[::-1] ans = "".join(ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language. Note: Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned. Example 1: Input: N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"} Output: 1 Explanation: Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders. Example 2: Input: N = 3, K = 3 dict = {"caa","aaa","aab"} Output: 1 Explanation: Here order of characters is 'c', 'a', 'b' Note that words are sorted and in the given language "caa" comes before "aaa", therefore 'c' is before 'a' in output. Similarly we can find other orders. Your Task: You don't need to read or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language. Expected Time Complexity: O(N * |S| + K) , where |S| denotes maximum length. Expected Space Compelxity: O(K) Constraints: 1 ≤ N, M ≤ 300 1 ≤ K ≤ 26 1 ≤ Length of words ≤ 50
class Solution: def findOrder(self, alien_dict, N, K): adj = {chr(97 + i): set() for i in range(K)} for i in range(N - 1): word_one, word_two = alien_dict[i], alien_dict[i + 1] length = min(len(word_one), len(word_two)) for j in range(length): if word_one[j] != word_two[j]: adj[word_one[j]].add(word_two[j]) break visited = {} res = [] def dfs(char): if char in visited: return visited[char] visited[char] = True for neighChar in adj[char]: if dfs(neighChar): return True visited[char] = False res.append(char) for char in adj: if dfs(char): return [] res.reverse() return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR RETURN LIST EXPR FUNC_CALL VAR RETURN VAR
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language. Note: Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned. Example 1: Input: N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"} Output: 1 Explanation: Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders. Example 2: Input: N = 3, K = 3 dict = {"caa","aaa","aab"} Output: 1 Explanation: Here order of characters is 'c', 'a', 'b' Note that words are sorted and in the given language "caa" comes before "aaa", therefore 'c' is before 'a' in output. Similarly we can find other orders. Your Task: You don't need to read or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language. Expected Time Complexity: O(N * |S| + K) , where |S| denotes maximum length. Expected Space Compelxity: O(K) Constraints: 1 ≤ N, M ≤ 300 1 ≤ K ≤ 26 1 ≤ Length of words ≤ 50
class Solution: def topoSort(self, adj, N): indegree = [(0) for i in range(0, N)] queue = [] topo = [] for i in range(0, N): for v in adj[i]: indegree[v] += 1 for i in range(0, N): if indegree[i] == 0: queue.append(i) while len(queue) != 0: node = queue.pop(0) topo.append(chr(node + 97)) for v in adj[node]: indegree[v] -= 1 if indegree[v] == 0: queue.append(v) return topo def findOrder(self, alien_dict, N, K): adj = [[] for i in range(0, K)] for i in range(0, N - 1): s1 = alien_dict[i] s2 = alien_dict[i + 1] l = min(len(s1), len(s2)) for i in range(0, l): if s1[i] != s2[i]: adj[ord(s1[i]) - 97].append(ord(s2[i]) - 97) break return self.topoSort(adj, K)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language. Note: Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned. Example 1: Input: N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"} Output: 1 Explanation: Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders. Example 2: Input: N = 3, K = 3 dict = {"caa","aaa","aab"} Output: 1 Explanation: Here order of characters is 'c', 'a', 'b' Note that words are sorted and in the given language "caa" comes before "aaa", therefore 'c' is before 'a' in output. Similarly we can find other orders. Your Task: You don't need to read or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language. Expected Time Complexity: O(N * |S| + K) , where |S| denotes maximum length. Expected Space Compelxity: O(K) Constraints: 1 ≤ N, M ≤ 300 1 ≤ K ≤ 26 1 ≤ Length of words ≤ 50
class Solution: def tropo_sort(self, adj, k): indegree = [0] * k for i in adj: for j in i: indegree[j] += 1 temp = [] for i in range(k): if indegree[i] == 0: temp.append(i) result = [] while temp: x = temp.pop(0) y = adj[x].copy() while y: l = y.pop(0) indegree[l] -= 1 if indegree[l] == 0: temp.append(l) result.append(chr(x + ord("a"))) return result def findOrder(self, alien_dict, N, K): adj = [] for i in range(k): adj.append([]) for i in range(len(alien_dict) - 1): s1 = alien_dict[i] s2 = alien_dict[i + 1] for j in range(min(len(s1), len(s2))): if s1[j] != s2[j]: adj[ord(s1[j]) - ord("a")].append(ord(s2[j]) - ord("a")) break return self.tropo_sort(adj, k)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR VAR
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language. Note: Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned. Example 1: Input: N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"} Output: 1 Explanation: Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders. Example 2: Input: N = 3, K = 3 dict = {"caa","aaa","aab"} Output: 1 Explanation: Here order of characters is 'c', 'a', 'b' Note that words are sorted and in the given language "caa" comes before "aaa", therefore 'c' is before 'a' in output. Similarly we can find other orders. Your Task: You don't need to read or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language. Expected Time Complexity: O(N * |S| + K) , where |S| denotes maximum length. Expected Space Compelxity: O(K) Constraints: 1 ≤ N, M ≤ 300 1 ≤ K ≤ 26 1 ≤ Length of words ≤ 50
class graph: def __init__(self, k): self.matrix = [[(False) for _ in range(k)] for _ in range(k)] self.ancestors = [(0) for _ in range(k)] self.index = {} self.next_index = 0 self.characters = "" def add_char(self, char): if char in self.index: return self.index[char] = self.next_index self.next_index += 1 self.characters += char def add_edge(self, first, second): i1 = self.index[first] i2 = self.index[second] if self.matrix[i1][i2]: return self.ancestors[i2] += 1 self.matrix[i1][i2] = True def dfs(self, location, order): if self.ancestors[location] > 1: self.ancestors[location] -= 1 return order.append(self.characters[location]) for i in range(len(self.matrix)): if self.matrix[location][i]: self.dfs(i, order) def topo_sort(self): order = [] for i in range(len(self.matrix)): if self.ancestors[i] == 0: self.dfs(i, order) return order class Solution: def findOrder(self, alien_dict, n, k): g = graph(k) for c in alien_dict[0]: g.add_char(c) for i in range(1, n): for c in alien_dict[i]: g.add_char(c) length = min(len(alien_dict[i]), len(alien_dict[i - 1])) for j in range(length): if alien_dict[i - 1][j] != alien_dict[i][j]: g.add_edge(alien_dict[i - 1][j], alien_dict[i][j]) break lst = g.topo_sort() order = "" for c in lst: order += c return order
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR STRING FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR NUMBER VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language. Note: Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned. Example 1: Input: N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"} Output: 1 Explanation: Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders. Example 2: Input: N = 3, K = 3 dict = {"caa","aaa","aab"} Output: 1 Explanation: Here order of characters is 'c', 'a', 'b' Note that words are sorted and in the given language "caa" comes before "aaa", therefore 'c' is before 'a' in output. Similarly we can find other orders. Your Task: You don't need to read or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language. Expected Time Complexity: O(N * |S| + K) , where |S| denotes maximum length. Expected Space Compelxity: O(K) Constraints: 1 ≤ N, M ≤ 300 1 ≤ K ≤ 26 1 ≤ Length of words ≤ 50
class Solution: def findOrder(self, alien_dict, N, K): alpha_num = {} num_alpha = {} for i in range(K): alpha_num[chr(i + 97)] = i num_alpha[i] = chr(i + 97) alien_dict = list(alien_dict) edges = [] for i in range(N - 1): word1 = alien_dict[i] word2 = alien_dict[i + 1] min1 = min(len(word1), len(word2)) ct = 0 while ct < min1: if word1[ct] != word2[ct]: edges.append([alpha_num[word1[ct]], alpha_num[word2[ct]]]) break ct += 1 adj = {} for i in range(K): adj[i] = [] for edge in edges: adj[edge[1]].append(edge[0]) def dfs(node, adj, vis, stack): vis[node] = 1 for adj_node in adj[node]: if vis[adj_node] == -1: dfs(adj_node, adj, vis, stack) stack.append(num_alpha[node]) vis = [-1] * K stack = [] for i in range(K): if vis[i] == -1: dfs(i, adj, vis, stack) return stack
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language. Note: Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned. Example 1: Input: N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"} Output: 1 Explanation: Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders. Example 2: Input: N = 3, K = 3 dict = {"caa","aaa","aab"} Output: 1 Explanation: Here order of characters is 'c', 'a', 'b' Note that words are sorted and in the given language "caa" comes before "aaa", therefore 'c' is before 'a' in output. Similarly we can find other orders. Your Task: You don't need to read or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language. Expected Time Complexity: O(N * |S| + K) , where |S| denotes maximum length. Expected Space Compelxity: O(K) Constraints: 1 ≤ N, M ≤ 300 1 ≤ K ≤ 26 1 ≤ Length of words ≤ 50
class Solution: def findOrder(self, alien_dict, N, K): code = [[0, i] for i in range(k)] def string(a, b): n = min(len(a), len(b)) for i in range(n): if a[i] != b[i]: code[ord(a[i]) - ord("a")][0] = ( code[ord(a[i]) - ord("a")][0] | 1 << ord(b[i]) - ord("a") | code[ord(b[i]) - ord("a")][0] ) return for i in range(N - 1, 0, -1): string(alien_dict[i - 1], alien_dict[i]) for i in range(N - 1, 0, -1): string(alien_dict[i - 1], alien_dict[i]) code.sort(key=lambda x: bin(x[0]).count("1")) ans = "" for a, b in code: ans = chr(b + ord("a")) + ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR STRING FOR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR RETURN VAR
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language. Note: Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned. Example 1: Input: N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"} Output: 1 Explanation: Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders. Example 2: Input: N = 3, K = 3 dict = {"caa","aaa","aab"} Output: 1 Explanation: Here order of characters is 'c', 'a', 'b' Note that words are sorted and in the given language "caa" comes before "aaa", therefore 'c' is before 'a' in output. Similarly we can find other orders. Your Task: You don't need to read or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language. Expected Time Complexity: O(N * |S| + K) , where |S| denotes maximum length. Expected Space Compelxity: O(K) Constraints: 1 ≤ N, M ≤ 300 1 ≤ K ≤ 26 1 ≤ Length of words ≤ 50
def get_order(word_list: [], k): adj_graph = get_relation_list(word_list, k) visited = [False] * k data_stack = [] for i in range(k): if not visited[i]: dfs_util(i, visited, adj_graph, data_stack) final_arr = [] while len(data_stack) > 0: pop_ele = data_stack.pop(-1) char_ele = chr(ord("a") + pop_ele) final_arr.append(char_ele) return final_arr def dfs_util(vertex: int, visited: [], adj_graph: [[]], data_stack: []): if visited[vertex]: return visited[vertex] = True for neighbor in adj_graph[vertex]: dfs_util(neighbor, visited, adj_graph, data_stack) data_stack.append(vertex) def get_relation_list(word_list: [], k): rel_arr = [[] for i in range(k)] i = 0 n = len(word_list) while i < n - 1: first_word = word_list[i] second_word = word_list[i + 1] min_len = min(len(first_word), len(second_word)) for j in range(min_len): if first_word[j] != second_word[j]: from_vertex = ord(first_word[j]) - ord("a") to_vertex = ord(second_word[j]) - ord("a") rel_arr[from_vertex].append(to_vertex) break i = i + 1 return rel_arr class Solution: def findOrder(self, alien_dict, N, K): return get_order(alien_dict, K)
FUNC_DEF LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR LIST LIST LIST LIST IF VAR VAR RETURN ASSIGN VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language. Note: Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned. Example 1: Input: N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"} Output: 1 Explanation: Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders. Example 2: Input: N = 3, K = 3 dict = {"caa","aaa","aab"} Output: 1 Explanation: Here order of characters is 'c', 'a', 'b' Note that words are sorted and in the given language "caa" comes before "aaa", therefore 'c' is before 'a' in output. Similarly we can find other orders. Your Task: You don't need to read or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language. Expected Time Complexity: O(N * |S| + K) , where |S| denotes maximum length. Expected Space Compelxity: O(K) Constraints: 1 ≤ N, M ≤ 300 1 ≤ K ≤ 26 1 ≤ Length of words ≤ 50
class Solution: def solve(self, src, visited, topo, adj): visited.add(src) for i in adj[src]: if i not in visited: self.solve(i, visited, topo, adj) topo.append(src) def findOrder(self, alien_dict, N, K): adj = [[] for _ in range(K)] for i in range(1, N): s1 = alien_dict[i - 1] s2 = alien_dict[i] for j in range(min(len(s1), len(s2))): if s1[j] != s2[j]: u = ord(s1[j]) - ord("a") v = ord(s2[j]) - ord("a") adj[u].append(v) break visited = set() topo = [] for i in range(K): if i not in visited: self.solve(i, visited, topo, adj) s = "".join(chr(node + ord("a")) for node in topo[::-1]) return s
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR NUMBER RETURN VAR
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $n$ integers written on it. Polycarp wrote the numbers from the disk into the $a$ array. It turned out that the drive works according to the following algorithm: the drive takes one positive number $x$ as input and puts a pointer to the first element of the $a$ array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $a$ array, the last element is again followed by the first one; as soon as the sum is at least $x$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $m$ questions. To answer the $i$-th of them, you need to find how many seconds the drive will work if you give it $x_i$ as input. Please note that in some cases the drive can work infinitely. For example, if $n=3, m=3$, $a=[1, -3, 4]$ and $x=[1, 5, 2]$, then the answers to the questions are as follows: the answer to the first query is $0$ because the drive initially points to the first item and the initial sum is $1$. the answer to the second query is $6$, the drive will spin the disk completely twice and the amount becomes $1+(-3)+4+1+(-3)+4+1=5$. the answer to the third query is $2$, the amount is $1+(-3)+4=2$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case consists of two positive integers $n$, $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of numbers on the disk and the number of asked questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). The third line of each test case contains $m$ positive integers $x_1, x_2, \ldots, x_m$ ($1 \le x \le 10^9$). It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$. -----Output----- Print $m$ numbers on a separate line for each test case. The $i$-th number is: $-1$ if the drive will run infinitely; the number of seconds the drive will run, otherwise. -----Examples----- Input 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 Output 0 6 2 -1 -1 1 3 -----Note----- None
import sys max_int = 2147483648 min_int = -max_int t = int(input()) for _t in range(t): n, m = map(int, sys.stdin.readline().split()) a = map(int, sys.stdin.readline().split()) x = map(int, sys.stdin.readline().split()) mx = 0 s = 0 maxs = [] for i, aa in enumerate(a): s += aa if s > mx: mx = s maxs.append((s, i)) full = s def search(v): l = -1 r = len(maxs) - 1 while r - l > 1: m = (r + l) // 2 if maxs[m][0] >= v: r = m else: l = m return maxs[r][1] out = [] for xx in x: tmp = 0 if not maxs: out.append(-1) continue elif xx > maxs[-1][0]: if full > 0: tmp = (xx - maxs[-1][0] - 1) // full + 1 xx -= full * tmp tmp *= n else: out.append(-1) continue out.append(tmp + search(xx)) print(" ".join(map(str, out)))
IMPORT ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $n$ integers written on it. Polycarp wrote the numbers from the disk into the $a$ array. It turned out that the drive works according to the following algorithm: the drive takes one positive number $x$ as input and puts a pointer to the first element of the $a$ array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $a$ array, the last element is again followed by the first one; as soon as the sum is at least $x$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $m$ questions. To answer the $i$-th of them, you need to find how many seconds the drive will work if you give it $x_i$ as input. Please note that in some cases the drive can work infinitely. For example, if $n=3, m=3$, $a=[1, -3, 4]$ and $x=[1, 5, 2]$, then the answers to the questions are as follows: the answer to the first query is $0$ because the drive initially points to the first item and the initial sum is $1$. the answer to the second query is $6$, the drive will spin the disk completely twice and the amount becomes $1+(-3)+4+1+(-3)+4+1=5$. the answer to the third query is $2$, the amount is $1+(-3)+4=2$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case consists of two positive integers $n$, $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of numbers on the disk and the number of asked questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). The third line of each test case contains $m$ positive integers $x_1, x_2, \ldots, x_m$ ($1 \le x \le 10^9$). It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$. -----Output----- Print $m$ numbers on a separate line for each test case. The $i$-th number is: $-1$ if the drive will run infinitely; the number of seconds the drive will run, otherwise. -----Examples----- Input 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 Output 0 6 2 -1 -1 1 3 -----Note----- None
for k in range(int(input())): n, m = list(map(int, input().split())) a = list(map(int, input().split())) x = list(map(int, input().split())) b = 0 c = 0 d = 0 e = [] y = [] n1 = 0 for i in range(n): if d == 0: if a[i] > 0: b += a[i] e.append([b, i]) n1 += 1 else: d = 1 c = a[i] else: c += a[i] if c > 0: d = 0 e.append([b + c, i]) b += c n1 += 1 c = 0 f = b + c if e == []: y = [(-1) for i in range(m)] else: g = e[-1][0] for i in range(m): if x[i] <= g: l = n1 / 2 h = round(l) while round(l) > 0: if e[h][0] < x[i]: h = min(n1 - 1, h + round(l / 2)) else: h = max(0, h - round(l / 2)) l /= 2 h -= 1 while h > 0 and e[h][0] == e[h + 1][0]: h -= 1 h = max(h, 0) while e[h][0] < x[i]: h += 1 y.append(e[h][1]) elif f > 0: x1 = (x[i] - g) // f if (x[i] - g) % f != 0: x1 += 1 x[i] -= x1 * f l = n1 / 2 h = round(l) while round(l) > 0: if e[h][0] < x[i]: h = min(n1 - 1, h + round(l / 2)) else: h = max(0, h - round(l / 2)) l /= 2 h -= 1 while h > 0 and e[h][0] == e[h + 1][0]: h -= 1 h = max(h, 0) while e[h][0] < x[i]: h += 1 y.append(e[h][1] + n * x1) else: y.append(-1) print(*y)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $n$ integers written on it. Polycarp wrote the numbers from the disk into the $a$ array. It turned out that the drive works according to the following algorithm: the drive takes one positive number $x$ as input and puts a pointer to the first element of the $a$ array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $a$ array, the last element is again followed by the first one; as soon as the sum is at least $x$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $m$ questions. To answer the $i$-th of them, you need to find how many seconds the drive will work if you give it $x_i$ as input. Please note that in some cases the drive can work infinitely. For example, if $n=3, m=3$, $a=[1, -3, 4]$ and $x=[1, 5, 2]$, then the answers to the questions are as follows: the answer to the first query is $0$ because the drive initially points to the first item and the initial sum is $1$. the answer to the second query is $6$, the drive will spin the disk completely twice and the amount becomes $1+(-3)+4+1+(-3)+4+1=5$. the answer to the third query is $2$, the amount is $1+(-3)+4=2$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case consists of two positive integers $n$, $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of numbers on the disk and the number of asked questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). The third line of each test case contains $m$ positive integers $x_1, x_2, \ldots, x_m$ ($1 \le x \le 10^9$). It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$. -----Output----- Print $m$ numbers on a separate line for each test case. The $i$-th number is: $-1$ if the drive will run infinitely; the number of seconds the drive will run, otherwise. -----Examples----- Input 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 Output 0 6 2 -1 -1 1 3 -----Note----- None
t = int(input()) for _ in range(t): n, m = list(map(int, input().split())) a = list(map(int, input().split())) xs = list(map(int, input().split())) pref = [a[i] for i in range(n)] pref_max = [a[i] for i in range(n)] for i in range(1, n): pref[i] += pref[i - 1] pref_max[i] = max(pref[i], pref_max[i - 1]) dlt = pref[-1] ans = [] if dlt <= 0: for x in xs: l = 0 r = n - 1 m = 0 res = n while l <= r: m = l + r >> 1 if pref_max[m] >= x: res = m r = m - 1 else: l = m + 1 if res == n: res = -1 ans.append(str(res)) else: mx = max(pref) for x in xs: k = max(0, (x - mx + dlt - 1) // dlt) x -= k * dlt l = 0 r = n - 1 m = 0 res = n while l <= r: m = l + r >> 1 if pref_max[m] >= x: res = m r = m - 1 else: l = m + 1 ans.append(str(k * n + res)) print(" ".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR 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 IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $n$ integers written on it. Polycarp wrote the numbers from the disk into the $a$ array. It turned out that the drive works according to the following algorithm: the drive takes one positive number $x$ as input and puts a pointer to the first element of the $a$ array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $a$ array, the last element is again followed by the first one; as soon as the sum is at least $x$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $m$ questions. To answer the $i$-th of them, you need to find how many seconds the drive will work if you give it $x_i$ as input. Please note that in some cases the drive can work infinitely. For example, if $n=3, m=3$, $a=[1, -3, 4]$ and $x=[1, 5, 2]$, then the answers to the questions are as follows: the answer to the first query is $0$ because the drive initially points to the first item and the initial sum is $1$. the answer to the second query is $6$, the drive will spin the disk completely twice and the amount becomes $1+(-3)+4+1+(-3)+4+1=5$. the answer to the third query is $2$, the amount is $1+(-3)+4=2$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case consists of two positive integers $n$, $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of numbers on the disk and the number of asked questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). The third line of each test case contains $m$ positive integers $x_1, x_2, \ldots, x_m$ ($1 \le x \le 10^9$). It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$. -----Output----- Print $m$ numbers on a separate line for each test case. The $i$-th number is: $-1$ if the drive will run infinitely; the number of seconds the drive will run, otherwise. -----Examples----- Input 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 Output 0 6 2 -1 -1 1 3 -----Note----- None
import sys zz = 1 sys.setrecursionlimit(10**5) if zz: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("all.txt", "w") di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x def ii(): return input().rstrip() def li(): return [int(xx) for xx in input().split()] def fli(): return [float(x) for x in input().split()] def dadd(d, p, val): if p in d: d[p].append(val) else: d[p] = [val] def gi(): return [xx for xx in input().split()] def gtc(tc, ans): print("Case #" + str(tc) + ":", ans) def cil(n, m): return n // m + int(n % m > 0) def fi(): return int(input()) def pro(a): return reduce(lambda a, b: a * b, a) def swap(a, i, j): a[i], a[j] = a[j], a[i] def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def gh(): sys.stdout.flush() def isvalid(i, j, n, m): return 0 <= i < n and 0 <= j < m def bo(i): return ord(i) - ord("a") def graph(n, m): for i in range(m): x, y = mi() a[x].append(y) a[y].append(x) t = fi() uu = t while t > 0: t -= 1 n, q = mi() a = li() pre = [0] m = [] for i in a: pre.append(pre[-1] + i) pre.pop(0) m = [pre[0]] for i in range(1, n): m.append(max(m[-1], pre[i])) p = li() for i in range(q): c = pre[-1] x = p[i] if c <= 0 and m[-1] < x: print(-1, end=" ") continue l = cur = 0 r = x while l <= r: mid = (l + r) // 2 if mid * c < x - m[-1]: l = mid + 1 else: cur = mid r = mid - 1 ans = cur * n x -= cur * c l = cur = 0 r = n - 1 while l <= r: mid = (l + r) // 2 if m[mid] < x: l = mid + 1 else: cur = mid r = mid - 1 ans += cur + 1 print(ans - 1, end=" ") print()
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $n$ integers written on it. Polycarp wrote the numbers from the disk into the $a$ array. It turned out that the drive works according to the following algorithm: the drive takes one positive number $x$ as input and puts a pointer to the first element of the $a$ array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $a$ array, the last element is again followed by the first one; as soon as the sum is at least $x$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $m$ questions. To answer the $i$-th of them, you need to find how many seconds the drive will work if you give it $x_i$ as input. Please note that in some cases the drive can work infinitely. For example, if $n=3, m=3$, $a=[1, -3, 4]$ and $x=[1, 5, 2]$, then the answers to the questions are as follows: the answer to the first query is $0$ because the drive initially points to the first item and the initial sum is $1$. the answer to the second query is $6$, the drive will spin the disk completely twice and the amount becomes $1+(-3)+4+1+(-3)+4+1=5$. the answer to the third query is $2$, the amount is $1+(-3)+4=2$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case consists of two positive integers $n$, $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of numbers on the disk and the number of asked questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). The third line of each test case contains $m$ positive integers $x_1, x_2, \ldots, x_m$ ($1 \le x \le 10^9$). It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$. -----Output----- Print $m$ numbers on a separate line for each test case. The $i$-th number is: $-1$ if the drive will run infinitely; the number of seconds the drive will run, otherwise. -----Examples----- Input 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 Output 0 6 2 -1 -1 1 3 -----Note----- None
t = int(input()) xs = [] pref = [] n = 0 class SegmentTree: def __init__(self, n): self.t = [(0) for _ in range(n * 4)] def Build(self, v, tl, tr): if tl + 1 == tr: self.t[v] = pref[tl] else: tm = tl + tr >> 1 self.Build(v * 2 + 1, tl, tm) self.Build(v * 2 + 2, tm, tr) self.t[v] = max(self.t[v * 2 + 1], self.t[v * 2 + 2]) def Get(self, v, tl, tr, l, r, val): if tl >= r or tr <= l or self.t[v] < val: return -1 if tl + 1 == tr: return tl else: tm = tl + tr >> 1 res = self.Get(v * 2 + 1, tl, tm, l, r, val) if res == -1: return self.Get(v * 2 + 2, tm, tr, l, r, val) return res tree = SegmentTree(0) def FindPos(val): return tree.Get(0, 0, n, 0, n, val) for _ in range(t): n, m = list(map(int, input().split())) a = list(map(int, input().split())) xs = list(map(int, input().split())) pref = [a[i] for i in range(n)] for i in range(1, n): pref[i] += pref[i - 1] tree = SegmentTree(n) tree.Build(0, 0, n) dlt = pref[-1] INF = 5 * 10**9 if dlt <= 0: for x in xs: pos = FindPos(x) print(pos, end=" ") print() else: mx = max(pref) for x in xs: k = max(0, (x - mx + dlt - 1) // dlt) ans = k * n x -= k * dlt ans += FindPos(x) print(ans, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR NUMBER NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $n$ integers written on it. Polycarp wrote the numbers from the disk into the $a$ array. It turned out that the drive works according to the following algorithm: the drive takes one positive number $x$ as input and puts a pointer to the first element of the $a$ array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $a$ array, the last element is again followed by the first one; as soon as the sum is at least $x$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $m$ questions. To answer the $i$-th of them, you need to find how many seconds the drive will work if you give it $x_i$ as input. Please note that in some cases the drive can work infinitely. For example, if $n=3, m=3$, $a=[1, -3, 4]$ and $x=[1, 5, 2]$, then the answers to the questions are as follows: the answer to the first query is $0$ because the drive initially points to the first item and the initial sum is $1$. the answer to the second query is $6$, the drive will spin the disk completely twice and the amount becomes $1+(-3)+4+1+(-3)+4+1=5$. the answer to the third query is $2$, the amount is $1+(-3)+4=2$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case consists of two positive integers $n$, $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of numbers on the disk and the number of asked questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). The third line of each test case contains $m$ positive integers $x_1, x_2, \ldots, x_m$ ($1 \le x \le 10^9$). It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$. -----Output----- Print $m$ numbers on a separate line for each test case. The $i$-th number is: $-1$ if the drive will run infinitely; the number of seconds the drive will run, otherwise. -----Examples----- Input 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 Output 0 6 2 -1 -1 1 3 -----Note----- None
import sys def findIndexGE(prefixSumsMax, startSum, query): n = len(prefixSumsMax) b = n i = -1 while b > 0: while i + b < n and startSum + prefixSumsMax[i + b] < query: i += b b //= 2 i += 1 return i def main(): t = int(input()) allans = [] for _ in range(t): n, m = readIntArr() arr = readIntArr() queries = readIntArr() prefixSums = arr.copy() for i in range(1, n): prefixSums[i] += prefixSums[i - 1] maxP = max(prefixSums) prefixSumsMax = prefixSums.copy() for i in range(1, n): prefixSumsMax[i] = max(prefixSumsMax[i - 1], prefixSums[i]) ans = [] for q in queries: if prefixSums[n - 1] <= 0: if q > maxP: ans.append(-1) else: ans.append(findIndexGE(prefixSumsMax, 0, q)) else: nCycles = -1 b = 10**9 while b > 0: while prefixSums[n - 1] * (nCycles + b) + maxP < q: nCycles += b b //= 2 nCycles += 1 startSum = prefixSums[n - 1] * nCycles lastCycleIdx = findIndexGE(prefixSumsMax, startSum, q) ans.append(nCycles * n + lastCycleIdx) allans.append(ans) multiLineArrayOfArraysPrint(allans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] inf = float("inf") MOD = 10**9 + 7 main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NUMBER WHILE BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $n$ integers written on it. Polycarp wrote the numbers from the disk into the $a$ array. It turned out that the drive works according to the following algorithm: the drive takes one positive number $x$ as input and puts a pointer to the first element of the $a$ array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $a$ array, the last element is again followed by the first one; as soon as the sum is at least $x$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $m$ questions. To answer the $i$-th of them, you need to find how many seconds the drive will work if you give it $x_i$ as input. Please note that in some cases the drive can work infinitely. For example, if $n=3, m=3$, $a=[1, -3, 4]$ and $x=[1, 5, 2]$, then the answers to the questions are as follows: the answer to the first query is $0$ because the drive initially points to the first item and the initial sum is $1$. the answer to the second query is $6$, the drive will spin the disk completely twice and the amount becomes $1+(-3)+4+1+(-3)+4+1=5$. the answer to the third query is $2$, the amount is $1+(-3)+4=2$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case consists of two positive integers $n$, $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of numbers on the disk and the number of asked questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). The third line of each test case contains $m$ positive integers $x_1, x_2, \ldots, x_m$ ($1 \le x \le 10^9$). It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$. -----Output----- Print $m$ numbers on a separate line for each test case. The $i$-th number is: $-1$ if the drive will run infinitely; the number of seconds the drive will run, otherwise. -----Examples----- Input 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 Output 0 6 2 -1 -1 1 3 -----Note----- None
def findIncrementIndex(sumIncrementIndices, x): l = 0 r = len(sumIncrementIndices) while l < r: m = (l + r) // 2 if sumIncrementIndices[m][0] < x: l = m + 1 else: r = m return sumIncrementIndices[r][1] def solve(A, n, X, m): runningSum = 0 lastIncrementedSum = 0 sumIncrementIndices = [] for i in range(n): runningSum += A[i] if runningSum > lastIncrementedSum: sumIncrementIndices.append((runningSum, i)) lastIncrementedSum = runningSum result = [] totalSum = runningSum for i in range(m): x = X[i] seconds = 0 if x > lastIncrementedSum: if totalSum <= 0: result.append(-1) continue roundsCount = (x - lastIncrementedSum + totalSum - 1) // totalSum seconds = roundsCount * n x -= roundsCount * totalSum seconds += findIncrementIndex(sumIncrementIndices, x) result.append(seconds) return result t = int(input()) for tc in range(t): n, m = map(int, input().split()) A = list(map(int, input().split())) X = list(map(int, input().split())) result = solve(A, n, X, m) print(*result)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $n$ integers written on it. Polycarp wrote the numbers from the disk into the $a$ array. It turned out that the drive works according to the following algorithm: the drive takes one positive number $x$ as input and puts a pointer to the first element of the $a$ array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $a$ array, the last element is again followed by the first one; as soon as the sum is at least $x$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $m$ questions. To answer the $i$-th of them, you need to find how many seconds the drive will work if you give it $x_i$ as input. Please note that in some cases the drive can work infinitely. For example, if $n=3, m=3$, $a=[1, -3, 4]$ and $x=[1, 5, 2]$, then the answers to the questions are as follows: the answer to the first query is $0$ because the drive initially points to the first item and the initial sum is $1$. the answer to the second query is $6$, the drive will spin the disk completely twice and the amount becomes $1+(-3)+4+1+(-3)+4+1=5$. the answer to the third query is $2$, the amount is $1+(-3)+4=2$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case consists of two positive integers $n$, $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of numbers on the disk and the number of asked questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). The third line of each test case contains $m$ positive integers $x_1, x_2, \ldots, x_m$ ($1 \le x \le 10^9$). It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$. -----Output----- Print $m$ numbers on a separate line for each test case. The $i$-th number is: $-1$ if the drive will run infinitely; the number of seconds the drive will run, otherwise. -----Examples----- Input 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 Output 0 6 2 -1 -1 1 3 -----Note----- None
t = int(input()) for __ in range(t): n, m = map(int, input().split(" ")) arr = [int(val) for val in input().split(" ")] x = [int(val) for val in input().split(" ")] stack = [] csum = 0 cmax = 0 for a in arr: csum += a cmax = max(cmax, csum) stack.append(cmax) res = [0] * m for i, y in enumerate(x): if csum <= 0: if y > cmax: res[i] = -1 else: left = 0 right = len(stack) while left < right: mid = (left + right) // 2 if stack[mid] >= y: right = mid else: left = mid + 1 res[i] = left else: num_req = max(0, (y - stack[-1] + csum - 1) // csum) left = 0 right = len(stack) while left < right: mid = (left + right) // 2 if stack[mid] >= y - csum * num_req: right = mid else: left = mid + 1 res[i] = num_req * n + left print(" ".join(map(str, res)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(log n) time and O(1) space.
class Solution: def singleNonDuplicate(self, nums): while nums: if len(nums) > 1 and nums[0] == nums[1]: nums.pop(0) nums.pop(0) else: return nums[0]
CLASS_DEF FUNC_DEF WHILE VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(log n) time and O(1) space.
class Solution: def singleNonDuplicate(self, nums): left = 0 if len(nums) == 1: return nums[left] right = len(nums) - 1 while left < right: mid = left + (right - left) // 2 if mid % 2 == 1: mid -= 1 if nums[mid + 1] == nums[mid]: left = mid + 2 else: right = mid return nums[left]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR 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 BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(log n) time and O(1) space.
class Solution: def singleNonDuplicate(self, nums): count = len(nums) lo = 0 hi = int(count / 2) while lo < hi: m = int((lo + hi) / 2) if nums[2 * m] != nums[2 * m + 1]: hi = m else: lo = m + 1 return nums[2 * lo]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP NUMBER VAR
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(log n) time and O(1) space.
class Solution: def singleNonDuplicate(self, nums): l, r = 0, len(nums) - 1 while l < r: mid = (l + r) // 2 if nums[mid] == nums[mid - 1]: if (r - mid) % 2 == 1: l = mid + 1 else: r = mid - 2 elif nums[mid] == nums[mid + 1]: if (mid - l) % 2 == 1: r = mid - 1 else: l = mid + 2 else: return nums[mid] return nums[l]
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 BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN VAR VAR
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(log n) time and O(1) space.
class Solution: def singleNonDuplicate(self, nums): return self.singleNonDuplicateUtil(nums, 0, len(nums) - 1) def singleNonDuplicateUtil(self, nums, l, r): if l < r: mid = int((l + r) * 0.5) if mid - 1 >= 0 and nums[mid - 1] != nums[mid]: mid = mid - 1 if (mid - l + 1) % 2 == 0: l = mid + 1 else: r = mid return self.singleNonDuplicateUtil(nums, l, r) else: return nums[l]
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(log n) time and O(1) space.
class Solution: def singleNonDuplicate(self, nums): if len(nums) == 1: return nums[0] startIndex = 0 endIndex = len(nums) - 1 while startIndex < endIndex: midIndex = (endIndex - startIndex) // 2 + startIndex midValue = nums[midIndex] leftValue = nums[midIndex - 1] if midIndex - 1 >= 0 else None rightValue = nums[midIndex + 1] if midIndex + 1 < len(nums) else None numLeftUnknowns = midIndex numRightUnknowns = len(nums) - 1 - midIndex if leftValue != midValue and rightValue != midValue: return midValue elif leftValue == midValue: numLeftUnknowns -= 1 else: numRightUnknowns -= 1 if numLeftUnknowns % 2 != 0: endIndex = midIndex - 1 else: startIndex = midIndex + 1 return nums[startIndex]
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 BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NONE ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(log n) time and O(1) space.
class Solution: def singleNonDuplicate(self, nums): if len(nums) == 1: return nums[0] flag = 0 for i in range(len(nums)): if flag == 0: if i == len(nums) - 1: return nums[i] if nums[i] == nums[i + 1]: flag = 1 else: return nums[i] else: flag = 0 continue
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(log n) time and O(1) space.
class Solution: def singleNonDuplicate(self, nums): i, j = 0, len(nums) - 1 while i < j: mid = i + (j - i) // 2 if mid % 2 == 1: mid -= 1 if nums[mid] == nums[mid + 1]: i = mid + 2 else: j = mid return nums[i]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first. Example 1: Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 Output: ["i", "love"] Explanation: "i" and "love" are the two most frequent words. Note that "i" comes before "love" due to a lower alphabetical order. Example 2: Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 Output: ["the", "is", "sunny", "day"] Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Input words contain only lowercase letters. Follow up: Try to solve it in O(n log k) time and O(n) extra space.
class Solution: def topKFrequent(self, words, k): counts = collections.Counter(words) items = list(counts.items()) items.sort(key=lambda item: (-item[1], item[0])) return [item[0] for item in items[0:k]]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR VAR NUMBER VAR
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first. Example 1: Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 Output: ["i", "love"] Explanation: "i" and "love" are the two most frequent words. Note that "i" comes before "love" due to a lower alphabetical order. Example 2: Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 Output: ["the", "is", "sunny", "day"] Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Input words contain only lowercase letters. Follow up: Try to solve it in O(n log k) time and O(n) extra space.
class Solution: def topKFrequent(self, words, k): cnt = {i: words.count(i) for i in list(set(words))} cntRev = {} ans = [] for key, val in cnt.items(): if val not in cntRev: cntRev[val] = [key] else: cntRev[val].append(key) for num in sorted(cntRev, reverse=True): if k <= len(cntRev[num]): ans += sorted(cntRev[num])[:k] break else: ans += sorted(cntRev[num]) k -= len(cntRev[num]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first. Example 1: Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 Output: ["i", "love"] Explanation: "i" and "love" are the two most frequent words. Note that "i" comes before "love" due to a lower alphabetical order. Example 2: Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 Output: ["the", "is", "sunny", "day"] Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Input words contain only lowercase letters. Follow up: Try to solve it in O(n log k) time and O(n) extra space.
class Solution: def topKFrequent(self, words, k): times_dict = {} for i in words: if i not in times_dict.keys(): times_dict[i] = 1 else: times_dict[i] += 1 return_list = [] for i in range(k): max = 0 max_key = "" for j in times_dict.keys(): if times_dict[j] > max: max = times_dict[j] max_key = j elif times_dict[j] == max and j < max_key: max = times_dict[j] max_key = j del times_dict[max_key] return_list.append(max_key) return return_list
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first. Example 1: Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 Output: ["i", "love"] Explanation: "i" and "love" are the two most frequent words. Note that "i" comes before "love" due to a lower alphabetical order. Example 2: Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 Output: ["the", "is", "sunny", "day"] Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Input words contain only lowercase letters. Follow up: Try to solve it in O(n log k) time and O(n) extra space.
class Solution: def topKFrequent(self, words, k): d = {} for word in words: d[word] = d.get(word, 0) + 1 ret = sorted(d, key=lambda word: (-d[word], word)) return ret[:k]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first. Example 1: Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 Output: ["i", "love"] Explanation: "i" and "love" are the two most frequent words. Note that "i" comes before "love" due to a lower alphabetical order. Example 2: Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 Output: ["the", "is", "sunny", "day"] Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Input words contain only lowercase letters. Follow up: Try to solve it in O(n log k) time and O(n) extra space.
class Solution: def topKFrequent(self, words, k): c = collections.Counter(words).most_common() res = [] while len(res) < k: temp = [] node = c.pop(0) temp.append(node[0]) while c and c[0][1] == node[1]: node = c.pop(0) temp.append(node[0]) res.extend(sorted(temp)) return res[:k]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first. Example 1: Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 Output: ["i", "love"] Explanation: "i" and "love" are the two most frequent words. Note that "i" comes before "love" due to a lower alphabetical order. Example 2: Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 Output: ["the", "is", "sunny", "day"] Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Input words contain only lowercase letters. Follow up: Try to solve it in O(n log k) time and O(n) extra space.
class Solution: def topKFrequent(self, words, k): counter = collections.Counter(words) keys = list(counter.keys()) keys.sort(key=lambda x: (-counter[x], x)) return keys[:k]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first. Example 1: Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 Output: ["i", "love"] Explanation: "i" and "love" are the two most frequent words. Note that "i" comes before "love" due to a lower alphabetical order. Example 2: Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 Output: ["the", "is", "sunny", "day"] Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Input words contain only lowercase letters. Follow up: Try to solve it in O(n log k) time and O(n) extra space.
class Solution: def topKFrequent(self, words, k): wDict = collections.Counter(words) res = [] maxHeap = [] for key, cnt in list(wDict.items()): heapq.heappush(maxHeap, (-cnt, key)) for i in range(k): freq, word = heapq.heappop(maxHeap) res.append(word) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): if len(nums) < 3: return False stack = [[nums[0], nums[0]]] m = nums[0] for num in nums[1:]: if num <= m: m = num else: while stack and num > stack[-1][0]: if num < stack[-1][1]: return True else: stack.pop() stack.append([m, num]) return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN NUMBER
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): if not nums: return False mins = [(-1) for i in range(len(nums))] mins[0] = nums[0] for i in range(1, len(nums)): mins[i] = min(mins[i - 1], nums[i]) s = [] for i in range(len(nums) - 1, -1, -1): if nums[i] != mins[i]: while s and s[-1] <= mins[i]: s.pop() if s and s[-1] < nums[i]: return True s.append(nums[i]) return False
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): if len(nums) < 3: return False minV = [] for i in range(len(nums)): if i == 0: minV.append(nums[i]) else: minV.append(min(nums[i], minV[-1])) st = [] for j in range(len(nums) - 1, 0, -1): if not st or nums[j] <= st[-1]: st.append(nums[j]) else: while st and nums[j] > st[-1]: s3 = st.pop() if s3 > minV[j]: return True st.append(nums[j]) return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): stack = [] max_c = -math.inf for i in range(len(nums) - 1, -1, -1): if nums[i] < max_c: return True while stack and stack[-1] < nums[i]: max_c = max(max_c, stack.pop()) stack.append(nums[i]) return False
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): if len(nums) < 3: return False minx = nums[0] seg = [(nums[0], nums[0])] for x in nums[1:]: while seg and seg[-1][1] <= x: seg.pop() if seg and x > seg[-1][0]: return True minx = min(minx, x) seg.append((minx, x)) return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): stack = [] for num in nums: if not stack or num < stack[-1][0]: stack.append([num, num]) elif num > stack[-1][0]: if num < stack[-1][1]: return True else: node = stack.pop() while stack and num >= stack[-1][1]: stack.pop() if stack and num > stack[-1][0]: return True stack.append([node[0], num]) return False
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR RETURN NUMBER
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): if len(nums) < 3: return False s2_candidate = [nums[-1]] cur_idx = len(nums) - 2 while cur_idx >= 0 and nums[cur_idx] <= s2_candidate[-1]: s2_candidate.append(nums[cur_idx]) cur_idx -= 1 if cur_idx < 0: return False s3 = nums[cur_idx] while s2_candidate and s2_candidate[-1] < s3: s2 = s2_candidate.pop() cur_idx -= 1 while cur_idx >= 0: if nums[cur_idx] < s2: return True elif nums[cur_idx] >= s3: if nums[cur_idx] > s3: s2 = s3 s3 = nums[cur_idx] while s2_candidate and s2_candidate[-1] <= s3: next_s2 = s2_candidate.pop() if next_s2 > s2 and next_s2 < s3: s2 = next_s2 elif nums[cur_idx] < s3 and nums[cur_idx] > s2: s2_candidate.append(nums[cur_idx]) cur_idx -= 1 return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): s3 = -(2**64) - 1 maxset = [] for i in reversed(nums): if i < s3: return True else: while len(maxset) > 0 and i > maxset[-1]: s3 = maxset.pop(-1) maxset.append(i) return False
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): if len(nums) < 3: return False low = [float("inf")] high = [float("inf")] currlow, currhigh = nums[0], nums[0] for i in range(1, len(nums)): while nums[i] > high[-1]: low.pop() high.pop() if low[-1] < nums[i] < high[-1]: return True if nums[i] == currlow or nums[i] == currhigh: continue elif nums[i] < currlow: low.append(currlow) high.append(currhigh) currlow, currhigh = nums[i], nums[i] elif nums[i] > currhigh: currhigh = nums[i] else: return True return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING ASSIGN VAR LIST FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Note: n will be less than 15,000. Example 1: Input: [1, 2, 3, 4] Output: False Explanation: There is no 132 pattern in the sequence. Example 2: Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
class Solution: def find132pattern(self, nums): min_nums = [0] * len(nums) for i, num in enumerate(nums): min_nums[i] = min(min_nums[i - 1], num) if i else num stack = [] for i, num in reversed(list(enumerate(nums))): while stack and stack[-1] <= min_nums[i]: stack.pop() if stack and num > stack[-1]: return True else: stack.append(num) return False
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Output: 4 Note: You may assume k is always valid, 1 ≤ k ≤ array's length.
class Solution: def findKthLargest(self, nums, k): def partition(nums, ind, i, j): big, scan = i, i nums[ind], nums[j] = nums[j], nums[ind] while scan < j: if nums[scan] > nums[j]: nums[big], nums[scan] = nums[scan], nums[big] big += 1 scan += 1 nums[j], nums[big] = nums[big], nums[j] return big i, j = 0, len(nums) - 1 while i <= j: ind = random.randint(i, j) new_ind = partition(nums, ind, i, j) if new_ind == k - 1: return nums[new_ind] elif new_ind < k - 1: i = new_ind + 1 else: j = new_ind - 1 return -1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Output: 4 Note: You may assume k is always valid, 1 ≤ k ≤ array's length.
class Solution: def swap(self, A, i, j): if A[i] != A[j]: A[i], A[j] = A[j], A[i] def partition(self, A, p, r): self.swap(A, r, random.randint(p, r)) x = A[r] j = p - 1 for i in range(p, r): if A[i] <= x: j += 1 self.swap(A, i, j) j += 1 self.swap(A, r, j) return j def findElement(self, A, k): low, high = 0, len(A) - 1 while low < high: i = self.partition(A, low, high) if i > k: high = i - 1 elif i < k: low = i + 1 else: return A[k] return A[k] def findKthLargest(self, nums, k): return self.findElement(nums, len(nums) - k)
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Output: 4 Note: You may assume k is always valid, 1 ≤ k ≤ array's length.
class Solution: def findKthLargest(self, nums, k): if not nums: return -1 pq = [] for num in nums: if len(pq) < k: heapq.heappush(pq, num) else: popped = heapq.heappop(pq) if popped < num: heapq.heappush(pq, num) else: heapq.heappush(pq, popped) return heapq.heappop(pq)
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Output: 4 Note: You may assume k is always valid, 1 ≤ k ≤ array's length.
class Solution: def findKthLargest(self, nums, k): heap = [] for num in nums: heapq.heappush(heap, num) for _ in range(len(nums) - k): heapq.heappop(heap) return heapq.heappop(heap)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): hmap = {} for val in a: hmap[val] = hmap.get(val, 0) + 1 temp = [(hmap[key], key) for key in hmap] temp.sort(key=lambda x: (-x[0], x[1])) i = 0 for tup in temp: freq, val = tup for j in range(freq): a[i] = val i += 1 return a
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): a.sort() freq_arr = [] if len(a) == 1: freq_arr.append((a[0], 1)) else: freq = 1 for i in range(1, len(a)): if a[i] == a[i - 1]: freq += 1 else: freq_arr.append((a[i - 1], freq)) freq = 1 freq_arr.append((a[-1], freq)) freq_arr_sorted = sorted(freq_arr, key=lambda x: x[-1], reverse=True) res = [] for item in freq_arr_sorted: res.extend([item[0]] * item[1]) return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR NUMBER VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): d = {} d_ = {} ans = [] for i in range(n): if a[i] not in d.keys(): d[a[i]] = 1 else: d[a[i]] += 1 for i in d.keys(): if d[i] not in d_.keys(): d_[d[i]] = [i] else: d_[d[i]].append(i) for i in sorted(d_, reverse=True): d_[i].sort() for j in range(len(d_[i])): for k in range(i): ans.append(d_[i][j]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): d = {} for i in a: if i in d: d[i] += 1 else: d[i] = 1 return sorted(a, key=lambda x: (-d[x], x))
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): count = dict() for x in a: if count.get(x) == None: count[x] = 1 else: count[x] += 1 a.sort(key=lambda e: [count[e], -e]) return a[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR NUMBER
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): f = {} for i in a: if i in f: f[i] += 1 else: f[i] = 1 a = sorted(a) a = sorted(a, key=lambda x: f[x], reverse=True) return a
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): d = {} for i in a: if i not in d: d[i] = 1 else: d[i] += 1 x = sorted(d.items()) y = sorted(x, key=lambda x: x[1], reverse=True) res = [] for k, v in y: j = v * [k] res.extend(j) return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): d = {} for i in a: if i in d: d[i] += 1 else: d[i] = 1 l = [] for i, j in d.items(): l.append([i, j]) l.sort(key=lambda x: (-x[1], x[0])) res = [] for i in l: for j in range(i[1]): res.append(i[0]) return res
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 VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): a_dict = dict() for i in a: if i in a_dict: a_dict[i] += 1 else: a_dict[i] = 1 sorted_a_dict = list( sorted(a_dict.items(), key=lambda item: [item[1], -item[0]], reverse=True) ) res = list() for ele, freq in sorted_a_dict: res += [ele] * freq return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR BIN_OP LIST VAR VAR RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): a.sort() cnt = {} for i in range(n): if a[i] in cnt: cnt[a[i]] += 1 else: cnt[a[i]] = 1 def givefreq(c): return cnt[c] a.sort(key=givefreq, reverse=True) return a
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF RETURN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): freq = [(0) for i in range(max(a) + 1)] hash_for_freq = [[] for i in range(n + 1)] for num in a: freq[num] += 1 for i in range(max(a) + 1): if freq[i]: hash_for_freq[freq[i]].append(i) l = [] for i in range(n, 0, -1): for num in hash_for_freq[i]: for j in range(i): l.append(num) return l
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): f = {} for i in a: if i in f: f[i] += 1 else: f[i] = 1 s = sorted(f.items(), key=lambda x: (-x[1], x[0])) x = [] for i in s: x.extend([i[0]] * i[1]) return x
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR NUMBER VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): mapp = {} arr1 = list(set(a)) n = len(list(set(a))) for i in a: if i in mapp: mapp[i] += 1 else: mapp[i] = 1 a.sort(key=lambda x: (-mapp[x], x), reverse=True) return a[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR NUMBER
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): A = {} l = [] ans = [] for i in a: if i in A: A[i] += 1 else: A[i] = 1 for i in A: l.append((A[i], i)) l.sort(key=lambda x: (-x[0], x[1])) for i in l: ans += [i[1]] * i[0] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR BIN_OP LIST VAR NUMBER VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): hash = dict() for i in range(n): hash[a[i]] = hash.get(a[i], 0) + 1 arr = [] for key, val in sorted(hash.items(), key=lambda x: (-x[1], x[0])): for i in range(val): arr.append(key) return arr
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): a.sort() a1 = {} for i in a: if i not in a1: a1[i] = 0 a1[i] += 1 a2 = {} d = [] for i in a: if a1[i] not in a2: a2[a1[i]] = [] d.append(a1[i]) a2[a1[i]].append(i) d.sort(reverse=True) e = [] for i in d: e += a2[i] return e
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): r = [] d = {} for b in a: if b in d: d[b] = d[b] + 1 else: d[b] = 1 for b in d: r.append((b, d[b])) r.sort(key=lambda x: (-x[1], x[0])) res = [] for x in r: freq = x[1] for i in range(freq): res.append(x[0]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): d = {} for i in a: if i not in d: d[i] = 1 else: d[i] += 1 k = sorted(d.items(), key=lambda x: x[1], reverse=True) i = 0 temp, res = [], [] while i < len(k): while i + 1 < len(k) and k[i][1] == k[i + 1][1]: temp += [k[i][0]] * k[i][1] i += 1 temp += [k[i][0]] * k[i][1] temp.sort() res += temp temp = [] i += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST WHILE VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP LIST VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP LIST VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): d = {x: (0) for x in a} for i in a: d[i] += 1 a.sort(key=lambda x: (-d[x], x)) return a
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an array of integers, sort the array according to frequency of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first. Example 1: Input: N = 5 A[] = {5,5,4,6,4} Output: 4 4 5 5 6 Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smallerelement comes first. So 4 4 comes firstthen comes 5 5. Finally comes 6. The output is 4 4 5 5 6. Example 2: Input: N = 5 A[] = {9,9,9,2,5} Output: 9 9 9 2 5 Explanation: The highest frequency here is 3. The element 9 has the highest frequency So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first. The output is 9 9 9 2 5. Your Task: You only need to complete the function sortByFreq that takes arr, and n as parameters and returns the sorted array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A_{i} ≤ 10^{5}
class Solution: def sortByFreq(self, a, n): f = {} for i in range(n): if a[i] not in f: f[a[i]] = 1 else: f[a[i]] += 1 a.sort(key=lambda x: (-f[x], x)) return a
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR