Skip to content

Commit 84105a9

Browse files
authored
Merge pull request #1527 from PDKhan/main
[PDKhan] WEEK 09 solutions
2 parents b144d5d + 461ba12 commit 84105a9

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

linked-list-cycle/PDKhan.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool hasCycle(ListNode *head) {
4+
ListNode* slow = head;
5+
ListNode* fast = head;
6+
7+
while(fast && fast->next){
8+
slow = slow->next;
9+
fast = fast->next->next;
10+
11+
if(slow == fast)
12+
return true;
13+
}
14+
15+
return false;
16+
}
17+
};

maximum-product-subarray/PDKhan.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int maxProduct(vector<int>& nums) {
4+
int result = nums[0];
5+
int curr_max = nums[0];
6+
int curr_min = nums[0];
7+
8+
for(int i = 1; i < nums.size(); i++){
9+
int tmp_max = curr_max;
10+
11+
curr_max = max(nums[i], max(curr_max * nums[i], curr_min * nums[i]));
12+
curr_min = min(nums[i], min(tmp_max * nums[i], curr_min * nums[i]));
13+
14+
result = max(result, curr_max);
15+
}
16+
17+
return result;
18+
}
19+
};

minimum-window-substring/PDKhan.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public:
3+
string minWindow(string s, string t) {
4+
unordered_map<char, int> t_map;
5+
unordered_map<char, int> window;
6+
int need = 0, match = 0;
7+
int min_len = INT_MAX;
8+
int min_low = 0;
9+
int l = 0;
10+
11+
for(char ch : t){
12+
if(t_map[ch] == 0)
13+
need++;
14+
15+
t_map[ch]++;
16+
}
17+
18+
for(int h = 0; h < s.length(); h++){
19+
window[s[h]]++;
20+
21+
if(window[s[h]] == t_map[s[h]])
22+
match++;
23+
24+
while(need == match){
25+
if(h - l + 1 < min_len){
26+
min_len = h - l + 1;
27+
min_low = l;
28+
}
29+
30+
window[s[l]]--;
31+
32+
if(window[s[l]] < t_map[s[l]])
33+
match--;
34+
35+
l++;
36+
}
37+
}
38+
39+
if(min_len == INT_MAX)
40+
return "";
41+
42+
return s.substr(min_low, min_len);
43+
}
44+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
void dfs(int prev, int r, int c, vector<vector<int>>& heights, vector<vector<bool>>& visit){
4+
if(r < 0 || c < 0 || r >= heights.size() || c >= heights[0].size() || visit[r][c] || heights[r][c] < prev)
5+
return;
6+
7+
visit[r][c] = true;
8+
9+
dfs(heights[r][c], r - 1, c, heights, visit);
10+
dfs(heights[r][c], r + 1, c, heights, visit);
11+
dfs(heights[r][c], r, c - 1, heights, visit);
12+
dfs(heights[r][c], r, c + 1, heights, visit);
13+
}
14+
15+
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
16+
vector<vector<int>> result;
17+
vector<vector<bool>> pacific(heights.size(), vector(heights[0].size(), false));
18+
vector<vector<bool>> atlantic(heights.size(), vector(heights[0].size(), false));
19+
20+
for(int i = 0; i < heights.size(); i++){
21+
dfs(heights[i][0], i, 0, heights, pacific);
22+
dfs(heights[i][heights[0].size()-1], i, heights[0].size()-1, heights, atlantic);
23+
}
24+
25+
for(int j = 0; j < heights[0].size(); j++){
26+
dfs(heights[0][j], 0, j, heights, pacific);
27+
dfs(heights[heights.size()-1][j], heights.size()-1, j, heights, atlantic);
28+
}
29+
30+
for(int i = 0; i < heights.size(); i++){
31+
for(int j = 0; j < heights[0].size(); j++){
32+
if(pacific[i][j] && atlantic[i][j]){
33+
result.push_back({i, j});
34+
}
35+
}
36+
}
37+
38+
return result;
39+
}
40+
};

sum-of-two-integers/PDKhan.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int getSum(int a, int b) {
4+
unsigned int result = 0;
5+
int carry = 0;
6+
7+
for(int i = 0; i < 32; i++){
8+
unsigned int aa = a & (1U << i);
9+
unsigned int bb = b & (1U << i);
10+
11+
if(aa && bb){
12+
if(carry)
13+
result |= (1U << i);
14+
15+
carry = 1;
16+
}else if(aa == 0 && bb == 0){
17+
if(carry)
18+
result |= (1U << i);
19+
20+
carry = 0;
21+
}else{
22+
if(carry == 0)
23+
result |= (1U << i);
24+
}
25+
}
26+
27+
return result;
28+
}
29+
};

0 commit comments

Comments
 (0)