description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
nums = self.merge(nums)
return nums
def merge(self, values):
if len(values) > 1:
m = len(values) // 2
left = values[:m]
right = values[m:]
left = self.merge(left)
right = self.merge(right)
values = []
while len(left) > 0 and len(right) > 0:
if left[0] < right[0]:
values.append(left[0])
left.pop(0)
else:
values.append(right[0])
right.pop(0)
for i in left:
values.append(i)
for i in right:
values.append(i)
return values | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | def merge(L, R):
if len(L) == 0:
return R
if len(R) == 0:
return L
l = 0
r = 0
res = []
while len(res) < len(L) + len(R):
if L[l] < R[r]:
res.append(L[l])
l += 1
else:
res.append(R[r])
r += 1
if l == len(L):
res += R[r:]
break
if r == len(R):
res += L[l:]
break
return res
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) < 2:
return nums
mid = len(nums) // 2
L = self.sortArray(nums[:mid])
R = self.sortArray(nums[mid:])
return merge(L, R) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def partition(self, arr, pi):
less, equal, high = [], [], []
for i in arr:
if i < pi:
less.append(i)
if i == pi:
equal.append(i)
if i > pi:
high.append(i)
return less, equal, high
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) < 1:
return nums
pivot = nums[0]
less, equal, high = self.partition(nums, pivot)
return self.sortArray(less) + equal + self.sortArray(high) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if nums == [] or len(nums) == 1:
return nums
m = len(nums) // 2
left = self.sortArray(nums[:m])
right = self.sortArray(nums[m:])
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
nums[k] = left[i]
i += 1
else:
nums[k] = right[j]
j += 1
k += 1
while j < len(right):
nums[k] = right[j]
j += 1
k += 1
while i < len(left):
nums[k] = left[i]
i += 1
k += 1
return nums | CLASS_DEF FUNC_DEF VAR VAR IF VAR LIST FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
length = len(nums)
for i in range(length - 1, -1, -1):
self.maxheap(nums, length, i)
for i in range(length - 1, 0, -1):
nums[0], nums[i] = nums[i], nums[0]
self.maxheap(nums, i, 0)
return nums
def maxheap(self, nums, n, node):
l = node * 2 + 1
r = node * 2 + 2
large = node
if l < n and nums[l] > nums[large]:
large = l
if r < n and nums[r] > nums[large]:
large = r
if large != node:
nums[node], nums[large] = nums[large], nums[node]
self.maxheap(nums, n, large) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) < 2:
return nums
def merge(l1, l2):
n1 = n2 = 0
res = []
while n1 < len(l1) and n2 < len(l2):
if l1[n1] < l2[n2]:
res.append(l1[n1])
n1 += 1
else:
res.append(l2[n2])
n2 += 1
res += l1[n1:]
res += l2[n2:]
return res
mid = len(nums) // 2
return merge(self.sortArray(nums[:mid]), self.sortArray(nums[mid:])) | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return self.mergeSort(nums, 0, len(nums) - 1)
def mergeSort(self, arr, i, j):
if i == j:
return [arr[i]]
mid = i + (j - i) // 2
left_arr = self.mergeSort(arr, i, mid)
right_arr = self.mergeSort(arr, mid + 1, j)
return self.merge(left_arr, right_arr)
def merge(self, a, b):
res = []
i = 0
j = 0
while len(res) < len(a) + len(b):
if j == len(b) or i < len(a) and a[i] < b[j]:
res.append(a[i])
i += 1
else:
res.append(b[j])
j += 1
return res | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_DEF IF VAR VAR RETURN LIST VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) == 1:
return nums
if len(nums) > 1:
mid = len(nums) // 2
left = nums[:mid]
right = nums[mid:]
self.sortArray(left)
self.sortArray(right)
self.merge(left, right, nums)
return nums
def merge(self, left, right, nums):
i = 0
j = 0
k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
nums[k] = left[i]
i += 1
k += 1
else:
nums[k] = right[j]
j += 1
k += 1
while i < len(left):
nums[k] = left[i]
i += 1
k += 1
while j < len(right):
nums[k] = right[j]
j += 1
k += 1
return | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def merge(self, arr1: List[int], arr2: List[int]) -> List[int]:
ret = []
ix1 = 0
ix2 = 0
while ix1 != len(arr1) and ix2 != len(arr2):
if arr1[ix1] < arr2[ix2]:
ret.append(arr1[ix1])
ix1 += 1
else:
ret.append(arr2[ix2])
ix2 += 1
if ix1 < len(arr1):
ret.extend(arr1[ix1:])
else:
ret.extend(arr2[ix2:])
return ret
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) == 1:
return nums
mid = len(nums) // 2
left = self.sortArray(nums[:mid])
right = self.sortArray(nums[mid:])
return self.merge(left, right) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def heapSort(arr):
n = len(arr)
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
heapSort(nums)
return nums | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) == 0:
return []
pivot = nums[0]
left = [x for x in nums[1:] if x <= pivot]
right = [x for x in nums[1:] if x > pivot]
return self.sortArray(left) + [pivot] + self.sortArray(right) | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def merge_sort(left, right):
res = []
left_p = right_p = 0
while left_p < len(left) and right_p < len(right):
if left[left_p] < right[right_p]:
res.append(left[left_p])
left_p += 1
else:
res.append(right[right_p])
right_p += 1
res.extend(left[left_p:])
res.extend(right[right_p:])
return res
if len(nums) <= 1:
return nums
middle = len(nums) // 2
left = self.sortArray(nums[:middle])
right = self.sortArray(nums[middle:])
if len(left) > len(right):
return merge_sort(right, left)
else:
return merge_sort(left, right) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return self.quicksort(nums)
def quicksort(self, nums):
if len(nums) == 1 or len(nums) == 0:
return nums
pivot = nums[len(nums) // 2]
left = [x for x in nums if x < pivot]
mid = [x for x in nums if x == pivot]
right = [x for x in nums if x > pivot]
return self.quicksort(left) + mid + self.quicksort(right) | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) <= 1:
return nums
pivot = len(nums) // 2
left_list = self.sortArray(nums[:pivot])
right_list = self.sortArray(nums[pivot:])
return self.merge_sort(left_list, right_list)
def merge_sort(self, left_list, right_list):
sorted_list = []
left_idx, right_idx = 0, 0
while left_idx <= len(left_list) - 1 and right_idx <= len(right_list) - 1:
if left_list[left_idx] > right_list[right_idx]:
sorted_list.append(right_list[right_idx])
right_idx += 1
else:
sorted_list.append(left_list[left_idx])
left_idx += 1
sorted_list += left_list[left_idx:]
sorted_list += right_list[right_idx:]
return sorted_list | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if nums is None or len(nums) < 2:
return nums
return self.countSort(nums, len(nums))
def countSort(self, nums, n):
memo = collections.defaultdict(lambda: 0)
for num in nums:
memo[num] += 1
answer = [None] * n
currentIndex = 0
for currentNum in range(-50000, 50001):
if currentNum in memo:
while memo[currentNum] > 0:
answer[currentIndex] = currentNum
currentIndex += 1
memo[currentNum] -= 1
return answer | CLASS_DEF FUNC_DEF VAR VAR IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) == 1:
return nums
n = int(len(nums) / 2)
a1 = nums[:n]
a2 = nums[n:]
a1 = self.sortArray(a1)
a2 = self.sortArray(a2)
return self.merge(a1, a2)
def merge(self, a1, a2):
i1 = 0
i2 = 0
ret = []
while i1 < len(a1) and i2 < len(a2):
if a1[i1] < a2[i2]:
ret.append(a1[i1])
i1 += 1
else:
ret.append(a2[i2])
i2 += 1
while i1 < len(a1):
ret.append(a1[i1])
print(a1[i1])
i1 += 1
while i2 < len(a2):
ret.append(a2[i2])
print(a2[i2])
i2 += 1
return ret | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) == 1:
return nums
elif len(nums) == 2:
return nums if nums[0] <= nums[1] else [nums[1], nums[0]]
else:
half = int(len(nums) / 2)
a1 = self.sortArray(nums[:half])
a2 = self.sortArray(nums[half:])
nu = []
olen = len(a1) + len(a2)
while len(nu) < olen:
if len(a1) == 0:
nu.append(a2.pop(0))
elif len(a2) == 0:
nu.append(a1.pop(0))
elif a1[0] < a2[0]:
nu.append(a1.pop(0))
else:
nu.append(a2.pop(0))
return nu | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR NUMBER VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
res, l, N = [0] * len(nums), 1, len(nums)
def _merge(ll, lh, rl, rh):
if rl >= N:
for i in range(ll, N):
res[i] = nums[i]
else:
p, q, i = ll, rl, ll
while p < lh and q < rh:
if nums[p] <= nums[q]:
res[i], p = nums[p], p + 1
else:
res[i], q = nums[q], q + 1
i += 1
b, e = (p, lh) if p < lh else (q, rh)
for j in range(b, e):
res[i] = nums[j]
i += 1
while l < N:
b = 0
while b < N:
_merge(b, b + l, b + l, min(N, b + 2 * l))
b += 2 * l
l, nums, res = l * 2, res, nums
return nums | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | def get_numbers(arr, target, cb):
result = []
for num in arr:
if cb(num, target):
result.append(num)
return result
def is_less(a, b):
return a < b
def is_greater(a, b):
return a > b
def is_equal(a, b):
return a == b
def get_less_numbers(arr, target):
return get_numbers(arr, target, is_less)
def get_greater_numbers(arr, target):
return get_numbers(arr, target, is_greater)
def get_equal_numbers(arr, target):
return get_numbers(arr, target, is_equal)
def q_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
less = get_less_numbers(arr, pivot)
greater = get_greater_numbers(arr, pivot)
mypivot = get_equal_numbers(arr, pivot)
return q_sort(less) + mypivot + q_sort(greater)
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return q_sort(nums) | FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) <= 1:
return nums
else:
pivot = nums[int(len(nums) / 2)]
L = []
M = []
R = []
for n in nums:
if n < pivot:
L.append(n)
elif n > pivot:
R.append(n)
else:
M.append(n)
return self.sortArray(L) + M + self.sortArray(R) | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
print(nums)
if nums == []:
return []
p = nums.pop()
s = []
b = []
for i in nums:
if i > p:
b.append(i)
else:
s.append(i)
return self.sortArray(s.copy()) + [p] + self.sortArray(b.copy()) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR LIST RETURN LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def devidelist(L: List[int]):
if len(L) <= 1:
return L
mid = len(L) // 2
left = devidelist(L[0:mid])
right = devidelist(L[mid:])
return mergesort(left, right)
def mergesort(left: List[int], right: List[int]):
if not left:
return right
if not right:
return left
lidx = 0
ridx = 0
ans = []
while lidx < len(left) or ridx < len(right):
if lidx == len(left):
ans.append(right[ridx])
ridx += 1
continue
if ridx == len(right):
ans.append(left[lidx])
lidx += 1
continue
if left[lidx] <= right[ridx]:
ans.append(left[lidx])
lidx += 1
else:
ans.append(right[ridx])
ridx += 1
return ans
return devidelist(nums) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR VAR IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def sort(ls1, ls2):
i = j = 0
sortedList = []
while i < len(ls1) and j < len(ls2):
if ls1[i] < ls2[j]:
sortedList.append(ls1[i])
i += 1
else:
sortedList.append(ls2[j])
j += 1
if i < len(ls1):
sortedList += ls1[i:]
else:
sortedList += ls2[j:]
return sortedList
def divide(ls):
if len(ls) <= 1:
return ls
middle = int(len(ls) / 2)
ls1 = divide(ls[:middle])
ls2 = divide(ls[middle:])
return sort(ls1, ls2)
return divide(nums) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def _merge(self, list1, list2):
tmp = []
while list1 and list2:
(
tmp.append(list1.pop(0))
if list1[0] < list2[0]
else tmp.append(list2.pop(0))
)
return tmp + (list1 or list2)
def sortArray(self, nums: List[int]) -> List[int]:
pivot = len(nums) // 2
return (
nums
if len(nums) < 2
else self._merge(self.sortArray(nums[:pivot]), self.sortArray(nums[pivot:]))
) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR VAR EXPR VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN BIN_OP VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
self.qsort(nums, 0, len(nums))
return nums
def qsort(self, nums, begin, end):
if begin >= end:
return
x = nums[end - 1]
i = j = begin
while j < end - 1:
if nums[j] < x:
self.swap(nums, i, j)
i += 1
j += 1
self.swap(nums, i, j)
self.qsort(nums, begin, i)
self.qsort(nums, i + 1, end)
def swap(self, nums, i, j):
a = nums[i]
nums[i] = nums[j]
nums[j] = a | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
quickSort(nums, 0, len(nums) - 1)
return nums
def partition(arr, low, high):
i = low
pivot = arr[high]
for j in range(low, high):
if arr[j] < pivot:
arr[i], arr[j] = arr[j], arr[i]
i += 1
arr[i], arr[high] = arr[high], arr[i]
return i
def quickSort(arr, low, high):
if low >= high:
return
pi = partition(arr, low, high)
quickSort(arr, low, pi - 1)
quickSort(arr, pi + 1, high) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
count = [0] * 100010
for n in nums:
count[n + 50000] += 1
res = []
for c in range(100010):
while count[c] > 0:
res.append(c - 50000)
count[c] -= 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
self.mergeSort(nums)
return nums
def mergeSort(self, nums: List[int]) -> None:
if len(nums) > 1:
mid = len(nums) // 2
L, R = nums[:mid], nums[mid:]
self.mergeSort(L)
self.mergeSort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
nums[k] = L[i]
i += 1
else:
nums[k] = R[j]
j += 1
k += 1
while i < len(L):
nums[k] = L[i]
i += 1
k += 1
while j < len(R):
nums[k] = R[j]
j += 1
k += 1 | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER NONE |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, listToSort: List[int]) -> List[int]:
divider = len(listToSort) // 2
a = listToSort[:divider]
b = listToSort[divider:]
sortedList = listToSort[0 : len(listToSort)]
a.sort()
b.sort()
i = 0
j = 0
k = 0
while i < len(a) and j < len(b):
if b[j] < a[i]:
sortedList[k] = b[j]
j += 1
k += 1
else:
sortedList[k] = a[i]
i += 1
k += 1
if i < len(a):
sortedList[k:] = a[i:]
if j < len(b):
sortedList[k:] = b[j:]
listToSort[0 : len(listToSort)] = sortedList[0 : len(sortedList)]
return listToSort | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) == 1:
return nums
mid = len(nums) // 2
left = self.sortArray(nums[:mid])
right = self.sortArray(nums[mid:])
result = []
p1 = p2 = 0
while p1 < len(left) and p2 < len(right):
if left[p1] < right[p2]:
result.append(left[p1])
p1 += 1
else:
result.append(right[p2])
p2 += 1
result.extend(left[p1:])
result.extend(right[p2:])
return result | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def merge_sort(self, nums):
def helper_sort(left, right):
if left > right:
return []
if left == right:
return [nums[left]]
mid = (left + right) // 2
l = helper_sort(left, mid)
r = helper_sort(mid + 1, right)
return helper_merge(l, r)
def helper_merge(left_arr, right_arr):
l_idx = 0
r_idx = 0
ret = []
while l_idx < len(left_arr) and r_idx < len(right_arr):
if left_arr[l_idx] < right_arr[r_idx]:
ret.append(left_arr[l_idx])
l_idx += 1
else:
ret.append(right_arr[r_idx])
r_idx += 1
ret.extend(left_arr[l_idx:])
ret.extend(right_arr[r_idx:])
return ret
return helper_sort(0, len(nums) - 1)
def merge_sort_iter(self, nums):
if len(nums) <= 1:
return nums
q = deque()
for n in nums:
q.append([n])
while len(q) > 1:
size = len(q)
idx = 0
while idx < size:
l_arr = q.popleft()
idx += 1
if idx == size:
q.append(l_arr)
break
r_arr = q.popleft()
idx += 1
l_idx = 0
r_idx = 0
tmp = []
while l_idx < len(l_arr) and r_idx < len(r_arr):
if l_arr[l_idx] < r_arr[r_idx]:
tmp.append(l_arr[l_idx])
l_idx += 1
else:
tmp.append(r_arr[r_idx])
r_idx += 1
tmp.extend(l_arr[l_idx:])
tmp.extend(r_arr[r_idx:])
q.append(tmp)
return q.popleft()
def sortArray(self, nums: List[int]) -> List[int]:
return self.merge_sort_iter(nums) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN LIST IF VAR VAR RETURN LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def helper(start, end):
if start >= end:
return
l = start
r = end
mid = l + (r - l) // 2
pivot = nums[mid]
while r >= l:
while r >= l and nums[l] < pivot:
l += 1
while r >= l and nums[r] > pivot:
r -= 1
if r >= l:
nums[l], nums[r] = nums[r], nums[l]
l += 1
r -= 1
helper(start, r)
helper(l, end)
helper(0, len(nums) - 1)
return nums | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def mergeSort(nums, start, end):
if start >= end:
return
mid = start + (end - start) // 2
mergeSort(nums, start, mid)
mergeSort(nums, mid + 1, end)
L = [0] * (mid - start + 1)
R = [0] * (end - mid)
n1 = len(L)
n2 = len(R)
for i in range(n1):
L[i] = nums[start + i]
for j in range(n2):
R[j] = nums[mid + 1 + j]
i = j = 0
for _ in range(start, end + 1):
if j >= n2 or i < n1 and L[i] <= R[j]:
nums[start + i + j] = L[i]
i += 1
else:
nums[start + i + j] = R[j]
j += 1
mergeSort(nums, 0, len(nums) - 1)
return nums | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
L = len(nums)
if L == 1:
return nums
else:
left = nums[: L // 2]
right = nums[L // 2 :]
return self.compare(self.sortArray(left), self.sortArray(right))
def compare(self, left, right):
combined = []
while len(left) > 0 and len(right) > 0:
if left[0] > right[0]:
combined.append(right.pop(0))
elif left[0] < right[0]:
combined.append(left.pop(0))
else:
combined.append(right.pop(0))
combined.append(left.pop(0))
combined.extend(left)
combined.extend(right)
return combined | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
for i in range(len(nums) // 2, 0, -1):
self.heapify(nums, i, len(nums) + 1)
for i in range(len(nums), 0, -1):
index = i - 1
nums[0], nums[index] = nums[index], nums[0]
self.heapify(nums, 1, i)
return nums
def heapify(self, nums: List[int], index, length):
left = index * 2
right = left + 1
if left >= length:
return
if right >= length:
if nums[index - 1] < nums[left - 1]:
nums[index - 1], nums[left - 1] = nums[left - 1], nums[index - 1]
self.heapify(nums, left, length)
return
if nums[left - 1] < nums[right - 1]:
if nums[index - 1] < nums[right - 1]:
nums[index - 1], nums[right - 1] = nums[right - 1], nums[index - 1]
self.heapify(nums, right, length)
elif nums[index - 1] < nums[left - 1]:
nums[index - 1], nums[left - 1] = nums[left - 1], nums[index - 1]
self.heapify(nums, left, length) | CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) <= 1:
return nums
sorted_list = nums
while len(sorted_list) > 1:
x = sorted_list.pop(0)
y = sorted_list.pop(0)
sorted_list.append(self.merge(x, y))
return sorted_list[0]
def merge(self, list1, list2):
if isinstance(list1, int):
list1 = [list1]
if isinstance(list2, int):
list2 = [list2]
ret = []
list1_cursor = list2_cursor = 0
while list1_cursor < len(list1) and list2_cursor < len(list2):
if list1[list1_cursor] < list2[list2_cursor]:
ret.append(list1[list1_cursor])
list1_cursor += 1
else:
ret.append(list2[list2_cursor])
list2_cursor += 1
ret.extend(list1[list1_cursor:])
ret.extend(list2[list2_cursor:])
return ret | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
lst = []
for x in nums:
heapq.heappush(lst, x)
return [heapq.heappop(lst) for x in range(len(nums))] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
self.quickSort(nums, 0, len(nums) - 1)
return nums
def mergeSort(self, nums):
if len(nums) == 1:
return nums
mid = len(nums) // 2
left, right = self.mergeSort(nums[:mid]), self.mergeSort(nums[mid:])
return merge(left, right)
def merge(le, ri):
i, j = 0, 0
res = []
while i < len(le) and j < len(ri):
if le[i] < ri[j]:
res.append(le[i])
i += 1
else:
res.append(ri[j])
j += 1
res.append(le[i:] if j == len(ri) - 1 else ri[j:])
print(res)
return res
def quickSort(self, nums, start, end):
random.shuffle(nums)
def sort(nums, start, end):
if end <= start:
return
i, j = start, end
p = start
curNum = nums[start]
while p <= j:
if nums[p] < curNum:
nums[i], nums[p] = nums[p], nums[i]
i += 1
p += 1
elif nums[p] > curNum:
nums[p], nums[j] = nums[j], nums[p]
j -= 1
else:
p += 1
sort(nums, start, i - 1)
sort(nums, j + 1, end)
sort(nums, start, end) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def merge_sort(nums):
if len(nums) <= 1:
return nums
pivot = int(len(nums) / 2)
left = merge_sort(nums[:pivot])
right = merge_sort(nums[pivot:])
return merge(left, right)
def merge(left, right):
out = []
left_cursor = 0
right_cursor = 0
while left_cursor < len(left) and right_cursor < len(right):
if left[left_cursor] <= right[right_cursor]:
out.append(left[left_cursor])
left_cursor += 1
else:
out.append(right[right_cursor])
right_cursor += 1
out.extend(right[right_cursor:])
out.extend(left[left_cursor:])
return out
return merge_sort(nums) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if nums == [] or len(nums) == 1:
return nums
if len(nums) == 2 and nums[0] < nums[1]:
return nums
first = 0
middle = len(nums) // 2
last = len(nums) - 1
if (
nums[first] <= nums[middle] <= nums[last]
or nums[last] <= nums[middle] <= nums[first]
):
nums[first], nums[middle] = nums[middle], nums[first]
elif (
nums[first] <= nums[last] <= nums[middle]
or nums[middle] <= nums[last] <= nums[first]
):
nums[first], nums[last] = nums[last], nums[first]
pivot = 0
cur = 0
boarder = 0
for i in range(1, len(nums)):
if nums[i] > nums[pivot]:
boarder = i
break
cur = boarder + 1
while cur < len(nums):
if nums[cur] <= nums[pivot]:
nums[boarder], nums[cur] = nums[cur], nums[boarder]
boarder += 1
cur += 1
left = nums[:boarder]
right = nums[boarder:]
nums = self.sortArray(left) + self.sortArray(right)
return nums | CLASS_DEF FUNC_DEF VAR VAR IF VAR LIST FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
counts = Counter(nums)
nums_before = 0
for k, v in sorted(counts.items(), key=lambda x: x[0]):
nums_before += v
counts[k] = nums_before - 1
out = [(0) for _ in range(len(nums))]
for i in range(len(nums)):
out[counts[nums[i]]] = nums[i]
counts[nums[i]] -= 1
return out | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def mergeSort(nos):
if len(nos) > 1:
mid = len(nos) // 2
left = nos[mid:]
right = nos[:mid]
left = mergeSort(left)
right = mergeSort(right)
nos = []
while left and right:
if left[0] < right[0]:
nos.append(left[0])
left.pop(0)
else:
nos.append(right[0])
right.pop(0)
for i in left:
nos.append(i)
for j in right:
nos.append(j)
return nos
return mergeSort(nums) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | def merge(list1, list2):
merged = []
while len(list1) != 0 and len(list2) != 0:
if list1[0] < list2[0]:
merged.append(list1.pop(0))
else:
merged.append(list2.pop(0))
if len(list1) == 0:
return merged + list2
else:
return merged + list1
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) == 1:
return nums
else:
return merge(
self.sortArray(nums[: len(nums) // 2]),
self.sortArray(nums[len(nums) // 2 :]),
) | FUNC_DEF ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP VAR VAR CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def merge(left_list, right_list):
if not left_list:
return right_list
if not right_list:
return left_list
left_ptr = right_ptr = 0
out = []
while left_ptr < len(left_list) and right_ptr < len(right_list):
if left_list[left_ptr] <= right_list[right_ptr]:
out.append(left_list[left_ptr])
left_ptr += 1
else:
out.append(right_list[right_ptr])
right_ptr += 1
out.extend(left_list[left_ptr:])
out.extend(right_list[right_ptr:])
return out
new_list = [[num] for num in nums]
while len(new_list) > 1:
out = []
i = 0
for i in range(0, len(new_list), 2):
if i == len(new_list) - 1:
merged_list = merge(new_list[i], [])
else:
merged_list = merge(new_list[i], new_list[i + 1])
out.append(merged_list)
new_list = out
return new_list[0] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR LIST VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
tmp = [(0) for _ in range(len(nums))]
self.ms(nums, 0, len(nums) - 1, tmp)
return nums
def ms(self, nums, start, end, tmp):
if start >= end:
return
mid = (start + end) // 2
self.ms(nums, start, mid, tmp)
self.ms(nums, mid + 1, end, tmp)
self.merge(nums, start, mid, end, tmp)
def merge(self, nums, start, mid, end, tmp):
left, right = start, mid + 1
idx = start
while left <= mid and right <= end:
if nums[left] < nums[right]:
tmp[idx] = nums[left]
left += 1
else:
tmp[idx] = nums[right]
right += 1
idx += 1
while left <= mid:
tmp[idx] = nums[left]
left += 1
idx += 1
while right <= end:
tmp[idx] = nums[right]
right += 1
idx += 1
for i in range(start, end + 1):
nums[i] = tmp[i]
return | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def maxheapify(self, a, heapsize, i):
l, r = 2 * i + 1, 2 * i + 2
leftisgreater = rightisgreater = False
if l < heapsize and a[i] < a[l]:
leftisgreater = True
if r < heapsize and a[i] < a[r]:
rightisgreater = True
if leftisgreater and not rightisgreater:
a[i], a[l] = a[l], a[i]
self.maxheapify(a, heapsize, l)
elif not leftisgreater and rightisgreater:
a[i], a[r] = a[r], a[i]
self.maxheapify(a, heapsize, r)
elif leftisgreater and rightisgreater:
if a[l] <= a[r]:
a[i], a[r] = a[r], a[i]
self.maxheapify(a, heapsize, r)
else:
a[i], a[l] = a[l], a[i]
self.maxheapify(a, heapsize, l)
def buildmaxheap(self, nums, heapsize):
for i in reversed(range(len(nums) // 2)):
self.maxheapify(nums, heapsize, i)
def heapsort(self, nums):
heapsize = len(nums)
self.buildmaxheap(nums, heapsize)
for i in range(len(nums)):
nums[0], nums[heapsize - 1] = nums[heapsize - 1], nums[0]
heapsize -= 1
self.maxheapify(nums, heapsize, 0)
def sortArray(self, nums: List[int]) -> List[int]:
self.heapsort(nums)
return nums | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def merge(self, left, mid, right, arr):
i = left
j = mid + 1
temp = []
while i <= mid and j <= right:
if arr[i] < arr[j]:
temp.append(arr[i])
i += 1
else:
temp.append(arr[j])
j += 1
while i <= mid:
temp.append(arr[i])
i += 1
while j <= right:
temp.append(arr[j])
j += 1
j = 0
for i in range(left, right + 1):
arr[i] = temp[j]
j += 1
def mergesort(self, left, right, arr):
if left >= right:
return
else:
mid = (left + right) // 2
self.mergesort(left, mid, arr)
self.mergesort(mid + 1, right, arr)
self.merge(left, mid, right, arr)
return
def insertionSort(self, arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
def heapify(self, index, n, arr):
i = index
left = 2 * i + 1
right = 2 * i + 2
max_index = i
while left < n:
if arr[left] > arr[max_index]:
max_index = left
if right < n:
if arr[right] > arr[max_index]:
max_index = right
if max_index == index:
break
arr[max_index], arr[index] = arr[index], arr[max_index]
index = max_index
left = 2 * index + 1
right = 2 * index + 2
def heapsort(self, arr):
n = len(arr)
for i in range(0, n):
self.heapify(n - i - 1, n, arr)
for i in range(0, n):
arr[0], arr[n - i - 1] = arr[n - i - 1], arr[0]
self.heapify(0, n - i - 1, arr)
def sortArray(self, nums: List[int]) -> List[int]:
self.heapsort(nums)
return nums | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) <= 1:
return nums
mid = len(nums) // 2
left = nums[:mid]
right = nums[mid:]
left = self.sortArray(left)
right = self.sortArray(right)
res = []
while len(left) > 0 and len(right) > 0:
if left[0] < right[0]:
res.append(left.pop(0))
else:
res.append(right.pop(0))
res = res + left + right
return res | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | def swap(arr, i, j):
arr[i], arr[j] = arr[j], arr[i]
def heapify(nums, n, i):
l = 2 * i
r = 2 * i + 1
largest = i
if l < n and nums[l] > nums[largest]:
largest = l
if r < n and nums[r] > nums[largest]:
largest = r
if largest != i:
swap(nums, i, largest)
heapify(nums, n, largest)
def build_heap(nums):
n = len(nums)
for i in range(n // 2, -1, -1):
heapify(nums, n, i)
def heap_sort(nums):
n = len(nums)
for i in range(n - 1, -1, -1):
swap(nums, 0, i)
heapify(nums, i, 0)
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
build_heap(nums)
heap_sort(nums)
return nums | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def merge(l1, l2):
p1 = p2 = 0
new_list = []
while p1 < len(l1) or p2 < len(l2):
if p1 < len(l1) and p2 < len(l2):
if l1[p1] < l2[p2]:
new_list.append(l1[p1])
p1 += 1
else:
new_list.append(l2[p2])
p2 += 1
elif p1 < len(l1):
new_list += l1[p1:]
p1 = len(l1)
else:
new_list += l2[p2:]
p2 = len(l2)
return new_list
if len(nums) <= 1:
return nums
pivot = len(nums) // 2
left = self.sortArray(nums[:pivot])
right = self.sortArray(nums[pivot:])
return merge(left, right) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def mergeSort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = mergeSort(arr[:mid])
right = mergeSort(arr[mid:])
newArr = []
i = 0
j = 0
while i < len(left) or j < len(right):
if j >= len(right) or i < len(left) and left[i] <= right[j]:
newArr.append(left[i])
i += 1
elif i >= len(left) or j < len(right) and right[j] <= left[i]:
newArr.append(right[j])
j += 1
return newArr
return mergeSort(nums) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r - m
L = [0] * n1
R = [0] * n2
for i in range(0, n1):
L[i] = arr[l + i]
for j in range(0, n2):
R[j] = arr[m + 1 + j]
i = 0
j = 0
k = l
while i < n1 and j < n2:
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
def mergeSort(arr, l, r):
if l < r:
m = (l + r) // 2
mergeSort(arr, l, m)
mergeSort(arr, m + 1, r)
merge(arr, l, m, r)
return arr
class Solution:
def sortArray(self, arr: List[int]) -> List[int]:
return mergeSort(arr, 0, len(arr) - 1) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
return self.mergeSort(nums)
def mergeSort(self, nums: List[int]) -> List[int]:
if len(nums) > 1:
l1 = nums[: len(nums) // 2]
l2 = nums[len(nums) // 2 :]
L = self.mergeSort(l1)
R = self.mergeSort(l2)
sorted_list = []
i = j = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
sorted_list.append(L[i])
i += 1
else:
sorted_list.append(R[j])
j += 1
while i < len(L):
sorted_list.append(L[i])
i += 1
while j < len(R):
sorted_list.append(R[j])
j += 1
return sorted_list
return nums | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
mi = abs(min(nums))
nums = [(i + mi) for i in nums]
l = len(str(max(nums)))
res = []
for i in nums:
if len(str(i)) == l:
res.append(str(i))
continue
d = l - len(str(i))
a = "0" * d + str(i)
res.append(a)
for i in range(l - 1, -1, -1):
res = self.f(res, i)
return [(int(i) - mi) for i in res]
def f(self, res, i):
count = {str(x): [] for x in range(10)}
for j in res:
count[j[i]].append(j)
arr = []
for j in "0123456789":
if len(count[j]) == 0:
continue
for x in count[j]:
arr.append(x)
return arr | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | def max_heapify(nums, i, lo, hi):
l = 2 * i + 1
r = 2 * i + 2
largest = i
if l <= hi and nums[i] < nums[l]:
largest = l
if r <= hi and nums[largest] < nums[r]:
largest = r
if largest != i:
nums[i], nums[largest] = nums[largest], nums[i]
max_heapify(nums, largest, lo, hi)
def build_max_heap(nums):
for i in range(len(nums) // 2 - 1, -1, -1):
max_heapify(nums, i, 0, len(nums) - 1)
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
build_max_heap(nums)
for i in range(len(nums) - 1, 0, -1):
nums[0], nums[i] = nums[i], nums[0]
max_heapify(nums, 0, 0, i - 1)
return nums | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
tmp = [(0) for _ in range(len(nums))]
self.merge_sort(nums, 0, len(nums) - 1, tmp)
return nums
def merge_sort(self, nums, left, right, tmp):
if left >= right:
return
mid = (left + right) // 2
self.merge_sort(nums, left, mid, tmp)
self.merge_sort(nums, mid + 1, right, tmp)
self.merge(nums, left, right, tmp)
def merge(self, nums, left, right, tmp):
n = right - left + 1
mid = (left + right) // 2
i, j = left, mid + 1
for k in range(n):
if i <= mid and (j > right or nums[i] <= nums[j]):
tmp[k] = nums[i]
i += 1
else:
tmp[k] = nums[j]
j += 1
for k in range(n):
nums[left + k] = tmp[k] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def merge(arr1, arr2):
if not arr1:
return arr2
if not arr2:
return arr1
res = []
a1 = 0
a2 = 0
while a1 < len(arr1) or a2 < len(arr2):
if a1 == len(arr1):
res.append(arr2[a2])
a2 += 1
continue
if a2 == len(arr2):
res.append(arr1[a1])
a1 += 1
continue
if arr1[a1] < arr2[a2]:
res.append(arr1[a1])
a1 += 1
else:
res.append(arr2[a2])
a2 += 1
return res
def mergesort(arr):
if len(arr) == 1:
return arr
mid = len(arr) // 2
left = mergesort(arr[:mid])
right = mergesort(arr[mid:])
return merge(left, right)
return mergesort(nums) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def merge(left, right):
result = []
while len(left) != 0 and len(right) != 0:
l = left[0]
r = right[0]
if r < l:
result.append(right.pop(0))
else:
result.append(left.pop(0))
return result + left + right
def mergeSort(arr):
if len(arr) < 2:
return arr[:]
else:
mid = len(arr) // 2
left = mergeSort(arr[:mid])
right = mergeSort(arr[mid:])
return merge(left, right)
return mergeSort(nums) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def partition(nums, low, high):
p = nums[low]
i, j = low, low + 1
while j <= high:
if p > nums[j]:
nums[i + 1], nums[j] = nums[j], nums[i + 1]
i += 1
j += 1
nums[i], nums[low] = nums[low], nums[i]
return i
def quickSort(nums, low, high):
if low < high:
pivot_ind = partition(nums, low, high)
quickSort(nums, low, pivot_ind - 1)
quickSort(nums, pivot_ind + 1, high)
low, high = 0, len(nums) - 1
quickSort(nums, low, high)
return nums | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class node:
def __init__(self, val):
self.val = val
self.right = None
self.left = None
def insert(self, val):
if self.val is not None:
if val < self.val:
if self.left is None:
self.left = node(val)
else:
self.left.insert(val)
elif self.right is None:
self.right = node(val)
else:
self.right.insert(val)
else:
self.val = val
def inorder(root, res):
if root:
inorder(root.left, res)
res.append(root.val)
inorder(root.right, res)
class Solution:
root = None
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) > 0:
self.root = node(nums[0])
for val in nums[1:]:
self.root.insert(val)
res = []
res = []
inorder(self.root, res)
print(res)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NONE IF VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR CLASS_DEF ASSIGN VAR NONE FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
buckets = [0] * 100002
res = []
for i in nums:
buckets[i + 50000] += 1
for i in range(len(buckets)):
if buckets[i] != 0:
for _ in range(buckets[i]):
res.append(i - 50000)
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def sort(nums):
if len(nums) == 1:
return nums
mid = len(nums) // 2
left = sort(nums[:mid])
right = sort(nums[mid:])
i, j = 0, 0
res = []
while i < mid and j < len(nums) - mid:
if left[i] <= right[j]:
res.append(left[i])
i += 1
else:
res.append(right[j])
j += 1
if i < mid:
res += left[i:]
if j < len(nums) - mid:
res += right[j:]
return res
return sort(nums) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def _selection_sort(a):
for i in range(len(a)):
m = i
for j in range(i + 1, len(a)):
if a[m] > a[j]:
m = j
a[i], a[m] = a[m], a[i]
def _bubble_sort(a):
for i in range(len(a)):
for j in range(len(a) - 1 - i):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
def _insertion_sort(a):
for i in range(1, len(a)):
v = a[i]
j = i
while j > 0 and a[j - 1] > v:
a[j] = a[j - 1]
j -= 1
a[j] = v
def _quick_sort(a, s, e):
def _partition(a, s, e):
f = l = s
while l < e:
if a[l] <= a[e]:
a[f], a[l] = a[l], a[f]
f += 1
l += 1
a[f], a[e] = a[e], a[f]
return f
if s < e:
p = _partition(a, s, e)
_quick_sort(a, s, e=p - 1)
_quick_sort(a, s=p + 1, e=e)
def _merge_sort(a):
def _merge(a, b):
result = []
i = j = 0
while i < len(a) and j < len(b):
if a[i] <= b[j]:
result.append(a[i])
i += 1
else:
result.append(b[j])
j += 1
result.extend(a[i:])
result.extend(b[j:])
return result
result = []
if len(a) <= 1:
result = a.copy()
return result
m = len(a) // 2
_left = a[:m]
_right = a[m:]
_new_left = _merge_sort(_left)
_new_right = _merge_sort(_right)
result = merge(_new_left, _new_right)
return result
_quick_sort(nums, s=0, e=len(nums) - 1)
return nums | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF FUNC_DEF ASSIGN 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 IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
if len(nums) <= 1:
return nums
pivot = len(nums) // 2
left_arr = self.sortArray(nums[:pivot])
right_arr = self.sortArray(nums[pivot:])
return self.merge(left_arr, right_arr)
def merge(self, left_nums, right_nums):
m, n = len(left_nums), len(right_nums)
i, j = 0, 0
combined_arr = []
while i < m and j < n:
if left_nums[i] < right_nums[j]:
combined_arr.append(left_nums[i])
i += 1
else:
combined_arr.append(right_nums[j])
j += 1
combined_arr.extend(left_nums[i:])
combined_arr.extend(right_nums[j:])
return combined_arr | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def merge(a, b):
l1 = l2 = 0
r1, r2 = len(a), len(b)
i, j = l1, l2
out = []
while i < r1 or j < r2:
if j == r2 or i < r1 and a[i] < b[j]:
out.append(a[i])
i += 1
elif j < r2:
out.append(b[j])
j += 1
return out
skip_interval = 1
while skip_interval < len(nums):
for i in range(0, len(nums), 2 * skip_interval):
middle = i + skip_interval
nums[i : i + 2 * skip_interval] = merge(
nums[i:middle], nums[middle : middle + skip_interval]
)
skip_interval *= 2
return nums | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]):
self.qsort(nums, 0, len(nums) - 1)
return nums
def qsort(self, nums, start_idx, end_idx):
if end_idx <= start_idx:
return
correct_idx = self.partition(nums, start_idx, end_idx)
self.qsort(nums, start_idx, correct_idx - 1)
self.qsort(nums, correct_idx + 1, end_idx)
def partition(self, nums, start, end):
pivot = nums[start]
left = start + 1
right = end
while left <= right:
if nums[left] > pivot and nums[right] < pivot:
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1
if nums[left] <= pivot:
left += 1
if nums[right] >= pivot:
right -= 1
nums[start], nums[right] = nums[right], nums[start]
return right | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def merge(self, low, mid, high, arr):
left = []
right = []
l_limit = mid + 1 - low
r_limit = high + 1 - mid - 1
for i in range(low, mid + 1):
left.append(arr[i])
for i in range(mid + 1, high + 1):
right.append(arr[i])
l_iter = 0
r_iter = 0
filler = low
while l_iter < l_limit or r_iter < r_limit:
if l_iter == l_limit:
arr[filler] = right[r_iter]
r_iter += 1
elif r_iter == r_limit:
arr[filler] = left[l_iter]
l_iter += 1
elif left[l_iter] < right[r_iter]:
arr[filler] = left[l_iter]
l_iter += 1
else:
arr[filler] = right[r_iter]
r_iter += 1
filler += 1
def mergeSort(self, arr, low, high):
if low < high:
mid = low + (high - low) // 2
self.mergeSort(arr, low, mid)
self.mergeSort(arr, mid + 1, high)
self.merge(low, mid, high, arr)
def sortArray(self, arr: List[int]) -> List[int]:
n = len(arr)
if n == 1:
return arr
else:
self.mergeSort(arr, 0, n - 1)
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given an array of integers nums, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000 | class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def merge(left_array, right_array):
if not left_array:
return right_array
if not right_array:
return left_array
left_low = 0
right_low = 0
output = []
while left_low < len(left_array) or right_low < len(right_array):
if left_low < len(left_array) and right_low < len(right_array):
if left_array[left_low] < right_array[right_low]:
output.append(left_array[left_low])
left_low += 1
else:
output.append(right_array[right_low])
right_low += 1
elif left_low < len(left_array):
output.append(left_array[left_low])
left_low += 1
else:
output.append(right_array[right_low])
right_low += 1
return output
def sort(low, high):
if low == high:
return [nums[low]]
elif low > high:
return []
mid = low + (high - low) // 2
left = sort(low, mid)
right = sort(mid + 1, high)
return merge(left, right)
if not nums:
return []
return sort(0, len(nums) - 1) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN LIST VAR VAR IF VAR VAR RETURN LIST ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR IF VAR RETURN LIST RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | import sys
class Solution:
def Bsearch(self, n, h, cost):
def cost_of(val):
return sum([(abs(hi - val) * ci) for hi, ci in zip(h, cost)])
l, r = 0, max(h)
ans = sys.maxsize
while l <= r:
mid = l + r >> 1
cost_at_mid = cost_of(mid)
cost_at_lower = cost_of(mid - 1) if mid > 0 else sys.maxsize
cost_at_higher = cost_of(mid + 1)
ans = min(ans, cost_at_mid, cost_at_lower, cost_at_higher)
if cost_at_lower >= cost_at_mid:
l = mid + 1
elif cost_at_higher >= cost_at_mid:
r = mid - 1
else:
return cost_at_mid
return ans | IMPORT CLASS_DEF FUNC_DEF FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def Bsearch(self, n, h, cost):
l = 0
r = 0
for e in h:
r = max(r, e)
for e in h:
l = min(l, e)
lc = 0
pc = 0
ans = 0
while l <= r:
mid = (l + r) // 2
lc = self.total_cost(mid - 1, h, cost)
pc = self.total_cost(mid, h, cost)
if lc >= pc:
ans = pc
l = mid + 1
else:
r = mid - 1
return ans
def total_cost(self, mid, h, Cost):
total_cost = 0
for i in range(len(h)):
total_cost += abs(mid - h[i]) * Cost[i]
return total_cost | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def Bsearch(self, n, h, cost):
start = 0
end = 1000000.0
while end >= start:
mid = start + (end - start) // 2
curr = self.costofequalisation(n, h, cost, mid)
next = self.costofequalisation(n, h, cost, mid + 1)
prev = self.costofequalisation(n, h, cost, mid - 1)
if next >= curr and prev >= curr:
return int(curr)
if curr == 0:
return -1
elif curr < next:
end = mid - 1
elif curr < prev:
start = mid + 1
return -1
def costofequalisation(self, n, h, cost, equalheight):
sum = 0
for i in range(n):
sum += abs(equalheight - h[i]) * cost[i]
return sum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def Bsearch(self, n, h, cost):
res = 0
low = 0
high = max(h)
while low <= high:
mid = low + (high - low) // 2
res_x = self.getcost(h, cost, mid)
res_x_plus_one = self.getcost(h, cost, mid + 1)
res_x_minus_one = self.getcost(h, cost, mid - 1)
if res_x_minus_one >= res_x and res_x <= res_x_plus_one:
return res_x
elif res_x_minus_one < res_x:
high = mid - 1
else:
low = mid + 1
def getcost(self, h, cost, pos):
overallcost = 0
for i in range(len(h)):
overallcost += abs(h[i] - pos) * cost[i]
return overallcost | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER 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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | def solve(H, cost, mid):
ans = 0
for i in range(len(H)):
d = abs(mid - H[i])
ans += cost[i] * d
return ans
class Solution:
def Bsearch(self, N, H, cost):
l = min(H)
r = max(H)
ans = 1000000000.0
while l <= r:
mid = l + r >> 1
c = solve(H, cost, mid)
prev = solve(H, cost, mid - 1)
if c <= prev:
ans = c
l = mid + 1
else:
ans = prev
r = mid - 1
return ans | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR 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 VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def get_cost(self, new_height, h, cost):
res = 0
for hi, ci in zip(h, cost):
res += abs(hi - new_height) * ci
return res
def Bsearch(self, n, h, cost):
l, r = min(h), max(h)
min_cost = float("inf")
while r - l > 1:
mid = (l + r) // 2
bm_cost = self.get_cost(mid - 1, h, cost)
am_cost = self.get_cost(mid + 1, h, cost)
mcost = self.get_cost(mid, h, cost)
min_cost = min(min_cost, mcost)
if mcost <= bm_cost and mcost <= am_cost:
return mcost
if mcost >= am_cost:
l = mid
elif mcost >= bm_cost:
r = mid
return min(min_cost, self.get_cost(l, h, cost), self.get_cost(r, h, cost)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def totalCost(self, mid, h, cost):
totalcost = 0
for i in range(len(h)):
length = abs(mid - h[i])
totalcost += length * cost[i]
return totalcost
def Bsearch(self, n, h, cost):
low = min(h)
high = max(h)
ans = 0
while low <= high:
mid = (low + high) // 2
left = self.totalCost(mid - 1, h, cost)
m = self.totalCost(mid, h, cost)
right = self.totalCost(mid + 1, h, cost)
if m <= left and m <= right:
return m
elif m < left:
low = mid + 1
elif m < right:
high = mid - 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 RETURN NUMBER |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | def check(h, cost, m):
ans = 0
for x, c in zip(h, cost):
ans += abs(x - m) * c
return ans
class Solution:
def Bsearch(self, n, h, cost):
s, e = min(h), max(h)
while s <= e:
mid = s + (e - s) // 2
m = check(h, cost, mid)
p = check(h, cost, mid - 1)
nx = check(h, cost, mid + 1)
if m <= p and m <= nx:
return m
elif m < nx:
e = mid - 1
elif m < p:
s = mid + 1
return -1 | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR 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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR 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 NUMBER |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def Bsearch(self, n, h, cost):
if len(set(h)) == 1:
return 0
arr = [(h[i], cost[i]) for i in range(n)]
arr.sort()
summa1 = sum(abs(arr[j][0] - arr[0][0]) * arr[j][1] for j in range(n))
summa2 = sum(abs(arr[j][0] - arr[1][0]) * arr[j][1] for j in range(n))
if summa1 < summa2:
return summa1
summa1 = sum(abs(arr[j][0] - arr[-1][0]) * arr[j][1] for j in range(n))
summa2 = sum(abs(arr[j][0] - arr[-2][0]) * arr[j][1] for j in range(n))
if summa1 < summa2:
return summa1
return self.helper(0, n, arr, n)
def helper(self, low, high, arr, n):
summa1 = sum(abs(arr[j][0] - arr[low][0]) * arr[j][1] for j in range(n))
summa2 = sum(abs(arr[j][0] - arr[high - 1][0]) * arr[j][1] for j in range(n))
while high - low > 1:
i = (low + high) // 2
summa1 = sum(abs(arr[j][0] - arr[i][0]) * arr[j][1] for j in range(n))
summa2 = sum(abs(arr[j][0] - arr[i + 1][0]) * arr[j][1] for j in range(n))
if summa1 > summa2:
low = i
elif summa1 < summa2:
high = i
else:
return min(self.helper(low, i, arr, n), self.helper(i, high, arr, n))
return min(summa1, summa2) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | import sys
class Solution:
def calc(self, h, cost, value, maxi, n):
import sys
if value < 1 or value > maxi:
return sys.maxsize
ans = 0
for i in range(n):
ans += abs(h[i] - value) * cost[i]
return ans
def Bsearch(self, n, h, cost):
low = 1
maxi = max(h)
high = maxi
import sys
ans = sys.maxsize
while low <= high:
mid = low + (high - low) // 2
mid_ans = self.calc(h, cost, mid, maxi, n)
left_ans = self.calc(h, cost, mid - 1, maxi, n)
right_ans = self.calc(h, cost, mid + 1, maxi, n)
if mid_ans < left_ans and mid_ans < right_ans:
ans = min(ans, mid_ans)
return ans
elif left_ans < mid_ans:
ans = min(ans, left_ans)
high = mid - 1
else:
ans = min(ans, right_ans)
low = mid + 1
return ans | IMPORT CLASS_DEF FUNC_DEF IMPORT IF VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IMPORT ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | import sys
class Solution:
def costOfOperation(self, n, h, cost, eq_h):
c = 0
for i in range(0, n, 1):
c += abs(h[i] - eq_h) * cost[i]
return c
def Bsearch(self, n, h, cost):
max_h = h[0]
for i in range(len(h)):
if h[i] > max_h:
max_h = h[i]
ans = sys.maxsize
high = 1 + max_h
low = 0
while high > low:
mid = (low + high) // 2
if mid > 0:
bm = self.costOfOperation(n, h, cost, mid - 1)
else:
bm = sys.maxsize
m = self.costOfOperation(n, h, cost, mid)
am = self.costOfOperation(n, h, cost, mid + 1)
if ans == m:
break
ans = min(ans, m)
if bm <= m:
high = mid
elif am <= m:
low = mid + 1
else:
return m
return ans | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def cost2(self, arr, c, h):
cos = 0
for i in range(len(arr)):
cos += c[i] * abs(arr[i] - h)
return cos
def binarysearch(self, arr, c, l, r):
if l <= r:
mid = (l + r) // 2
bm = 100000000.0
am = 100000000.0
if mid > 0:
bm = self.cost2(arr, c, mid - 1)
mm = self.cost2(arr, c, mid)
am = self.cost2(arr, c, mid + 1)
if mm <= am:
return self.binarysearch(arr, c, l, mid - 1)
else:
return self.binarysearch(arr, c, mid + 1, r)
return self.cost2(arr, c, l)
def Bsearch(self, n, h, cost):
return self.binarysearch(h, cost, 0, max(h)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def Bsearch(self, n, h, cost):
lo = 0
hi = max(h)
res = -1
while lo <= hi:
m = lo + (hi - lo) // 2
def minheight(x):
min_height = x
i = 0
mycost = 0
while i < n:
if h[i] < min_height or h[i] > min_height:
mycost += cost[i] * abs(min_height - h[i])
i += 1
return mycost
y = minheight(m)
if minheight(m + 1) > y:
res = y
hi = m - 1
else:
lo = m + 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def check(self, n, h, cost, mid):
ans = 0
for i in range(n):
ans += abs(h[i] - mid) * cost[i]
return ans
def Bsearch(self, n, h, cost):
ans = 9999999999
lo = 0
hi = 1000000.0
mid = (lo + hi) // 2
while lo <= hi:
cs = self.check(n, h, cost, mid + 1)
cm = self.check(n, h, cost, mid)
ce = self.check(n, h, cost, mid - 1)
if ce >= cm and cm <= cs:
return int(cm)
elif ce < cm:
hi = mid - 1
else:
lo = mid + 1
mid = (lo + hi) // 2
ans = max(ans, cm)
return int(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given heights h[] of N towers, the task is to bring every tower to the same height by either adding or removing blocks in a tower. Every addition or removal operation costs cost[] a particular value for the respective tower. Find out the Minimum cost to Equalize the Towers.
Example 1:
Input: N = 3, h[] = {1, 2, 3}
cost[] = {10, 100, 1000}
Output: 120
Explanation: The heights can be equalized
by either "Removing one block from 3 and
adding one in 1" or "Adding two blocks in
1 and adding one in 2". Since the cost
of operation in tower 3 is 1000, the first
process would yield 1010 while the second
one yields 120. Since the second process
yields the lowest cost of operation, it is
the required output.
Example 2:
Input: N = 5, h[] = {9, 12, 18, 3, 10}
cost[] = {100, 110, 150, 25, 99}
Output: 1623
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function Bsearch() that takes integer N, array H, and array Cost as parameters and returns the minimum cost required to equalize the towers.
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def isValid(self, n, h, cost, midHeight):
totalCost = 0
for i in range(n):
totalCost += abs(h[i] - midHeight) * cost[i]
return totalCost
def Bsearch(self, n, h, cost):
start = 0
end = 10000000000.0
while start <= end:
mid = (start + end) // 2
midCost = self.isValid(n, h, cost, mid)
midCostBelow = self.isValid(n, h, cost, mid - 1)
midCostAbove = self.isValid(n, h, cost, mid + 1)
if midCostBelow >= midCost and midCost <= midCostAbove:
return int(midCost)
if midCostBelow < midCost:
end = mid - 1
elif midCostAbove < midCost:
start = mid + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | for i in range(int(input())):
n = int(input())
dict = {}
for i in range(2, n + n + 1):
dict[i] = 0
for i in range(n):
j, k = map(int, input().split())
dict[j + k] = dict[j + k] + 1
flag = 1
for i in range(2, n + 2):
if dict[i] >= i - 1:
flag = 0
x = n - 1
for i in range(n + 2, n + n + 1):
if dict[i] >= x:
flag = 0
x = x - 1
if flag == 0:
print("NO")
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | def process(N, blocks):
flag = True
for i in range(3, N + 2):
if i in blocks and blocks[i] == i - 1:
return "NO"
k = N - 1
for i in range(2 + N, 2 * N):
if i in blocks and blocks[i] == k:
return "NO"
k -= 1
return "YES"
for _ in range(int(input())):
N = int(input())
blocks = {}
for __ in range(N):
x, y = map(int, input().split())
if x + y in blocks:
blocks[x + y] += 1
else:
blocks[x + y] = 1
print("NO" if N == 2 else process(N, blocks)) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR RETURN STRING VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | t = int(input())
for _ in range(t):
n = int(input())
a = []
for __ in range(n):
x, y = map(int, input().split())
a.append([x, y])
a.sort()
row = 1
col = n
d = {}
for i in range(n):
num = a[i][1] - col
if num not in d:
d[num] = 0
d[num] += 1
row += 1
col -= 1
ans = 1
for k, v in d.items():
if abs(k) + v == n:
ans = 0
if ans:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | q = int(input())
for _ in range(q):
n = int(input())
dic = {}
ans = "YES"
for i in range(n):
x, y = map(int, input().split())
a = x + y
dic[a] = dic.get(a, 0) + 1
for k, v in dic.items():
if k - v == 1 or k + v == 1 + 2 * n:
ans = "NO"
break
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER BIN_OP NUMBER VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
obs = {}
for _ in range(n):
x, y = input().split()
x, y = int(x), int(y)
if x + y - 2 in obs:
obs[x + y - 2] += 1
else:
obs[x + y - 2] = 1
for i in obs.keys():
if i <= n - 1:
if obs[i] == i + 1:
print("NO")
break
if i > n - 1:
if obs[i] == n - 1 - (i - n):
print("NO")
break
else:
print("YES") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | for _ in range(int(input())):
n = int(input())
row = [0] * (n + 1)
for i in range(n):
x, y = list(map(int, input().split()))
row[x] = y
f = 0
strt = row[n]
for i in range(n - 1, 0, -1):
if row[i] == strt + 1:
strt += 1
if strt == n:
f = 1
break
else:
break
strt = row[1]
for i in range(2, n + 1):
if strt - 1 == row[i]:
strt -= 1
if strt == 1:
f = 1
break
else:
break
if f:
print("NO")
continue
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | for t in range(int(input())):
N = int(input())
arr = [tuple(map(int, input().split())) for i in range(N)]
mp = {}
flag = False
for i, j in arr:
if i + j in mp:
mp[i + j] += 1
else:
mp[i + j] = 1
if i + j > N + 1:
if mp[i + j] == 2 * N - i - j + 1:
flag = True
break
elif mp[i + j] == i + j - 1:
flag = True
break
if flag:
print("NO")
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | t = int(input())
for _ in range(t):
n = int(input())
obs = []
for _ in range(n):
obs.append(tuple(map(int, input().split())))
obs.sort()
ans = "YES"
check = False
for i in range(1, n):
if obs[i - 1][0] == 1 or obs[i - 1][1] == n:
check = True
if check:
if obs[i - 1][1] - obs[i][1] != 1:
check = False
if (obs[i][0] == n or obs[i][1] == 1) and check:
ans = "NO"
break
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER IF VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | from sys import setrecursionlimit, stdin
input = stdin.readline
setrecursionlimit(10**7)
def dfs1(i, j):
if i > n or j <= 0:
return 0
if pos.get((i + 1, j - 1), False):
return dfs1(i + 1, j - 1) + 1
return 1
def dfs2(i, j):
if i <= 0 or j > n:
return 0
if pos.get((i - 1, j + 1), False):
return dfs2(i - 1, j + 1) + 1
return 1
def answer():
count1 = dfs1(root1[0], root1[1])
value1 = root1[1]
count2 = dfs2(root2[0], root2[1])
value2 = n + 1 - root2[1]
if count1 == value1 or count2 == value2:
return "NO"
return "YES"
for T in range(int(input())):
n = int(input())
pos = dict()
for i in range(n):
x, y = map(int, input().split())
pos[x, y] = True
if x == 1:
root1 = [x, y]
if x == n:
root2 = [x, y]
print(answer()) | ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST VAR VAR IF VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | def solve(arr):
arr.sort(key=lambda x: x[0])
TopTOLeft = -1
BottomToRight = -1
for i in range(1, len(arr)):
if arr[i][1] == arr[i - 1][1] - 1:
TopTOLeft = i
else:
break
for i in range(len(arr) - 2, -1, -1):
if arr[i][1] == arr[i + 1][1] + 1:
BottomToRight = i
else:
break
if (
TopTOLeft != -1
and arr[TopTOLeft][1] == 1
or BottomToRight != -1
and arr[BottomToRight][1] == len(arr)
):
print("NO")
else:
print("YES")
def main():
for i in range(int(input())):
arr = []
n = int(input())
for i in range(n):
a = list(map(int, input().split()))
arr.append(a)
solve(arr)
main() | FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | def main():
T = int(input())
while T > 0:
n = int(input())
dic = {}
dic_row = {}
for i in range(n):
r, c = map(int, input().split())
dic[c] = r
dic_row[r] = c
start = dic[1]
col = 1
block = True
while start > 0:
if dic[col] != start:
block = False
break
else:
start -= 1
col += 1
if block is True:
print("NO")
else:
block = True
row = n
start = dic_row[n]
while start <= n:
if dic_row[row] != start:
block = False
break
else:
start += 1
row -= 1
if block is True:
print("NO")
else:
print("YES")
T -= 1
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
dict_ = {}
for _ in range(n):
a, b = [int(i) for i in stdin.readline().split()]
dict_[a] = b
prev = 1, dict_[1] - 1
flag = 0
for i in range(2, n + 1):
a, b = prev
prev_diff = b - a + 1
col = dict_[i]
if prev_diff == 1 and col == a:
flag = 1
break
if b <= col:
new = 1, col - 1
elif a < col < b:
new = 1, n
else:
new = col + 1, n
prev = new
if flag == 1:
print("NO")
elif prev[1] == n:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | def func3(n):
fm = [(0) for i in range(2 * n)]
for i in range(n):
x, y = map(int, input().split())
r = x + y
fm[r] += 1
l = 3
r = 2 * n - 1
c = 2
while l <= r:
if fm[l] == c or fm[r] == c:
print("NO")
return
else:
l += 1
r -= 1
c += 1
print("YES")
return
t = int(input())
for i in range(t):
n = int(input())
func3(n) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | for _ in range(int(input())):
n = int(input())
li = sorted([list(map(int, input().split())) for i in range(n)])
k = li[0][0] + li[0][1]
for i in range(n):
if sum(li[i]) != k:
break
a = li[i][1]
k = sum(li[n - 1])
for i in reversed(range(n)):
if sum(li[i]) != k:
break
b = li[i][1]
print("NO") if a == 1 or b == n else print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR VAR NUMBER VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | T = int(input())
for t in range(T):
n = int(input())
r = [0] * n
c = [0] * n
for i in range(n):
x, y = [int(u) for u in input().split()]
r[x - 1] = y - 1
c[y - 1] = x - 1
answer = True
for i in range(1, n):
if r[i] != r[i - 1] - 1:
break
if r[i] == 0:
answer = False
break
for i in range(n - 2, -1, -1):
if r[i] != r[i + 1] + 1:
break
if r[i] == n - 1:
answer = False
break
if answer == True:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
rocks = {tuple(map(int, input().split())) for _ in range(n)}
extrems = [rock for rock in rocks if rock[0] == 1 or rock[1] == n]
puc = True
for ex in extrems:
if ex in rocks:
rocks.remove(ex)
nous = [ex]
while nous:
x, y = nous.pop()
for i in range(-1, 2):
for j in range(-1, 2):
if not (i == 0 and j == 0):
nou_x, nou_y = x + i, y + j
if 1 <= nou_x <= n and 1 <= nou_y <= n:
if (nou_x, nou_y) in rocks:
if nou_x == n or nou_y == 1:
puc = False
break
nous.append((nou_x, nou_y))
rocks.remove((nou_x, nou_y))
if not puc:
break
if not puc:
break
print("YES" if puc else "NO") | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR VAR STRING STRING |
Read problem statements in [Mandarin], [Bengali], and [Russian] as well.
You are given a positive integer N. Consider a square grid of size N \times N, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. Initially you are at (1,1) and you have to reach (N,N). From a cell you can either move one cell to the right or one cell down (if possible). Formally, if you are at (i,j), then you can either move to (i+1,j) if i < N, or to (i,j+1) if j < N.
There are exactly N blocks in the grid, such that each row contains exactly one block and each column contains exactly one block. You can't move to a cell which contains a block. It is guaranteed that blocks will not placed in (1,1) and (N,N).
You have to find out whether you can reach (N,N).
------ Input Format ------
- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains N - the size of the square grid.
- The i-th line of the next N lines contains two integers X_{i} and Y_{i} indicating that (X_{i}, Y_{i}) is the position of a block in the grid.
------ Output Format ------
For each test case, if there exists a path from (1,1) to (N,N), output YES, otherwise output NO.
You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes and YES will all be treated as identical).
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤10^{6}$
$1 ≤X_{i},Y_{i} ≤N$
$(X_{i}, Y_{i}) \ne (1, 1)$ and $(X_{i}, Y_{i}) \ne (N, N)$ for all $1 ≤i ≤N$
$X_{i} \ne X_{j}$ and $Y_{i} \ne Y_{j}$ for all $1 ≤i < j ≤N$
- Sum of $N$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
2
3
1 2
2 3
3 1
2
1 2
2 1
----- Sample Output 1 ------
YES
NO
----- explanation 1 ------
- Test case $1$: We can follow the path $(1,1) \to (2,1) \to (2,2) \to (3,2) \to (3,3)$.
- Test case $2$: We can't move from the starting point, so it is impossible to reach $(N, N)$. | for _ in range(int(input())):
n = int(input())
l = []
dic = {}
a = []
for i in range(n):
l1 = list(map(int, input().split()))
dic[l1[0]] = l1[1]
if l1[1] == 1:
a = l1
fl = 1
for i in range(a[0]):
if dic[a[0] - i] != i + 1:
fl = 0
break
if fl == 1:
print("NO")
continue
fl = 1
for i in range(n - dic[n] + 1):
if dic[n - i] != dic[n] + i:
fl = 0
break
if fl == 1:
print("NO")
continue
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.