-
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.
Minimize the Maximum Difference of Pairs
- Loading branch information
Showing
4 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
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
1 change: 1 addition & 0 deletions
1
solutions/minimize-the-maximum-difference-of-pairs/CMakeLists.txt
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 @@ | ||
add_catch(test_minimize_the_maximum_difference_of_pairs test.cpp) |
28 changes: 28 additions & 0 deletions
28
solutions/minimize-the-maximum-difference-of-pairs/solution.hpp
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,28 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
int minimizeMax(std::vector<int> nums, int p) { | ||
std::sort(nums.begin(), nums.end()); | ||
int left = 0, right = nums.back() - nums.front(); | ||
while (left < right) { | ||
const auto middle = left + (right - left) / 2; | ||
countValidPairs(nums, middle) >= p ? right = middle : left = middle + 1; | ||
} | ||
return left; | ||
} | ||
|
||
private: | ||
int countValidPairs(const std::vector<int> nums, int threshold) { | ||
int cnt = 0; | ||
for (size_t i = 0; i + 1 < nums.size(); ++i) { | ||
if (nums[i + 1] - nums[i] <= threshold) { | ||
++cnt, ++i; | ||
} | ||
} | ||
return cnt; | ||
} | ||
}; |
21 changes: 21 additions & 0 deletions
21
solutions/minimize-the-maximum-difference-of-pairs/test.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,21 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
{ | ||
std::vector<int> nums{10, 1, 2, 7, 1, 3}; | ||
int p = 2; | ||
REQUIRE(1 == Solution().minimizeMax(nums, p)); | ||
} | ||
{ | ||
std::vector<int> nums{4, 2, 1, 2}; | ||
int p = 1; | ||
REQUIRE(0 == Solution().minimizeMax(nums, p)); | ||
} | ||
{ | ||
std::vector<int> nums{50}; | ||
int p = 0; | ||
REQUIRE(0 == Solution().minimizeMax(nums, p)); | ||
} | ||
} |