From cc59335189301a36725705cb37c89e478fd0d50a Mon Sep 17 00:00:00 2001 From: PDKhan Date: Mon, 26 May 2025 15:33:18 -0700 Subject: [PATCH 1/5] Linked List Cycle solution --- linked-list-cycle/PDKhan.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 linked-list-cycle/PDKhan.cpp diff --git a/linked-list-cycle/PDKhan.cpp b/linked-list-cycle/PDKhan.cpp new file mode 100644 index 000000000..f458bdc5d --- /dev/null +++ b/linked-list-cycle/PDKhan.cpp @@ -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; + } + }; From fa8af06bc841d0c443631a2adde8d2f7eecb0e27 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Mon, 26 May 2025 15:33:35 -0700 Subject: [PATCH 2/5] Pacific Atlantic Water Flow solution --- pacific-atlantic-water-flow/PDKhan.cpp | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 pacific-atlantic-water-flow/PDKhan.cpp diff --git a/pacific-atlantic-water-flow/PDKhan.cpp b/pacific-atlantic-water-flow/PDKhan.cpp new file mode 100644 index 000000000..e12c5cf10 --- /dev/null +++ b/pacific-atlantic-water-flow/PDKhan.cpp @@ -0,0 +1,40 @@ +class Solution { + public: + void dfs(int prev, int r, int c, vector>& heights, vector>& 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> pacificAtlantic(vector>& heights) { + vector> result; + vector> pacific(heights.size(), vector(heights[0].size(), false)); + vector> 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; + } + }; From 5cd52d6888b0b226a9e797bcb079c19571de0534 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Mon, 26 May 2025 15:33:53 -0700 Subject: [PATCH 3/5] Maximum Product Subarray solution --- maximum-product-subarray/PDKhan.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 maximum-product-subarray/PDKhan.cpp diff --git a/maximum-product-subarray/PDKhan.cpp b/maximum-product-subarray/PDKhan.cpp new file mode 100644 index 000000000..a5848ac8c --- /dev/null +++ b/maximum-product-subarray/PDKhan.cpp @@ -0,0 +1,19 @@ +class Solution { + public: + int maxProduct(vector& 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; + } + }; From 05a6e0c1e2d0f7fa7bdd46010c347cb788439fd2 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Mon, 26 May 2025 15:34:06 -0700 Subject: [PATCH 4/5] Sum of Two Integers solution --- sum-of-two-integers/PDKhan.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 sum-of-two-integers/PDKhan.cpp diff --git a/sum-of-two-integers/PDKhan.cpp b/sum-of-two-integers/PDKhan.cpp new file mode 100644 index 000000000..63cfca7b6 --- /dev/null +++ b/sum-of-two-integers/PDKhan.cpp @@ -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; + } + }; From 461ba127fc0a0d5ffd1b7f7ed7580e34848fddfd Mon Sep 17 00:00:00 2001 From: PDKhan Date: Mon, 26 May 2025 15:34:25 -0700 Subject: [PATCH 5/5] Minimum Window Substring solution --- minimum-window-substring/PDKhan.cpp | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 minimum-window-substring/PDKhan.cpp diff --git a/minimum-window-substring/PDKhan.cpp b/minimum-window-substring/PDKhan.cpp new file mode 100644 index 000000000..6f7132848 --- /dev/null +++ b/minimum-window-substring/PDKhan.cpp @@ -0,0 +1,44 @@ +class Solution { + public: + string minWindow(string s, string t) { + unordered_map t_map; + unordered_map 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); + } + };