From 40fd08c0783de7636005b0614311697ad6b7d9fc Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 19 Apr 2025 22:09:38 -0700 Subject: [PATCH 1/5] Merge Two Sorted Lists solution --- merge-two-sorted-lists/PDKhan.cpp | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 merge-two-sorted-lists/PDKhan.cpp diff --git a/merge-two-sorted-lists/PDKhan.cpp b/merge-two-sorted-lists/PDKhan.cpp new file mode 100644 index 000000000..e737d2e43 --- /dev/null +++ b/merge-two-sorted-lists/PDKhan.cpp @@ -0,0 +1,37 @@ +class Solution { + public: + ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { + ListNode* new_head = NULL; + ListNode* tail; + + while(list1 || list2){ + ListNode* curr; + + if(list1 && list2){ + if(list1->val < list2->val){ + curr = list1; + list1 = list1->next; + }else{ + curr = list2; + list2 = list2->next; + } + }else if(list1){ + curr = list1; + list1 = list1->next; + }else{ + curr = list2; + list2 = list2->next; + } + + if(new_head == NULL){ + new_head = curr; + tail = new_head; + }else{ + tail->next = curr; + tail = tail->next; + } + } + + return new_head; + } + }; From cbaca32bcb702ea21a4e82324ed302f91a61bb54 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 19 Apr 2025 22:10:03 -0700 Subject: [PATCH 2/5] Maximum Depth of Binary Tree solution --- maximum-depth-of-binary-tree/PDKhan.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 maximum-depth-of-binary-tree/PDKhan.cpp diff --git a/maximum-depth-of-binary-tree/PDKhan.cpp b/maximum-depth-of-binary-tree/PDKhan.cpp new file mode 100644 index 000000000..0854b8365 --- /dev/null +++ b/maximum-depth-of-binary-tree/PDKhan.cpp @@ -0,0 +1,9 @@ +class Solution { + public: + int maxDepth(TreeNode* root) { + if(root == nullptr) + return 0; + + return 1 + max(maxDepth(root->left), maxDepth(root->right)); + } + }; From 7abb7b4c07f47c00a6989f444ba37107526670a6 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 19 Apr 2025 22:10:26 -0700 Subject: [PATCH 3/5] Find Minimum In Rotated Sorted Array solution --- .../PDKhan.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/PDKhan.cpp diff --git a/find-minimum-in-rotated-sorted-array/PDKhan.cpp b/find-minimum-in-rotated-sorted-array/PDKhan.cpp new file mode 100644 index 000000000..45d3a373b --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/PDKhan.cpp @@ -0,0 +1,18 @@ +class Solution { + public: + int findMin(vector& nums) { + int left = 0; + int right = nums.size() - 1; + + while(left < right){ + int mid = (left + right) / 2; + + if(nums[mid] > nums[right]) + left = mid + 1; + else + right = mid; + } + + return nums[left]; + } + }; From 03c962074b46208b536ba55fc13dcbc326d7fea1 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 19 Apr 2025 22:10:40 -0700 Subject: [PATCH 4/5] Word Search solution --- word-search/PDKhan.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 word-search/PDKhan.cpp diff --git a/word-search/PDKhan.cpp b/word-search/PDKhan.cpp new file mode 100644 index 000000000..70ec97c7f --- /dev/null +++ b/word-search/PDKhan.cpp @@ -0,0 +1,43 @@ +class Solution { + public: + bool search(int r, int c, int index, vector>& board, string word){ + if(index == word.length()) + return true; + + if(r < 0 || r >= board.size() || c < 0 || c >= board[0].size() || board[r][c] != word[index]) + return false; + + char curr = board[r][c]; + + board[r][c] = '0'; + + if(search(r + 1, c, index + 1, board, word) == true) + return true; + + if(search(r - 1, c, index + 1, board, word) == true) + return true; + + if(search(r, c + 1, index + 1, board, word) == true) + return true; + + if(search(r, c - 1, index + 1, board, word) == true) + return true; + + board[r][c] = curr; + + return false; + } + + bool exist(vector>& board, string word) { + for(int i = 0; i < board.size(); i++){ + for(int j = 0; j < board[i].size(); j++){ + if(board[i][j] == word[0]){ + if(search(i, j, 0, board, word) == true) + return true; + } + } + } + + return false; + } + }; From f96fa500136502fb03bf5d604da8e43c942c2f71 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sat, 19 Apr 2025 22:10:51 -0700 Subject: [PATCH 5/5] Coin Chage solution --- coin-change/PDKhan.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 coin-change/PDKhan.cpp diff --git a/coin-change/PDKhan.cpp b/coin-change/PDKhan.cpp new file mode 100644 index 000000000..33dfdfa62 --- /dev/null +++ b/coin-change/PDKhan.cpp @@ -0,0 +1,20 @@ +class Solution { + public: + int coinChange(vector& coins, int amount) { + vector dp(amount+1, amount+1); + + dp[0] = 0; + + for(int i = 1; i <= amount; i++){ + for(int j = 0; j < coins.size(); j++){ + if(i >= coins[j]) + dp[i] = min(dp[i], 1 + dp[i - coins[j]]); + } + } + + if(dp[amount] > amount) + return -1; + + return dp[amount]; + } + };