-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
584 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<vector<int>> rotate(vector<vector<int>> &matrix) | ||
{ | ||
int n = matrix.size(); | ||
int m = matrix[0].size(); | ||
if (n == 0) | ||
return {}; | ||
else | ||
{ | ||
reverse(matrix.begin(), matrix.end()); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
for (int j = i + 1; j < m; j++) | ||
{ | ||
swap(matrix[i][j], matrix[j][i]); | ||
} | ||
} | ||
return matrix; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int rowWithMax1(vector<vector<int>> &matrix) | ||
{ | ||
int n = matrix.size(); | ||
int m = matrix[0].size(); | ||
if (n == 0) | ||
return 0; | ||
else | ||
{ | ||
int row = -1; | ||
int i = 0; | ||
int j = m - 1; | ||
while (i <= n && j >= 0) | ||
{ | ||
if (matrix[i][j] == 1) | ||
{ | ||
row = i; | ||
j--; | ||
} | ||
else | ||
{ | ||
i++; | ||
} | ||
} | ||
return row; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
bool searchMatrix(vector<vector<int>> &arr, int target) | ||
{ | ||
if (arr.size() == 0) | ||
return false; | ||
else | ||
{ | ||
int n = arr.size(); | ||
int m = arr[0].size(); | ||
int low = 0, high = m - 1; | ||
while (low < n && high >= 0) | ||
{ | ||
if (arr[low][high] == target) | ||
{ | ||
return true; | ||
} | ||
else if (arr[low][high] > target) | ||
{ | ||
high--; | ||
} | ||
else | ||
{ | ||
low++; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(string s, string pat) | ||
{ | ||
int n = s.length(); | ||
int i = 0; | ||
int j = 0; | ||
int k = pat.length(); | ||
int ans = 0; | ||
map<char, int> mp; | ||
for (auto j : pat) | ||
{ | ||
mp[j]++; | ||
} | ||
int count = mp.size(); | ||
while (j < n) | ||
{ | ||
// calculation | ||
if (mp.find(s[j]) != mp.end()) | ||
{ | ||
mp[s[j]]--; | ||
if (mp[s[j]] == 0) | ||
{ | ||
count--; | ||
} | ||
} | ||
|
||
// less than k | ||
if ((j - i + 1) < k) | ||
{ | ||
j++; | ||
} | ||
// equal to k | ||
else if ((j - i + 1) == k) | ||
{ | ||
if (count == 0) | ||
{ | ||
ans++; | ||
} | ||
// nullify the start | ||
if (mp.find(s[i]) != mp.end()) | ||
{ | ||
mp[s[i]]++; | ||
if (mp[s[i]] == 1) | ||
{ | ||
count++; | ||
} | ||
} | ||
i++; | ||
j++; | ||
} | ||
} | ||
return ans; | ||
} |
39 changes: 39 additions & 0 deletions
39
Sliding Window/Fixed/first_negative_number_in_window_of_size_k.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<int> solve(vector<int> nums, int k) | ||
{ | ||
int n = nums.size(); | ||
queue<int> qu; | ||
vector<int> ans; | ||
int i = 0; | ||
int j = 0; | ||
while (j < n) | ||
{ | ||
if (nums[j] < 0) | ||
{ | ||
qu.push(nums[j]); | ||
} | ||
if ((j - i + 1) < k) | ||
{ | ||
j++; | ||
} | ||
else if ((j - i + 1) == k) | ||
{ | ||
if (qu.size() == 0) | ||
{ | ||
ans.push_back(0); | ||
} | ||
else | ||
{ | ||
ans.push_back(qu.front()); | ||
if (nums[i] == qu.front()) | ||
{ | ||
qu.pop(); | ||
} | ||
} | ||
i++; | ||
j++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<int> solve(vector<int> nums, int k) | ||
{ | ||
int n = nums.size(); | ||
int i = 0; | ||
int j = 0; | ||
vector<int> ans; | ||
deque<int> list; | ||
if (k < nums.size()) | ||
{ | ||
ans.push_back(*max_element(nums.begin(), nums.end())); | ||
return ans; | ||
} | ||
while (j < n) | ||
{ | ||
while (list.size() > 0 && list.back() < nums[j]) | ||
{ | ||
list.pop_back(); | ||
} | ||
list.push_back(nums[j]); | ||
if ((j - i + 1) < k) | ||
{ | ||
j++; | ||
} | ||
else if ((j - i + 1) == k) | ||
{ | ||
ans.push_back(list.front()); | ||
if (list.front() == nums[i]) | ||
{ | ||
list.pop_front(); | ||
} | ||
i++; | ||
j++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(vector<int> nums, int k) | ||
{ | ||
int n = nums.size(); | ||
int i = 0; | ||
int j = 0; | ||
int sum = 0; | ||
int mx = INT_MIN; | ||
while (j < n) | ||
{ | ||
sum += nums[j]; | ||
if ((j - i + 1) < k) | ||
{ | ||
j++; | ||
} | ||
else if ((j - i + 1) == k) | ||
{ | ||
mx = max(mx, sum); | ||
sum -= nums[i]; | ||
i++; | ||
j++; | ||
} | ||
} | ||
return mx; | ||
} |
Oops, something went wrong.