Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions coin-change/dylan-jung.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dp 배열을 무한대로 초기화하셨군요! 대개 이런 경우 방문 처리를 해야 한다고 하나요.. 저는 이 문제를 바로 풀지는 못하고 태그를 토대로 방법을 유추만 해 봤는데, dp 배열을 -1로 초기화했습니다. 결국 if가 반환할 때랑 값을 갱신할 때, 어디에 들어가냐 문제인 것 같기는 해요.

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];
}
};
24 changes: 24 additions & 0 deletions encode-and-decode-strings/dylan-jung.cpp
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;
}
};
23 changes: 23 additions & 0 deletions find-minimum-in-rotated-sorted-array/dylan-jung.cpp
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];
}
};
25 changes: 25 additions & 0 deletions maximum-depth-of-binary-tree/dylan-jung.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}
};
32 changes: 32 additions & 0 deletions merge-two-sorted-lists/dylan-jung.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
};
40 changes: 40 additions & 0 deletions word-search/dylan-jung.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visited 방식을 사용하셨군요! 탐색 문제에서는 visited 배열의 사용을 생활화해야 하나, 하는 생각이 듭니다. 저는 주어진 배열의 글자를 특수문자로 바꾸고 원상복구 시켜가면서 백트래킹을 사용했는데 아무래도 좀 헷갈리더군요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그것도 백트래킹에서 사용하기에 좋은 방법인 듯합니다!

그 방법에서 잠깐 생각해보니 다음과 같은 탐색 상황일 때,

char mem = arr[i][j];
arr[i][j] = '+'
dfs(next)
arr[i][j] = mem;

처럼 결국 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;
}
};