var purchasePlans = function(nums, target) { let i = 0, j = nums.length - 1, ans = 0; nums.sort((a, b) => a - b); while(i < j){ if(nums[i] + nums[j] > target) j--; else { ans += j - i; i++; } } return ans % 1000000007; };
[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { publicintpurchasePlans(int[] nums, int target) { intmod=1_000_000_007; intans=0; Arrays.sort(nums); intleft=0, right = nums.length - 1; while (left < right) { if (nums[left] + nums[right] > target) right--; else { ans += right - left; left++; } ans %= mod; } return ans % mod; } }
[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intpurchasePlans(vector<int>& nums, int target){ longlong res = 0; longlong mod = 1000000007; sort(nums.begin(),nums.end()); int i=0,j = nums.size()-1; for(i=0;i<j;i++) { while(j>i&&nums[i]+nums[j]>target) { j--; } res += j-i; } return res%mod; } };
[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
intcomp(constvoid* a,constvoid* b){ return *(int*)a - *(int*)b; } intpurchasePlans(int* nums, int numsSize, int target){ longlong sum = 0; qsort(nums,numsSize,sizeof(int),comp); unsigned left = 0,right = numsSize - 1; while(left < right){ if((nums[left] + nums[right]) > target){ right--; } else{ sum += right - left; left++; } } return sum%(1000000007); }
[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
funcpurchasePlans(nums []int, target int)int { sort.Ints(nums) j := len(nums) - 1 ans := 0 for i := 0; i < len(nums); i++ { for ; j > i; j-- { if nums[i]+nums[j] <= target { break } } if j > i { ans = ans + (j - i) } } return ans % 1000000007 }
[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class Solution: def purchasePlans(self, nums: List[int], target: int) -> int: nums.sort() ans = 0 i = 0 j = len(nums)-1 while i < j: if nums[i] + nums[j] > target: j -= 1 else: ans += (j-i) i += 1 if ans<=1000000007: return ans else: return ans%1000000007