Skip to content

4 files changed

+125
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
4+
int n = groupSizes.size();
5+
unordered_map<int, vector<int>> peopleInGroupSize = {};
6+
for(int i = 0; i < n; i++) {
7+
int curSize = groupSizes[i];
8+
peopleInGroupSize[curSize].push_back(i);
9+
}
10+
vector<vector<int>> ans = {};
11+
for(auto [groupSize, people]: peopleInGroupSize) {
12+
// start making groups of |groupSize|
13+
vector<int> cur = {};
14+
for(int i = 0; i < people.size(); i++) {
15+
cur.push_back(people[i]);
16+
if ( cur.size() == groupSize) {
17+
ans.push_back(cur);
18+
cur = {};
19+
}
20+
}
21+
}
22+
return ans;
23+
}
24+
};
25+
/*
26+
[group_size] --> [ids of people in it]
27+
[3] --> [0,1,2,3,4,6]
28+
[1] --> [5]
29+
*/

LeetCode/1302.deepest-leaves-sum.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
pair<int, int> getDepthAndSum(TreeNode* root) {
14+
if (root == NULL) return {0, 0};
15+
if (root->left == NULL and root->right == NULL)
16+
return {1, root->val};
17+
18+
auto [left_depth, left_sum] = getDepthAndSum(root->left);
19+
auto [right_depth, right_sum] = getDepthAndSum(root->right);
20+
21+
if (left_depth == right_depth)
22+
return {1 + left_depth, left_sum + right_sum};
23+
if (left_depth > right_depth)
24+
return {1 + left_depth, left_sum};
25+
return {1 + right_depth, right_sum};
26+
}
27+
public:
28+
int deepestLeavesSum(TreeNode* root) {
29+
return getDepthAndSum(root).second;
30+
}
31+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<int> processQueries(vector<int>& queries, int m) {
4+
unordered_map<int, int> pos, rev_pos; // pos[3] - 10 => 3 occurs at index 10
5+
for(int i = 1; i <= m; i++) {
6+
pos[i] = i-1, rev_pos[i-1] = i;
7+
}
8+
9+
vector<int> ans = {};
10+
for(int q: queries) {
11+
ans.push_back(pos[q]);
12+
// increment the pos of all nos from 0 to pos[q]-1
13+
for (int i = pos[q]-1 ; i >= 0; i--) {
14+
int cur = rev_pos[i];
15+
pos[cur] = i+1; // shift to right side
16+
rev_pos[i+1] = cur;
17+
}
18+
pos[q] = 0;
19+
rev_pos[0] = q;
20+
}
21+
return ans;
22+
}
23+
};
24+
// 1,2,3,4,5
25+
// 3,1,2,4,5
26+
27+
// _ _ _ _ _ _ _ _ _ _ _
28+
// ^
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
vector<int> minOperations(string boxes) {
4+
int n = boxes.size();
5+
int left_cnt = 0, tot_cnt = 0; // cnt of 1s
6+
int left_sum = 0, tot_sum = 0; // sum of indices where its a 1
7+
8+
9+
for(int i = 0; i < n; i++) {
10+
if (boxes[i] == '1') tot_cnt++, tot_sum += i;
11+
}
12+
13+
vector<int> ans = {};
14+
for(int i = 0; i < n; i++) {
15+
int right_cnt = tot_cnt - left_cnt;
16+
int right_sum = tot_sum - left_sum;
17+
18+
int left = left_cnt * i - left_sum;
19+
int right = right_sum - right_cnt * i;
20+
ans.push_back(left + right);
21+
22+
if (boxes[i] == '1') left_cnt++, left_sum += i;
23+
}
24+
return ans;
25+
}
26+
};
27+
28+
29+
/*
30+
i=13
31+
j<13
32+
i-j1 + i-j2 => 2*i - (j1+j2)
33+
34+
j>13
35+
j1-i + j2-i => (j1+j2) - 2 *(i)
36+
*/
37+

0 commit comments

Comments
 (0)