-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1679_maxOperations.cpp
More file actions
31 lines (29 loc) · 974 Bytes
/
1679_maxOperations.cpp
File metadata and controls
31 lines (29 loc) · 974 Bytes
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
class Solution {
public:
int maxOperations(vector<int>& nums, int k) {
int ptrL = 0;
int ptrR = nums.size() - 1;
int cnt = 0;
// Need to sort the array, because use two pointer the Right one is the biggest num
// that can control the Left and Right to correct position
// Time complexity is O(nlogn), because use sort method
// Space complexity is O(1)
// Also can use hashmap to solve this problem
// but the space complexity will greater than two pointer,
// the time complexity will smaller than two pointer
sort(nums.begin(), nums.end());
while(ptrL < ptrR){
if(nums[ptrL] + nums[ptrR] == k){
cnt++;
ptrL++;
ptrR--;
}else if(nums[ptrL] + nums[ptrR] > k){
ptrR--;
}
else{
ptrL++;
}
}
return cnt;
}
};