|
43 | 43 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
44 | 44 |
|
45 | 45 | ```python
|
46 |
| - |
| 46 | +class Solution: |
| 47 | + def purchasePlans(self, nums: List[int], target: int) -> int: |
| 48 | + nums.sort() |
| 49 | + i, j = 0, len(nums) - 1 |
| 50 | + res = 0 |
| 51 | + while i < j: |
| 52 | + if nums[i] + nums[j] > target: |
| 53 | + j -= 1 |
| 54 | + else: |
| 55 | + res += (j - i) |
| 56 | + i += 1 |
| 57 | + return res % 1000000007 |
47 | 58 | ```
|
48 | 59 |
|
49 | 60 | ### **Java**
|
50 | 61 |
|
51 | 62 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
52 | 63 |
|
53 | 64 | ```java
|
| 65 | +class Solution { |
| 66 | + public int purchasePlans(int[] nums, int target) { |
| 67 | + Arrays.sort(nums); |
| 68 | + int res = 0, mod = 1000000007; |
| 69 | + for (int i = 0, j = nums.length - 1; i < j; ++i) { |
| 70 | + while (i < j && nums[i] + nums[j] > target) { |
| 71 | + --j; |
| 72 | + } |
| 73 | + if (i < j) { |
| 74 | + res = (res + j - i) % mod; |
| 75 | + } |
| 76 | + } |
| 77 | + return res; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### **C++** |
| 83 | + |
| 84 | +```cpp |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + int purchasePlans(vector<int>& nums, int target) { |
| 88 | + const int MOD = 1000000007; |
| 89 | + sort(nums.begin(), nums.end()); |
| 90 | + int res = 0; |
| 91 | + for (int i = 0, j = nums.size() - 1; i < j; ++i) |
| 92 | + { |
| 93 | + while (i < j && nums[i] + nums[j] > target) --j; |
| 94 | + if (i < j) res = (res + j - i) % MOD; |
| 95 | + } |
| 96 | + return res; |
| 97 | + } |
| 98 | +}; |
| 99 | +``` |
54 | 100 |
|
| 101 | +### **Go** |
| 102 | +
|
| 103 | +```go |
| 104 | +func purchasePlans(nums []int, target int) int { |
| 105 | + sort.Ints(nums) |
| 106 | + res, mod := 0, 1000000007 |
| 107 | + for i, j := 0, len(nums)-1; i < j; i++ { |
| 108 | + for i < j && nums[i]+nums[j] > target { |
| 109 | + j-- |
| 110 | + } |
| 111 | + if i < j { |
| 112 | + res = (res + j - i) % mod |
| 113 | + } |
| 114 | + } |
| 115 | + return res |
| 116 | +} |
55 | 117 | ```
|
56 | 118 |
|
57 | 119 | ### **...**
|
|
0 commit comments