Given an integer array nums, return an integer array counts where counts[i] is the number of smaller elements to the right of nums[i].
Example 1:
Input: nums = [5,2,6,1] Output: [2,1,1,0] Explanation: To the right of 5 there are 2 smaller elements (2 and 1). To the right of 2 there is only 1 smaller element (1). To the right of 6 there is 1 smaller element (1). To the right of 1 there is 0 smaller element.
classSolution { public List<Integer> countSmaller(int[] nums) { intn= nums.length; int[] src = newint[n]; // index of nums for (inti=1; i < n; i++) { src[i] = i; } int[] count = newint[n], aux = Arrays.copyOf(src, n); mergeSort(nums, aux, src, 0, n-1, count); List<Integer> res = newArrayList<>(); for (int num: count) { res.add(num); } return res; }
privatevoidmergeSort(int[] nums, int[] src, int[] dst, int l, int r, int[] count) { if (l >= r) return; intm= l + ((r - l) >> 1); mergeSort(nums, dst, src, l, m, count); mergeSort(nums, dst, src, m+1, r, count); if (nums[src[m]] <= nums[src[m+1]]) { System.arraycopy(src, l, dst, l, r-l+1); return; } merge(nums, src, dst, l, m, r, count); }
privatevoidmerge(int[] nums, int[] src, int[] dst, int l, int m, int r, int[] count) { inti= l, j = m + 1, cnt = 0; for (intk= l; k <= r; k++) { if (i > m) { dst[k] = src[j++]; } elseif (j > r || nums[src[i]] <= nums[src[j]]) { count[src[i]] += cnt; // all numbers in right subarry which retrieved before nums[i] dst[k] = src[i++]; } else { cnt++; // nums[j] < nums[i] dst[k] = src[j++]; } } } }
2426. Number of Pairs Satisfying Inequality
You are given two 0-indexed integer arrays nums1 and nums2, each of size n, and an integer diff. Find the number of pairs(i, j) such that:
0 <= i < j <= n - 1
nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff
Return the number of pairs that satisfy the conditions.
Example 1:
Input: nums1 = [3,2,5], nums2 = [2,2,1], diff = 1 Output: 3 Explanation: There are 3 pairs that satisfy the conditions: 1. i = 0, j = 1: 3 - 2 <= 2 - 2 + 1. Since i < j and 1 <= 1, this pair satisfies the conditions. 2. i = 0, j = 2: 3 - 5 <= 2 - 1 + 1. Since i < j and -2 <= 2, this pair satisfies the conditions. 3. i = 1, j = 2: 2 - 5 <= 2 - 1 + 1. Since i < j and -3 <= 2, this pair satisfies the conditions. Therefore, we return 3.
privatelongmergeSort(int[] nums, int[] aux, int l, int r, int diff) { if (l >= r) return0; intm= l + ((r - l) >> 1); return mergeSort(nums, aux, l, m, diff) + mergeSort(nums, aux, m+1, r, diff) + merge(nums, aux, l, m, r, diff); }
privatelongmerge(int[] nums, int[] aux, int l, int m, int r, int diff) { longcnt=0; for (intk= l, i = l, j = m + 1; k <= r; k++) { if (i > m) { aux[k] = nums[j++]; } elseif (j > r || nums[i] <= nums[j]) { intt= binarySearch(nums, m+1, r, nums[i] - diff); cnt += r - t + 1; aux[k] = nums[i++]; } else { aux[k] = nums[j++]; } } System.arraycopy(aux, l, nums, l, r - l + 1); return cnt; }
privateintbinarySearch(int[] nums, int l, int r, int target) { while (l <= r) { intm= l + ((r - l) >> 1); if (nums[m] < target) { l = m + 1; } else { r = m - 1; } } return l; } }
327. Count of Range Sum
Given an integer array nums and two integers lower and upper, return the number of range sums that lie in [lower, upper] inclusive.
Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j inclusive, where i <= j.
Example 1:
Input: nums = [-2,5,-1], lower = -2, upper = 2 Output: 3 Explanation: The three ranges are: [0,0], [2,2], and [0,2] and their respective sums are: -2, -1, 2.
classSolution { publicintcountRangeSum(int[] nums, int lower, int upper) { intn= nums.length; long[] arr = newlong[n]; for (inti=0; i < n; i++) { // presum arr[i] = nums[i] + (i > 0 ? arr[i-1] : 0); } return mergeSort(arr, Arrays.copyOf(arr, n), 0, n-1, lower, upper); }
privateintmergeSort(long[] nums, long[] aux, int l, int r, int lo, int up) { if (l == r) { return nums[l] > up || nums[l] < lo ? 0 : 1; } intm= l + ((r - l) >> 1); return mergeSort(nums, aux, l, m, lo, up) + mergeSort(nums, aux, m+1, r, lo, up) + merge(nums, aux, l, m, r, lo, up); }
privateintmerge(long[] nums, long[] aux, int l, int m, int r, int lo, int up) { intcnt=0; for (intk= l, i = l, j = m + 1; k <= r; k++) { if (i > m) { aux[k] = nums[j++]; } elseif (j > r || nums[i] <= nums[j]) { intr1= floor(nums, m+1, r, nums[i]+lo), r2 = ceil(nums, m+1, r, nums[i]+up); cnt += r2 - r1 + 1; aux[k] = nums[i++]; } else { aux[k] = nums[j++]; } } System.arraycopy(aux, l, nums, l, r - l + 1); return cnt; }
privateintceil(long[] nums, int l, int r, long upper) { while (l <= r) { intm= l + ((r - l) >> 1); if (nums[m] > upper) { r = m - 1; } else { l = m + 1; } } return r; }
privateintfloor(long[] nums, int l, int r, long lower) { while (l <= r) { intm= l + ((r - l) >> 1); if (nums[m] < lower) { l = m + 1; } else { r = m - 1; } } return l; } }