Skip to content

[PDKhan] WEEK 09 solutions #1527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions linked-list-cycle/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;

while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;

if(slow == fast)
return true;
}

return false;
}
};
19 changes: 19 additions & 0 deletions maximum-product-subarray/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int maxProduct(vector<int>& nums) {
int result = nums[0];
int curr_max = nums[0];
int curr_min = nums[0];

for(int i = 1; i < nums.size(); i++){
int tmp_max = curr_max;

curr_max = max(nums[i], max(curr_max * nums[i], curr_min * nums[i]));
curr_min = min(nums[i], min(tmp_max * nums[i], curr_min * nums[i]));

result = max(result, curr_max);
}

return result;
}
};
44 changes: 44 additions & 0 deletions minimum-window-substring/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char, int> t_map;
unordered_map<char, int> window;
int need = 0, match = 0;
int min_len = INT_MAX;
int min_low = 0;
int l = 0;

for(char ch : t){
if(t_map[ch] == 0)
need++;

t_map[ch]++;
}

for(int h = 0; h < s.length(); h++){
window[s[h]]++;

if(window[s[h]] == t_map[s[h]])
match++;

while(need == match){
if(h - l + 1 < min_len){
min_len = h - l + 1;
min_low = l;
}

window[s[l]]--;

if(window[s[l]] < t_map[s[l]])
match--;

l++;
}
}

if(min_len == INT_MAX)
return "";

return s.substr(min_low, min_len);
}
};
40 changes: 40 additions & 0 deletions pacific-atlantic-water-flow/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public:
void dfs(int prev, int r, int c, vector<vector<int>>& heights, vector<vector<bool>>& visit){
if(r < 0 || c < 0 || r >= heights.size() || c >= heights[0].size() || visit[r][c] || heights[r][c] < prev)
return;

visit[r][c] = true;

dfs(heights[r][c], r - 1, c, heights, visit);
dfs(heights[r][c], r + 1, c, heights, visit);
dfs(heights[r][c], r, c - 1, heights, visit);
dfs(heights[r][c], r, c + 1, heights, visit);
}

vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
vector<vector<int>> result;
vector<vector<bool>> pacific(heights.size(), vector(heights[0].size(), false));
vector<vector<bool>> atlantic(heights.size(), vector(heights[0].size(), false));

for(int i = 0; i < heights.size(); i++){
dfs(heights[i][0], i, 0, heights, pacific);
dfs(heights[i][heights[0].size()-1], i, heights[0].size()-1, heights, atlantic);
}

for(int j = 0; j < heights[0].size(); j++){
dfs(heights[0][j], 0, j, heights, pacific);
dfs(heights[heights.size()-1][j], heights.size()-1, j, heights, atlantic);
}

for(int i = 0; i < heights.size(); i++){
for(int j = 0; j < heights[0].size(); j++){
if(pacific[i][j] && atlantic[i][j]){
result.push_back({i, j});
}
}
}

return result;
}
};
29 changes: 29 additions & 0 deletions sum-of-two-integers/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
int getSum(int a, int b) {
unsigned int result = 0;
int carry = 0;

for(int i = 0; i < 32; i++){
unsigned int aa = a & (1U << i);
unsigned int bb = b & (1U << i);

if(aa && bb){
if(carry)
result |= (1U << i);

carry = 1;
}else if(aa == 0 && bb == 0){
if(carry)
result |= (1U << i);

carry = 0;
}else{
if(carry == 0)
result |= (1U << i);
}
}

return result;
}
};