Skip to content

Commit

Permalink
Merge branch 'master' into Next-Permuation
Browse files Browse the repository at this point in the history
  • Loading branch information
sachuverma authored Oct 27, 2021
2 parents 55da1b2 + fd43fee commit d288f68
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Leetcode Top Interview Questions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [Minimum Number Of Operations to Move Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/submissions/) - [C++ Solution](./solutions/Minimum%20Number%20Of%20Operations%20To%20Move%20All%20Balls%20To%20Each%20Box.cpp)
- [Group People By Group Size](//https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/) - [C++ Solution](./solutions/Group%20The%20People%20Given%20By%20Group%20Size.cpp)
- [Next Permutation](https://leetcode.com/problems/next-permutation/) - [C++ Solution](./solutions/Next%20Permutation.cpp)
- [Sliding Window Maximum](//https://leetcode.com/problems/sliding-window-maximum/) - [C++ Solution](./solutions/Sliding%20Window%20Maximum.cpp)

### 2. Strings

Expand Down Expand Up @@ -89,6 +90,7 @@

### 1. Array

- [4Sum](https://leetcode.com/problems/4sum/) - [Cpp Solution](./solutions/4Sum.cpp)
- [3Sum](https://leetcode.com/problems/3sum/) - [Cpp Solution](./solutions/3Sum.cpp)
- [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) - [Cpp Solution](./solutions/Set%20Matrix%20Zeroes.cpp)
- [Group Anagrams](https://leetcode.com/problems/group-anagrams/) - [Cpp Solution](./solutions/Group%20Anagrams.cpp)
Expand Down
64 changes: 64 additions & 0 deletions Leetcode Top Interview Questions/solutions/4Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.
Example 1:
Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:
Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]
Constraints:
1 <= nums.length <= 200
-109 <= nums[i] <= 109
-109 <= target <= 109
*/
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n = nums.size();
vector<vector<int>> result;
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int left = j+1, right = n-1;
while(left < right)
{
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if(sum == target)
{
result.push_back({nums[i],nums[j],nums[left],nums[right]});
while(left < right-2 && nums[left+1] == nums[left])
left++;
while(left < right-2 && nums[right-1] == nums[right])
right--;
left++;
right--;
}
else if(sum < target)
left++;
else
right--;
}
while(j < n-1 && nums[j+1] == nums[j])
j++;
}
while(i < n-1 && nums[i+1] == nums[i])
i++;
}
return result;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
if (n == 0 || k == 0) return {};

vector<int> v(n-k+1);
deque<int> d;

for (int i = 0; i < n; ++i) {
while (!d.empty() && d.front() <= i-k) d.pop_front();
while (!d.empty() && nums[d.back()] < nums[i]) d.pop_back();
d.push_back(i);
if (i >= k-1) v[i+1-k] = nums[d.front()];
}
return v;
}

};
2 changes: 1 addition & 1 deletion Striver Sheet/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# STRIVER DSA SHEET

[Takeuforward Website](https://takeuforward.org/)
[Sheet Link](https://docs.google.com/document/d/1JLKCaz4n4YtYdQ1bJF1X46BKbxldedb6N1OMiXh9Dmo/edit?usp=sharing)

### Day 1 (Arrays)
Expand Down

0 comments on commit d288f68

Please sign in to comment.