mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
leetcode new problems
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> fourSum(vector<int>& nums, int target) {
|
||||
int n = nums.size();
|
||||
sort(nums.begin(), nums.end());
|
||||
set<vector<int>> set;
|
||||
vector<vector<int>> output;
|
||||
for(int i=0; i<n-3; i++){
|
||||
for(int j=i+1; j<n-2; j++){
|
||||
long long newTarget = (long long)target - (long long)nums[i] - (long long)nums[j];
|
||||
int low = j+1, high = n-1;
|
||||
while(low < high){
|
||||
if(nums[low] + nums[high] < newTarget){
|
||||
low++;
|
||||
}
|
||||
else if(nums[low] + nums[high] > newTarget){
|
||||
high--;
|
||||
}
|
||||
else{
|
||||
set.insert({nums[i], nums[j], nums[low], nums[high]});
|
||||
low++; high--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(auto it : set){
|
||||
output.push_back(it);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
||||
//TC : O(n^3)
|
||||
//SC : o(n)
|
||||
@@ -0,0 +1,28 @@
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> fourSum(vector<int>& nums, int target) {
|
||||
int n = nums.size();
|
||||
sort(nums.begin(), nums.end());
|
||||
set<vector<int>> set;
|
||||
vector<vector<int>> output;
|
||||
for(int i=0; i<n-3; i++){
|
||||
for(int j=i+1; j<n-2; j++){
|
||||
for(int k=j+1; k<n-1; k++){
|
||||
for(int l=k+1; l<n; l++){
|
||||
if((long long)nums[i] + (long long)nums[j] + (long long)nums[k] +
|
||||
(long long)nums[l] == target){
|
||||
set.insert({nums[i], nums[j], nums[k], nums[l]});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(auto it : set){
|
||||
output.push_back(it);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
||||
// TC : O(n^4)
|
||||
// SC : O(n)
|
||||
@@ -0,0 +1,45 @@
|
||||
class Solution{
|
||||
public:
|
||||
vector<vector<int>> foursum(vector<int> &nums, int target){
|
||||
sort(begin(nums), end(nums));
|
||||
return ksum(nums, target, 0 ,4);
|
||||
}
|
||||
vector<vector<int>> ksum(vector<int> &nums, long long target, int start, int k){
|
||||
vector<vector<int>> res;
|
||||
if(start == nums.size()) return res;
|
||||
long long avg = target/k;
|
||||
if(nums[start] > avg || nums.back()<avg) return res;
|
||||
|
||||
if(k==2) return twosum(nums, target, start);
|
||||
|
||||
for(int i = start; i<nums.size(); i++){
|
||||
if(i == start || nums[i]!= nums[i-1]){
|
||||
for(auto &subset : ksum(nums, target - nums[i], i+1, k-1)){
|
||||
res.push_back({nums[i]});
|
||||
res.back().insert(end(res.back()), begin(subset), end(subset));
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
||||
|
||||
}
|
||||
vector<vector<int>> twosum(vector<int> &nums, long long target, int start){
|
||||
vector<vector<int>> res;
|
||||
unordered_set<long long> s;
|
||||
for(int i = start; i<nums.size();i++){
|
||||
if(res.empty() || res.back()[1]!= nums[i]){
|
||||
if(s.count(target- nums[i])){
|
||||
res.push_back({int(target-nums[i]),nums[i]});
|
||||
}
|
||||
}
|
||||
s.insert(nums[i]);
|
||||
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
// TC : o(n^3) or o(n^(k-1)) in general
|
||||
// SC : O(n)
|
||||
@@ -0,0 +1,19 @@
|
||||
Should be a hard problem.
|
||||
Core Idea : Fix one number, reduce the problem size, repeat until only two numbers are left, then solve that directly
|
||||
|
||||
v.end() :
|
||||
A member function
|
||||
Exists only if v is a container class
|
||||
|
||||
end(v) :
|
||||
A free function (std::end) which internally calls v.end(), safer, modern and generic
|
||||
|
||||
Main difference:
|
||||
```
|
||||
int arr[] = {1,2,3};
|
||||
|
||||
auto a = end(arr); // works
|
||||
auto b = arr.end(); // ERROR
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user