diff options
Diffstat (limited to 'other/min_num_opp_arr_cont.cpp')
-rw-r--r-- | other/min_num_opp_arr_cont.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/other/min_num_opp_arr_cont.cpp b/other/min_num_opp_arr_cont.cpp new file mode 100644 index 0000000..5dcb40f --- /dev/null +++ b/other/min_num_opp_arr_cont.cpp @@ -0,0 +1,22 @@ +#include "leetcode.h" + +class Solution { +public: + int minOperations(vector<int> &nums) { + int n = nums.size(), ans = n, j = 0; + sort(nums.begin(), nums.end()); + nums.erase(unique(nums.begin(), nums.end()), nums.end()); + for (int i = 0; i < nums.size(); ++i) { + while (j < nums.size() && nums[j] < nums[i] + n) + ++j; + ans = min(ans, n - j + i); + } + return ans; + } +}; + +int main() { + Solution obj; + vector<int> nums = {4, 2, 5, 3}; + cout << obj.minOperations(nums); +} |