Skip to content

Commit 80611a9

Browse files
authored
Merge pull request #1332 from PDKhan/main
2 parents 4e52bdf + f96fa50 commit 80611a9

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

coin-change/PDKhan.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int coinChange(vector<int>& coins, int amount) {
4+
vector<int> dp(amount+1, amount+1);
5+
6+
dp[0] = 0;
7+
8+
for(int i = 1; i <= amount; i++){
9+
for(int j = 0; j < coins.size(); j++){
10+
if(i >= coins[j])
11+
dp[i] = min(dp[i], 1 + dp[i - coins[j]]);
12+
}
13+
}
14+
15+
if(dp[amount] > amount)
16+
return -1;
17+
18+
return dp[amount];
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findMin(vector<int>& nums) {
4+
int left = 0;
5+
int right = nums.size() - 1;
6+
7+
while(left < right){
8+
int mid = (left + right) / 2;
9+
10+
if(nums[mid] > nums[right])
11+
left = mid + 1;
12+
else
13+
right = mid;
14+
}
15+
16+
return nums[left];
17+
}
18+
};
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
int maxDepth(TreeNode* root) {
4+
if(root == nullptr)
5+
return 0;
6+
7+
return 1 + max(maxDepth(root->left), maxDepth(root->right));
8+
}
9+
};

merge-two-sorted-lists/PDKhan.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
4+
ListNode* new_head = NULL;
5+
ListNode* tail;
6+
7+
while(list1 || list2){
8+
ListNode* curr;
9+
10+
if(list1 && list2){
11+
if(list1->val < list2->val){
12+
curr = list1;
13+
list1 = list1->next;
14+
}else{
15+
curr = list2;
16+
list2 = list2->next;
17+
}
18+
}else if(list1){
19+
curr = list1;
20+
list1 = list1->next;
21+
}else{
22+
curr = list2;
23+
list2 = list2->next;
24+
}
25+
26+
if(new_head == NULL){
27+
new_head = curr;
28+
tail = new_head;
29+
}else{
30+
tail->next = curr;
31+
tail = tail->next;
32+
}
33+
}
34+
35+
return new_head;
36+
}
37+
};

word-search/PDKhan.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
bool search(int r, int c, int index, vector<vector<char>>& board, string word){
4+
if(index == word.length())
5+
return true;
6+
7+
if(r < 0 || r >= board.size() || c < 0 || c >= board[0].size() || board[r][c] != word[index])
8+
return false;
9+
10+
char curr = board[r][c];
11+
12+
board[r][c] = '0';
13+
14+
if(search(r + 1, c, index + 1, board, word) == true)
15+
return true;
16+
17+
if(search(r - 1, c, index + 1, board, word) == true)
18+
return true;
19+
20+
if(search(r, c + 1, index + 1, board, word) == true)
21+
return true;
22+
23+
if(search(r, c - 1, index + 1, board, word) == true)
24+
return true;
25+
26+
board[r][c] = curr;
27+
28+
return false;
29+
}
30+
31+
bool exist(vector<vector<char>>& board, string word) {
32+
for(int i = 0; i < board.size(); i++){
33+
for(int j = 0; j < board[i].size(); j++){
34+
if(board[i][j] == word[0]){
35+
if(search(i, j, 0, board, word) == true)
36+
return true;
37+
}
38+
}
39+
}
40+
41+
return false;
42+
}
43+
};

0 commit comments

Comments
 (0)