Skip to content

Commit

Permalink
Minimum Number of Operations to Make Array Continuous
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 10, 2023
1 parent da63696 commit 4e52d41
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ add_task(minimum-number-of-arrows-to-burst-balloons)
add_task(minimum-number-of-days-to-make-m-bouquets)
add_task(minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix)
add_task(minimum-number-of-increments-on-subarrays-to-form-a-target-array)
add_task(minimum-number-of-operations-to-make-array-continuous)
add_task(minimum-number-of-taps-to-open-to-water-a-garden)
add_task(minimum-number-of-vertices-to-reach-all-nodes)
add_task(minimum-one-bit-operations-to-make-integers-zero)
Expand Down
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)
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;
}
};
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));
}
}

0 comments on commit 4e52d41

Please sign in to comment.