-
-
Notifications
You must be signed in to change notification settings - Fork 305
[dylan-jung] WEEK 04 solutions #2131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
57a55c3
0a5dd4a
de1ca43
eed5169
df49ade
12aee53
4878256
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| class Solution { | ||
| public: | ||
| long dp[10001]; | ||
| int coinChange(vector<int>& 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]; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class Codec { | ||
| public: | ||
| string encode(vector<string>& strs) { | ||
| string result; | ||
| for (const string& s : strs) { | ||
| result += to_string(s.size()) + "#" + s; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| vector<string> decode(string s) { | ||
| vector<string> 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; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| class Solution { | ||
| public: | ||
| int findMin(vector<int>& 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]; | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 깊이를 나타내는 인수를 들고 dfs하는 문제였죠. 직관적으로 풀어주신 듯 합니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leetcode 특유의 리스트 관리 방법에서 제가 꽤 어려움을 많이 겪었던 기억이 납니다. 결국 연결 리스트는 참조 기반으로 이루어지고, 포인터를 기반으로 더미 노드를 맨 앞에 둔 다음 반환시 next를 사용한다는 것 말이죠.. 리스트1과 리스트2 중 남는 것을 이어붙이는 부분에서 매우 깔끔하게 해 주셨다는 생각이 듭니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. visited 방식을 사용하셨군요! 탐색 문제에서는 visited 배열의 사용을 생활화해야 하나, 하는 생각이 듭니다. 저는 주어진 배열의 글자를 특수문자로 바꾸고 원상복구 시켜가면서 백트래킹을 사용했는데 아무래도 좀 헷갈리더군요.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그것도 백트래킹에서 사용하기에 좋은 방법인 듯합니다! 그 방법에서 잠깐 생각해보니 다음과 같은 탐색 상황일 때, char mem = arr[i][j]; 처럼 결국 mem이라는 변수를 결국 만들어서 스택 영역에 저장해야한다는 점 때문에 bool 배열인 visited가 살짝 더 좋아보이긴 하네요. 사소한 차이라서 편한거 쓰서도 좋을 것 같습니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| class Solution { | ||
| public: | ||
| vector<vector<char>> 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<vector<char>>& 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; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dp 배열을 무한대로 초기화하셨군요! 대개 이런 경우 방문 처리를 해야 한다고 하나요.. 저는 이 문제를 바로 풀지는 못하고 태그를 토대로 방법을 유추만 해 봤는데, dp 배열을 -1로 초기화했습니다. 결국 if가 반환할 때랑 값을 갱신할 때, 어디에 들어가냐 문제인 것 같기는 해요.