diff --git a/coin-change/dylan-jung.cpp b/coin-change/dylan-jung.cpp new file mode 100644 index 0000000000..6c81b9a695 --- /dev/null +++ b/coin-change/dylan-jung.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + long dp[10001]; + int coinChange(vector& coins, int amount) { + long INF = (long)1 << 31; + fill(dp, dp+10001, INF); + + for(int& item: coins) { + if(item > 10000) continue; + dp[item] = 1; + } + + for(int i = 1; i <= amount; i++) { + for(int& item: coins) { + int j = i - item; + if(j > 0 && dp[j] != -1) { + dp[i] = min(dp[j] + 1, dp[i]); + } + } + } + + if(dp[amount] == INF) { + if(amount == 0) return 0; + else return -1; + } + return dp[amount]; + } +}; diff --git a/encode-and-decode-strings/dylan-jung.cpp b/encode-and-decode-strings/dylan-jung.cpp new file mode 100644 index 0000000000..451e72d25e --- /dev/null +++ b/encode-and-decode-strings/dylan-jung.cpp @@ -0,0 +1,24 @@ +class Codec { + public: + string encode(vector& strs) { + string result; + for (const string& s : strs) { + result += to_string(s.size()) + "#" + s; + } + return result; + } + + vector decode(string s) { + vector result; + int i = 0; + while (i < s.size()) { + int j = i; + while (s[j] != '#') j++; + int length = stoi(s.substr(i, j - i)); + string cur = s.substr(j + 1, length); + result.push_back(cur); + i = j + 1 + length; + } + return result; + } + }; diff --git a/find-minimum-in-rotated-sorted-array/dylan-jung.cpp b/find-minimum-in-rotated-sorted-array/dylan-jung.cpp new file mode 100644 index 0000000000..44e996ba37 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/dylan-jung.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int findMin(vector& nums) { + int l = 0; int r = nums.size() - 1; + int min = -10000; + if(nums[l] <= nums[r]) return nums[0]; // ordered case + while(l <= r) { + int mid = (l+r)/2; + if(nums[l] > nums[mid]) { + min = mid; + r = mid; + } + else if(nums[mid] > nums[r]) { + min = mid+1; + l = mid+1; + } + else { + break; + } + } + return nums[min]; + } +}; diff --git a/maximum-depth-of-binary-tree/dylan-jung.cpp b/maximum-depth-of-binary-tree/dylan-jung.cpp new file mode 100644 index 0000000000..d507899027 --- /dev/null +++ b/maximum-depth-of-binary-tree/dylan-jung.cpp @@ -0,0 +1,25 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int dfs(TreeNode* root, int depth) { + int ret = depth; + if(root->left) ret = max(ret, dfs(root->left, depth+1)); + if(root->right) ret = max(ret, dfs(root->right, depth+1)); + return ret; + } + + int maxDepth(TreeNode* root) { + if(!root) return 0; + return dfs(root, 1); + } +}; diff --git a/merge-two-sorted-lists/dylan-jung.cpp b/merge-two-sorted-lists/dylan-jung.cpp new file mode 100644 index 0000000000..b7945e6c4d --- /dev/null +++ b/merge-two-sorted-lists/dylan-jung.cpp @@ -0,0 +1,32 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { + ListNode dummy; + ListNode* tail = &dummy; + + while (list1 && list2) { + if (list1->val < list2->val) { + tail->next = list1; + list1 = list1->next; + } else { + tail->next = list2; + list2 = list2->next; + } + tail = tail->next; + } + + tail->next = list1 ? list1 : list2; + + return dummy.next; + } +}; diff --git a/word-search/dylan-jung.cpp b/word-search/dylan-jung.cpp new file mode 100644 index 0000000000..9520edddf8 --- /dev/null +++ b/word-search/dylan-jung.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + vector> g_board; + int m, n; + int dx[4] = { -1, 1, 0, 0 }; + int dy[4] = { 0, 0, -1, 1 }; + bool visited[6][6]; + + bool hasWord(string word, int idx, int r, int c) { + if(idx >= word.size()) return true; + if(!(0 <= r && r < n && 0 <= c && c < m)) return false; + if(g_board[r][c] != word[idx]) return false; + if(visited[r][c]) return false; + visited[r][c] = true; + // cout << r << " " << c << " " << word[idx] << "\n"; + for(int i = 0; i < 4; i++) { + int nr = r + dx[i]; + int nc = c + dy[i]; + if(hasWord(word, idx+1, nr, nc)) + return true; + } + visited[r][c] = false; + return false; + } + + bool exist(vector>& board, string word) { + g_board = board; + n = board.size(); + m = board[0].size(); + for(int i = 0; i < n; i++) { + for(int j = 0; j < m; j++) { + if(g_board[i][j] == word[0]) { + fill(&visited[0][0], &visited[0][0]+36, false); + if(hasWord(word, 0, i, j)) return true; + } + } + } + return false; + } +};