Skip to content

Commit 703ac57

Browse files
authored
Create 2294. Partition Array Such That Maximum Difference Is K (#819)
2 parents 5bb08ca + 73c4dd9 commit 703ac57

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int partitionArray(vector<int>& nums, int k) {
4+
sort(nums.begin(), nums.end()); // 1. sort
5+
int i = 0, n = nums.size(), groups = 0;
6+
7+
while (i < n) { // 3. process one group
8+
int limit = nums[i] + k; // smallest + k
9+
i = upper_bound(nums.begin() + i, // first index > limit
10+
nums.end(),
11+
limit) - nums.begin();
12+
++groups; // one group formed
13+
}
14+
return groups; // 4. answer
15+
}
16+
};

0 commit comments

Comments
 (0)