-
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.
Minimum Number of Operations to Make Array Continuous
- Loading branch information
Showing
4 changed files
with
41 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/minimum-number-of-operations-to-make-array-continuous/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_minimum_number_of_operations_to_make_array_continuous test.cpp) |
21 changes: 21 additions & 0 deletions
21
solutions/minimum-number-of-operations-to-make-array-continuous/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,21 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
static int minOperations(std::vector<int> nums) { | ||
const auto n = nums.size(); | ||
|
||
std::sort(nums.begin(), nums.end()); | ||
nums.erase(std::unique(nums.begin(), nums.end()), nums.end()); | ||
|
||
auto ans = n - 1; | ||
for (auto it = nums.begin(); it != nums.end(); ++it) { | ||
auto ub = std::upper_bound(it, nums.end(), *it + n - 1); | ||
ans = std::min(ans, n - std::distance(it, ub)); | ||
} | ||
return ans; | ||
} | ||
}; |
18 changes: 18 additions & 0 deletions
18
solutions/minimum-number-of-operations-to-make-array-continuous/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,18 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
{ | ||
std::vector<int> nums{4, 2, 5, 3}; | ||
REQUIRE(0 == Solution::minOperations(nums)); | ||
} | ||
{ | ||
std::vector<int> nums{1, 2, 3, 5, 6}; | ||
REQUIRE(1 == Solution::minOperations(nums)); | ||
} | ||
{ | ||
std::vector<int> nums{1, 10, 100, 1000}; | ||
REQUIRE(3 == Solution::minOperations(nums)); | ||
} | ||
} |