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
15 changes: 15 additions & 0 deletions coin-change/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {

vector<int> dp(amount + 1, 0xfffffff);
dp[0] = 0;

for(int i = 0; i < coins.size(); i++)
for(int j = coins[i]; j <= amount; j++)
dp[j] = min(dp[j], dp[j - coins[i]] + 1);

return (dp[amount] == 0xfffffff ? -1 : dp[amount]);
}
};

52 changes: 52 additions & 0 deletions find-minimum-in-rotated-sorted-array/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// class Solution {
// public:
// int findMin(vector<int>& nums) {
// queue<pair<int, int>> que;
// int left = 0, right = nums.size() - 1, mid, previous, next;

// if(nums.size() == 1)
// return nums[0];
// if(nums.size() == 2)
// return min(nums[0], nums[1]);

// que.push({left, right});
// while(!que.empty()) {
// left = que.front().first;
// right = que.front().second;
// que.pop();

// mid = (left + right) / 2;
// previous = (mid == 0 ? nums.size() - 1 : mid - 1);
// next = (mid + 1 == nums.size() ? 0 : mid + 1);

// if(nums[previous] > nums[mid] && nums[mid] < nums[next])
// return nums[mid];

// if(left <= mid - 1)
// que.push({left, mid - 1});
// if(mid + 1 <= right)
// que.push({mid + 1, right});
// }

// return -1;
// }
// };

class Solution {
public:
int findMin(vector<int>& nums) {
int left = 0, right = nums.size() - 1, mid;

while(left < right) {
mid = (left + right) / 2;

if(nums[right] < nums[mid])
left = mid + 1;
else
right = mid;
}
Comment on lines +40 to +47
Copy link
Contributor

Choose a reason for hiding this comment

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

이분 탐색으로 가능한 문제였군요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

맨 처음 풀이를 너무 이상하게 풀었는데, 풀고나서 보이더라구요 ㅎㅎ


return nums[left];
}
};

49 changes: 49 additions & 0 deletions maximum-depth-of-binary-tree/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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 maxDepth(TreeNode* root) {
// if(root == nullptr)
// return 0;

// int ans = -1, depth;
// queue<pair<int, TreeNode*>> que;

// que.push({1, root});
// while(!que.empty()) {
// depth = que.front().first;
// TreeNode* Node = que.front().second;
// que.pop();

// if(!Node->left && !Node->right) {
// ans = max(ans, depth);
// continue;
// }
// if(Node->left)
// que.push({depth + 1, Node->left});
// if(Node->right)
// que.push({depth + 1, Node->right});
// }

// return ans;
// }
// };

class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root)
return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};

69 changes: 69 additions & 0 deletions merge-two-sorted-lists/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* 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) {
// vector<int> nums;

// while(list1) {
// nums.push_back(list1->val);
// list1 = list1->next;
// }
// while(list2) {
// nums.push_back(list2->val);
// list2 = list2->next;
// }

// if (nums.empty()) {
// return nullptr;
// }

// sort(nums.begin(), nums.end());
// ListNode* head = new ListNode(nums[0]);
// ListNode* current = head;
// for(int i = 1; i < nums.size(); i++) {
// current->next = new ListNode(nums[i]);
// current = current->next;
// }

// return head;
// }
// };

class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode* dummy = new ListNode(0);
ListNode* current = dummy;

while(list1 && list2) {
if(list1->val < list2->val) {
current->next = list1;
list1 = list1->next;
} else {
current->next = list2;
list2 = list2->next;
}
current = current->next;
}

if(list1)
current->next = list1;
else
current->next = list2;

ListNode* head = dummy->next;
delete dummy;

return head;
}
};

40 changes: 40 additions & 0 deletions word-search/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public:
int m, n;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

bool exist(vector<vector<char>>& board, string word) {
m = board.size();
n = board[0].size();

for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (dfs(board, word, i, j, 0))
return true;

return false;
}

bool dfs(vector<vector<char>>& board, string& word, int x, int y, int idx) {
if (idx == word.size()) return true;

if (x < 0 || y < 0 || x >= m || y >= n || board[x][y] != word[idx])
return false;

char temp = board[x][y];
board[x][y] = '*';

bool found = false;
for (int k = 0; k < 4; k++)
if (dfs(board, word, x + dx[k], y + dy[k], idx + 1)) {
found = true;
break;
}

board[x][y] = temp;

return found;
}
};