forked from projectX-india/madlab-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.txt
35 lines (35 loc) · 1.59 KB
/
3Sum.txt
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
class Solution {
public:
// DYNAMIC PROGRAMMING PROBLEM
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
unordered_map<int, unordered_map<int, unordered_map<int, int>>> dp;
vector<vector<int>> ans;
for(int i = 0; i < nums.size(); i++) {
int target = -1 * nums[i];
int start = i + 1;
int end = nums.size() - 1;
while(start < end) {
if(nums[start] + nums[end] == target) {
if(dp[nums[i]][nums[start]][nums[end]] != 1 && dp[nums[start]][nums[i]][nums[end]] != 1 && dp[nums[start]][nums[end]][nums[i]] != 1 && dp[nums[i]][nums[end]][nums[start]] != 1 && dp[nums[end]][nums[start]][nums[i]] != 1 && dp[nums[i]][nums[end]][nums[start]] != 1) {
vector<int> v = {nums[i], nums[start], nums[end]};
dp[nums[i]][nums[start]][nums[end]] = 1;
dp[nums[start]][nums[i]][nums[end]] = 1;
dp[nums[start]][nums[end]][nums[i]] = 1;
dp[nums[i]][nums[end]][nums[start]] = 1;
dp[nums[end]][nums[start]][nums[i]] = 1;
dp[nums[i]][nums[end]][nums[start]] = 1;
ans.push_back(v);
}
start++;
end--;
} else if(nums[start] + nums[end] < target) {
start++;
} else {
end--;
}
}
}
return ans;
}
};