From feadd8292144ae20d5bb391d7de1a5e9c91fe0f6 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 10 May 2025 20:16:32 -0700 Subject: [PATCH 1/6] Reverse Linked List solution --- reverse-linked-list/PDKhan.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 reverse-linked-list/PDKhan.cpp diff --git a/reverse-linked-list/PDKhan.cpp b/reverse-linked-list/PDKhan.cpp new file mode 100644 index 000000000..71ff745f8 --- /dev/null +++ b/reverse-linked-list/PDKhan.cpp @@ -0,0 +1,16 @@ +class Solution { + public: + ListNode* reverseList(ListNode* head) { + ListNode* new_head = NULL; + + while(head){ + ListNode* next = head->next; + + head->next = new_head; + new_head = head; + head = next; + } + + return new_head; + } + }; From a047163e7c25a94ac8b8d907dcfa3d20797e18c8 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 10 May 2025 20:17:05 -0700 Subject: [PATCH 2/6] Longest Substring Without Repeating Characters solution --- .../PDKhan.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 longest-substring-without-repeating-characters/PDKhan.cpp diff --git a/longest-substring-without-repeating-characters/PDKhan.cpp b/longest-substring-without-repeating-characters/PDKhan.cpp new file mode 100644 index 000000000..cdfb9758e --- /dev/null +++ b/longest-substring-without-repeating-characters/PDKhan.cpp @@ -0,0 +1,20 @@ +class Solution { + public: + int lengthOfLongestSubstring(string s) { + int result = 0; + int start = 0; + unordered_map map; + + for(int end = 0; end < s.length(); end++){ + char ch = s[end]; + + if(map.count(ch) && map[ch] >= start) + start = map[ch] + 1; + + map[ch] = end; + result = max(result, end - start + 1); + } + + return result; + } + }; \ No newline at end of file From f26f58f2d7f5f6edd73fd03682748a352d90ed04 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 10 May 2025 20:17:24 -0700 Subject: [PATCH 3/6] Number of Islands solution --- number-of-islands/PDKhan.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 number-of-islands/PDKhan.cpp diff --git a/number-of-islands/PDKhan.cpp b/number-of-islands/PDKhan.cpp new file mode 100644 index 000000000..891f19ead --- /dev/null +++ b/number-of-islands/PDKhan.cpp @@ -0,0 +1,29 @@ +class Solution { + public: + void search(int r, int c, vector>& grid){ + if(r < 0 || c < 0 || r >= grid.size() || c >= grid[r].size() || grid[r][c] == '0') + return; + + grid[r][c] = '0'; + + search(r-1, c, grid); + search(r+1, c, grid); + search(r, c-1, grid); + search(r, c+1, grid); + } + + int numIslands(vector>& grid) { + int cnt = 0; + + for(int i = 0; i < grid.size(); i++){ + for(int j = 0; j < grid[i].size(); j++){ + if(grid[i][j] == '1'){ + search(i, j, grid); + cnt++; + } + } + } + + return cnt; + } + }; From fc886c4d3407e48a4e03ab4c1dcdd52545488c82 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 10 May 2025 20:17:39 -0700 Subject: [PATCH 4/6] Unique Paths solution --- unique-paths/PDKhan.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 unique-paths/PDKhan.cpp diff --git a/unique-paths/PDKhan.cpp b/unique-paths/PDKhan.cpp new file mode 100644 index 000000000..aca265348 --- /dev/null +++ b/unique-paths/PDKhan.cpp @@ -0,0 +1,22 @@ +class Solution { + public: + int uniquePaths(int m, int n) { + vector> dp(m, vector(n, 0)); + + dp[0][0] = 1; + + for(int i = 1; i < m; i++) + dp[i][0] = 1; + + for(int j = 1; j < n; j++) + dp[0][j] = 1; + + for(int i = 1; i < m; i++){ + for(int j = 1; j < n; j++){ + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + + return dp[m - 1][n - 1]; + } + }; From 5478114d79cfb819a74b1fb244e5d37608bf3956 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 10 May 2025 20:17:53 -0700 Subject: [PATCH 5/6] Set Matrix Zeroes solution --- set-matrix-zeroes/PDKhan.cpp | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 set-matrix-zeroes/PDKhan.cpp diff --git a/set-matrix-zeroes/PDKhan.cpp b/set-matrix-zeroes/PDKhan.cpp new file mode 100644 index 000000000..975b59e45 --- /dev/null +++ b/set-matrix-zeroes/PDKhan.cpp @@ -0,0 +1,46 @@ +class Solution { + public: + void setZeroes(vector>& matrix) { + int rows = matrix.size(); + int cols = matrix[0].size(); + + bool firstRowZero = false; + bool firstColZero = false; + + for(int j = 0; j < cols; j++){ + if(matrix[0][j] == 0) + firstRowZero = true; + } + + for(int i = 0; i < rows; i++){ + if(matrix[i][0] == 0) + firstColZero = true; + } + + for(int i = 1; i < rows; i++){ + for(int j = 1; j < cols; j++){ + if(matrix[i][j] == 0){ + matrix[i][0] = 0; + matrix[0][j] = 0; + } + } + } + + for(int i = 1; i < matrix.size(); i++){ + for(int j = 1; j < matrix[0].size(); j++){ + if(matrix[0][j] == 0 || matrix[i][0] == 0) + matrix[i][j] = 0; + } + } + + if(firstRowZero){ + for(int j = 0; j < cols; j++) + matrix[0][j] = 0; + } + + if(firstColZero){ + for(int i = 0; i < rows; i++) + matrix[i][0] = 0; + } + } + }; From e23d66bcb39b4981032eea13820b105ea4f6ccb9 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 10 May 2025 20:21:34 -0700 Subject: [PATCH 6/6] Add new line --- longest-substring-without-repeating-characters/PDKhan.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-substring-without-repeating-characters/PDKhan.cpp b/longest-substring-without-repeating-characters/PDKhan.cpp index cdfb9758e..b57a1719c 100644 --- a/longest-substring-without-repeating-characters/PDKhan.cpp +++ b/longest-substring-without-repeating-characters/PDKhan.cpp @@ -17,4 +17,4 @@ class Solution { return result; } - }; \ No newline at end of file + };