-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2009-Minimum_Number_of_Operations_to_Make_Array_Continuous.cpp
More file actions
124 lines (111 loc) · 3.17 KB
/
2009-Minimum_Number_of_Operations_to_Make_Array_Continuous.cpp
File metadata and controls
124 lines (111 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************************
* 2009-Minimum_Number_of_Operations_to_Make_Array_Continuous.cpp
* Billy.Ljm
* 10 October 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/minimum-number-of-operations-to-make-array-continuous/
*
* You are given an integer array nums. In one operation, you can replace any
* element in nums with any integer.
*
* nums is considered continuous if both of the following conditions are
* fulfilled:
* - All elements in nums are unique.
* - The difference between the maximum element and the minimum element in nums
* equals nums.length - 1.
*
* For example, nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is
* not continuous.
*
* Return the minimum number of operations to make nums continuous.
*
* ===========
* My Approach
* ===========
* We can guess a minimum element (and thus maximum element) of the continuous
* array. Then, we just have to count how many element of the given array do not
* appear in the continuous array; these elements will have to be replaced. We
* have to check for all minimum elements between min(nums) and max(nums) - n.
* And to check this efficiently, we can count how many elements entered or
* exited a sliding window b/w iterations.
*
* This has a time complexity of O(n log n), and space complexity of O(n), where
* n is the size of the array
******************************************************************************/
#include <iostream>
#include <vector>
#include <set>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Solution
*/
class Solution {
public:
int minOperations(vector<int>& nums) {
int n = nums.size() - 1;
int count = 0, maxcount = 0; // count of elements in window
// sort & get unique elements
set<int> unique(nums.begin(), nums.end());
// create first sliding window of [min(nums), min(nums) + n]
auto minit = unique.begin();
auto maxit = unique.end()--;
for (auto it = unique.begin(); it != unique.end(); it++) {
if (*it <= *minit + n) count++;
else {
maxit = it;
break;
}
}
maxcount = count;
// slide window
while (maxit != unique.end()) {
// move to next element
minit++;
count--;
// update window to include new elements
while (maxit != unique.end() and *maxit <= *minit + n) {
count++;
maxit++;
}
// remember max count
maxcount = max(maxcount, count);
}
return n + 1 - maxcount;
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
vector<int> nums;
// test case 1
nums = { 4,2,5,3 };
std::cout << "minOperations(" << nums << ") = ";
std::cout << sol.minOperations(nums) << std::endl;
// test case 2
nums = { 1,2,3,5,6 };
std::cout << "minOperations(" << nums << ") = ";
std::cout << sol.minOperations(nums) << std::endl;
// test case 3
nums = { 41,33,29,33,35,26,47,24,18,28 };
std::cout << "minOperations(" << nums << ") = ";
std::cout << sol.minOperations(nums) << std::endl;
return 0;
}