Skip to content

Commit 15aa6ab

Browse files
authored
Merge pull request #16 from Rishabh0210/Rishabh0210-patch-1
Rishabh0210 patch 1
2 parents db5bfd2 + 9ea85ef commit 15aa6ab

9 files changed

+257
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3353/
2+
3+
4+
5+
class Solution {
6+
public:
7+
int change(int amount, vector<int>& coins) {
8+
vector<vector<int>> DP(amount + 1, vector<int>(coins.size() + 1, 0));
9+
sort(coins.begin(), coins.end());
10+
11+
12+
int i, j;
13+
for(i = 0; i <= coins.size(); i++)
14+
DP[0][i] = 1;
15+
16+
for(i = 1; i <= amount; i++) {
17+
for(j = 1; j <= coins.size(); j++) {
18+
if(i >= coins[j - 1])
19+
DP[i][j] = DP[i - coins[j - 1]][j];
20+
DP[i][j] += DP[i][j - 1];
21+
}
22+
}
23+
return DP[amount][coins.size()];
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3348/
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* struct ListNode {
6+
* int val;
7+
* ListNode *next;
8+
* ListNode(int x) : val(x), next(NULL) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
void deleteNode(ListNode* node) {
14+
ListNode* prev = node;
15+
while(node -> next != NULL) {
16+
node -> val = node -> next -> val;
17+
prev = node;
18+
node = node -> next;
19+
}
20+
prev -> next = NULL;
21+
// free(node);
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3347/
2+
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* struct TreeNode {
7+
* int val;
8+
* TreeNode *left;
9+
* TreeNode *right;
10+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
11+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
13+
* };
14+
*/
15+
class Solution {
16+
public:
17+
TreeNode* invertTree(TreeNode* root) {
18+
queue<TreeNode*> q;
19+
q.push(root);
20+
21+
while(!q.empty()) {
22+
TreeNode* node = q.front();
23+
q.pop();
24+
25+
if(node == NULL)
26+
continue;
27+
28+
TreeNode* left = node -> left;
29+
TreeNode* right = node -> right;
30+
31+
node -> right = left;
32+
node -> left = right;
33+
34+
q.push(left);
35+
q.push(right);
36+
}
37+
return root;
38+
}
39+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/540/week-2-june-8th-june-14th/3355/
2+
3+
class Solution {
4+
public:
5+
bool isSubsequence(string s, string t) {
6+
int l1 = s.size();
7+
int l2 = t.size();
8+
int i = 0, j = 0;
9+
while(i < l2 && j < l1) {
10+
if(t[i] == s[j])
11+
j++;
12+
i++;
13+
}
14+
return j == l1;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3352/
2+
3+
class Solution {
4+
private:
5+
static bool compare(vector<int> v1, vector<int> v2) {
6+
return v1[0] < v2[0];
7+
}
8+
public:
9+
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
10+
sort(people.begin(), people.end(), compare);
11+
int n = people.size();
12+
vector<vector<int>> ans(n, vector<int>(2, INT_MAX));
13+
14+
int i;
15+
for(i = 0; i < n; i++) {
16+
int person_in_front = people[i][1];
17+
int j = 0;
18+
for(j = 0; j < n; j++) {
19+
if(ans[j][0] == INT_MAX && person_in_front == 0)
20+
break;
21+
else if(ans[j][0] == INT_MAX || ans[j][0] == people[i][0])
22+
person_in_front--;
23+
}
24+
ans[j][0] = people[i][0];
25+
ans[j][1] = people[i][1];
26+
}
27+
28+
return ans;
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3351/
2+
3+
class Solution {
4+
public:
5+
vector<int> cumWeight;
6+
Solution(vector<int>& w) {
7+
int sum = 0;
8+
for(int i = 0; i < w.size(); i++) {
9+
sum += w[i];
10+
cumWeight.push_back(sum);
11+
}
12+
}
13+
14+
int pickIndex() {
15+
int upper_limit = cumWeight[cumWeight.size() - 1];
16+
int target = rand() % upper_limit;
17+
int mid, ans, high, low;
18+
high = cumWeight.size() - 1;
19+
low = 0;
20+
ans = -1;
21+
22+
while(low <= high) {
23+
mid = (low + (high - low)/2);
24+
if(target < cumWeight[mid]) {
25+
ans = mid;
26+
high = mid - 1;
27+
} else {
28+
low = mid + 1;
29+
}
30+
}
31+
return ans;
32+
}
33+
};
34+
35+
/**
36+
* Your Solution object will be instantiated and called as such:
37+
* Solution* obj = new Solution(w);
38+
* int param_1 = obj->pickIndex();
39+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3350/
2+
3+
class Solution {
4+
public:
5+
void reverseString(vector<char>& s) {
6+
int i, j;
7+
i = 0;
8+
j = s.size() - 1;
9+
10+
while(i < j) {
11+
char temp = s[i];
12+
s[i] = s[j];
13+
s[j] = temp;
14+
15+
i++;
16+
j--;
17+
}
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/540/week-2-june-8th-june-14th/3356/
2+
3+
class Solution {
4+
public:
5+
int searchInsert(vector<int>& nums, int target) {
6+
int l = 0, h = nums.size() - 1;
7+
while(l <= h) {
8+
int mid = l + (h - l)/2;
9+
if(nums[mid] == target)
10+
return mid;
11+
else if(nums[mid] < target)
12+
l = mid + 1;
13+
else
14+
h = mid - 1;
15+
}
16+
return l;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3349/
2+
3+
class Solution {
4+
private:
5+
static bool compare(pair<pair<int, int>, int> p1, pair<pair<int, int>, int> p2) {
6+
return p1.second > p2.second;
7+
}
8+
public:
9+
int twoCitySchedCost(vector<vector<int>>& costs) {
10+
vector<pair<pair<int, int>, int>> weight;
11+
12+
int i, n = costs.size();
13+
14+
for(i = 0; i < n; i++) {
15+
pair<int, int> p1 = make_pair(costs[i][0], costs[i][1]);
16+
17+
int diff = abs(costs[i][0] - costs[i][1]);
18+
19+
pair<pair<int, int>, int> p2 = make_pair(p1, diff);
20+
weight.push_back(p2);
21+
}
22+
23+
sort(weight.begin(), weight.end(), compare);
24+
25+
int A = n/2, B = n/2;
26+
int ans = 0;
27+
for(i = 0; i < n; i++) {
28+
pair<int, int> p = weight[i].first;
29+
30+
if(A > 0 && B > 0) {
31+
if(p.first < p.second) {
32+
A--;
33+
ans += p.first;
34+
} else {
35+
B--;
36+
ans += p.second;
37+
}
38+
} else if (A > 0) {
39+
A--;
40+
ans += p.first;
41+
} else {
42+
B--;
43+
ans += p.second;
44+
}
45+
}
46+
return ans;
47+
}
48+
};

0 commit comments

Comments
 (0)