-
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 Operations to Reduce X to Zero
- Loading branch information
Showing
4 changed files
with
59 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-operations-to-reduce-x-to-zero/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_operations_to_reduce_x_to_zero test.cpp) |
31 changes: 31 additions & 0 deletions
31
solutions/minimum-operations-to-reduce-x-to-zero/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,31 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <numeric> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
static int minOperations(const std::vector<int> &nums, int x) { | ||
const auto sum = std::accumulate(nums.begin(), nums.end(), 0); | ||
const auto length = maxSubArrayLen(nums, sum - x); | ||
return length == -1 ? -1 : nums.size() - length; | ||
} | ||
|
||
private: | ||
static int maxSubArrayLen(const std::vector<int> &nums, int target_sum) { | ||
const int n = nums.size(); | ||
int window_sum = 0; | ||
int length = -1; | ||
for (int l = 0, r = 0; r < n; ++r) { | ||
window_sum += nums[r]; | ||
while (window_sum > target_sum && l <= r) { | ||
window_sum -= nums[l++]; | ||
} | ||
if (window_sum == target_sum) { | ||
length = std::max(length, r - l + 1); | ||
} | ||
} | ||
return length; | ||
} | ||
}; |
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,26 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
{ | ||
std::vector<int> nums{1, 1, 4, 2, 3}; | ||
const int x = 5; | ||
REQUIRE(2 == Solution::minOperations(nums, x)); | ||
} | ||
{ | ||
std::vector<int> nums{5, 6, 7, 8, 9}; | ||
const int x = 4; | ||
REQUIRE(-1 == Solution::minOperations(nums, x)); | ||
} | ||
{ | ||
std::vector<int> nums{3, 2, 20, 1, 1, 3}; | ||
const int x = 10; | ||
REQUIRE(5 == Solution::minOperations(nums, x)); | ||
} | ||
{ | ||
std::vector<int> nums{1, 2, 3, 4}; | ||
const int x = 10; | ||
REQUIRE(4 == Solution::minOperations(nums, x)); | ||
} | ||
} |