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]; + } + }; 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]; + } + }; 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)); + } + }; 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; + } + }; 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; + } + };