-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e89584
commit e83e558
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
C++/2294-PartitionArraySuchThatMaximumDifferenceIsK/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <algorithm> | ||
#include <cstddef> | ||
#include <cstdlib> | ||
#include <vector> | ||
|
||
using namespace std; | ||
class Solution { | ||
public: | ||
int partitionArray(std::vector<int>& nums, int k) { | ||
// Reduce the problem, again! The question may seem like it wants to | ||
// disallow sorting, by mentioning the definition of a subsequence. | ||
// But notice that the order of elements within each subsequence do not | ||
// matter. We only want the minimum and maximum of each subsequence such | ||
// that max - min <= k. | ||
// Therefore, sorting allows us to reduce this problem into a sliding window | ||
// question (a little modified, since we do not want all subarrays, but the | ||
// minimum number of subarrays), where the leftmost and rightmost of the | ||
// window differ by at most k. | ||
if (nums.empty()) { | ||
return 0; | ||
} | ||
|
||
std::sort(nums.begin(), nums.end()); | ||
|
||
// start with a singular partition => the entire array. | ||
int partitions = 1; | ||
int l = 0; | ||
for (int r = 1; r < nums.size(); ++r) { | ||
// try to move r rightwards until the condition nums[r] - nums[l] <= k | ||
// is violated. Then we know that the elements in [l..r-1] belong to its | ||
// partition. | ||
if (nums[r] - nums[l] > k) { | ||
++partitions; | ||
l = r; | ||
} | ||
} | ||
|
||
return partitions; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters