Skip to content

Commit 982944a

Browse files
committed
#224 & #227 & #245 & #255 & #269 solutions
1 parent f978922 commit 982944a

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

coin-change/ys-han00.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int coinChange(vector<int>& coins, int amount) {
4+
5+
vector<int> dp(amount + 1, 0xfffffff);
6+
dp[0] = 0;
7+
8+
for(int i = 0; i < coins.size(); i++)
9+
for(int j = coins[i]; j <= amount; j++)
10+
dp[j] = min(dp[j], dp[j - coins[i]] + 1);
11+
12+
return (dp[amount] == 0xfffffff ? -1 : dp[amount]);
13+
}
14+
};
15+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// class Solution {
2+
// public:
3+
// int findMin(vector<int>& nums) {
4+
// queue<pair<int, int>> que;
5+
// int left = 0, right = nums.size() - 1, mid, previous, next;
6+
7+
// if(nums.size() == 1)
8+
// return nums[0];
9+
// if(nums.size() == 2)
10+
// return min(nums[0], nums[1]);
11+
12+
// que.push({left, right});
13+
// while(!que.empty()) {
14+
// left = que.front().first;
15+
// right = que.front().second;
16+
// que.pop();
17+
18+
// mid = (left + right) / 2;
19+
// previous = (mid == 0 ? nums.size() - 1 : mid - 1);
20+
// next = (mid + 1 == nums.size() ? 0 : mid + 1);
21+
22+
// if(nums[previous] > nums[mid] && nums[mid] < nums[next])
23+
// return nums[mid];
24+
25+
// if(left <= mid - 1)
26+
// que.push({left, mid - 1});
27+
// if(mid + 1 <= right)
28+
// que.push({mid + 1, right});
29+
// }
30+
31+
// return -1;
32+
// }
33+
// };
34+
35+
class Solution {
36+
public:
37+
int findMin(vector<int>& nums) {
38+
int left = 0, right = nums.size() - 1, mid;
39+
40+
while(left < right) {
41+
mid = (left + right) / 2;
42+
43+
if(nums[right] < nums[mid])
44+
left = mid + 1;
45+
else
46+
right = mid;
47+
}
48+
49+
return nums[left];
50+
}
51+
};
52+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
// class Solution {
13+
// public:
14+
// int maxDepth(TreeNode* root) {
15+
// if(root == nullptr)
16+
// return 0;
17+
18+
// int ans = -1, depth;
19+
// queue<pair<int, TreeNode*>> que;
20+
21+
// que.push({1, root});
22+
// while(!que.empty()) {
23+
// depth = que.front().first;
24+
// TreeNode* Node = que.front().second;
25+
// que.pop();
26+
27+
// if(!Node->left && !Node->right) {
28+
// ans = max(ans, depth);
29+
// continue;
30+
// }
31+
// if(Node->left)
32+
// que.push({depth + 1, Node->left});
33+
// if(Node->right)
34+
// que.push({depth + 1, Node->right});
35+
// }
36+
37+
// return ans;
38+
// }
39+
// };
40+
41+
class Solution {
42+
public:
43+
int maxDepth(TreeNode* root) {
44+
if(!root)
45+
return 0;
46+
return 1 + max(maxDepth(root->left), maxDepth(root->right));
47+
}
48+
};
49+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
// class Solution {
12+
// public:
13+
// ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14+
// vector<int> nums;
15+
16+
// while(list1) {
17+
// nums.push_back(list1->val);
18+
// list1 = list1->next;
19+
// }
20+
// while(list2) {
21+
// nums.push_back(list2->val);
22+
// list2 = list2->next;
23+
// }
24+
25+
// if (nums.empty()) {
26+
// return nullptr;
27+
// }
28+
29+
// sort(nums.begin(), nums.end());
30+
// ListNode* head = new ListNode(nums[0]);
31+
// ListNode* current = head;
32+
// for(int i = 1; i < nums.size(); i++) {
33+
// current->next = new ListNode(nums[i]);
34+
// current = current->next;
35+
// }
36+
37+
// return head;
38+
// }
39+
// };
40+
41+
class Solution {
42+
public:
43+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
44+
ListNode* dummy = new ListNode(0);
45+
ListNode* current = dummy;
46+
47+
while(list1 && list2) {
48+
if(list1->val < list2->val) {
49+
current->next = list1;
50+
list1 = list1->next;
51+
} else {
52+
current->next = list2;
53+
list2 = list2->next;
54+
}
55+
current = current->next;
56+
}
57+
58+
if(list1)
59+
current->next = list1;
60+
else
61+
current->next = list2;
62+
63+
ListNode* head = dummy->next;
64+
delete dummy;
65+
66+
return head;
67+
}
68+
};
69+

word-search/ys-han00.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
int m, n;
4+
int dx[4] = {-1, 0, 1, 0};
5+
int dy[4] = {0, 1, 0, -1};
6+
7+
bool exist(vector<vector<char>>& board, string word) {
8+
m = board.size();
9+
n = board[0].size();
10+
11+
for (int i = 0; i < m; i++)
12+
for (int j = 0; j < n; j++)
13+
if (dfs(board, word, i, j, 0))
14+
return true;
15+
16+
return false;
17+
}
18+
19+
bool dfs(vector<vector<char>>& board, string& word, int x, int y, int idx) {
20+
if (idx == word.size()) return true;
21+
22+
if (x < 0 || y < 0 || x >= m || y >= n || board[x][y] != word[idx])
23+
return false;
24+
25+
char temp = board[x][y];
26+
board[x][y] = '*';
27+
28+
bool found = false;
29+
for (int k = 0; k < 4; k++)
30+
if (dfs(board, word, x + dx[k], y + dy[k], idx + 1)) {
31+
found = true;
32+
break;
33+
}
34+
35+
board[x][y] = temp;
36+
37+
return found;
38+
}
39+
};
40+

0 commit comments

Comments
 (0)