Skip to content

Commit 2441c4f

Browse files
committed
update solutions
1 parent e29a178 commit 2441c4f

25 files changed

+552
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Runtime: 268 ms
2+
// Memory Usage: 60.5 MB
3+
class Solution {
4+
public:
5+
void traverse(TreeNode* root, vector<int> &l) {
6+
if (!root) return;
7+
traverse(root->left, l);
8+
l.push_back(root->val);
9+
traverse(root->right, l);
10+
}
11+
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
12+
vector<int> l1, l2;
13+
traverse(root1, l1);
14+
traverse(root2, l2);
15+
vector<int> res;
16+
int i = 0, j = 0;
17+
while (i < l1.size() && j < l2.size()) {
18+
if (l1[i] < l2[j]) {
19+
res.push_back(l1[i++]);
20+
} else {
21+
res.push_back(l2[j++]);
22+
}
23+
}
24+
while (i < l1.size()) {
25+
res.push_back(l1[i++]);
26+
}
27+
while (j < l2.size()) {
28+
res.push_back(l2[j++]);
29+
}
30+
return res;
31+
}
32+
};

check-if-n-and-its-double-exist.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Runtime: 4 ms
2+
// Memory Usage: 9.2 MB
3+
class Solution {
4+
public:
5+
bool checkIfExist(vector<int>& arr) {
6+
set<int> s;
7+
for (int a : arr) {
8+
if (s.find(a * 2) != s.end() || (a % 2 == 0 && s.find(a / 2) != s.end())) {
9+
return true;
10+
}
11+
s.insert(a);
12+
}
13+
return false;
14+
}
15+
};

contiguous-array.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Runtime: 176 ms
2+
// Memory Usage: 17.7 MB
3+
class Solution {
4+
public:
5+
int findMaxLength(vector<int>& nums) {
6+
int count = 0;
7+
int res = 0;
8+
unordered_map<int, int> mp;
9+
for (int i = 0; i < nums.size(); i++) {
10+
count += nums[i] == 0 ? -1 : 1;
11+
if (count == 0) {
12+
res = i + 1;
13+
} else if (mp.find(count) != mp.end()) {
14+
res = max(res, i - mp[count]);
15+
} else {
16+
mp[count] = i;
17+
}
18+
}
19+
return res;
20+
}
21+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Runtime: 0 ms
2+
// Memory Usage: 8.5 MB
3+
class Solution {
4+
public:
5+
vector<int> getNoZeroIntegers(int n) {
6+
vector<int> res;
7+
for (int i = 1; i < n; i++) {
8+
string s = to_string(i) + to_string(n - i);
9+
if (s.find('0') == string::npos) {
10+
res.push_back(n - i);
11+
res.push_back(i);
12+
return res;
13+
}
14+
}
15+
return res;
16+
}
17+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Runtime: 20 ms
2+
// Memory Usage: 10.3 MB
3+
class Solution {
4+
public:
5+
int countNegatives(vector<vector<int>>& grid) {
6+
int res = 0;
7+
for (int i = grid.size() - 1; i >= 0 && grid[i][grid[i].size() - 1] < 0; i--) {
8+
for (int j = grid[i].size() - 1; j >= 0 && grid[i][j] < 0; j--) {
9+
res++;
10+
}
11+
}
12+
return res;
13+
}
14+
};

count-servers-that-communicate.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Runtime: 56 ms
2+
// Memory Usage: 15.1 MB
3+
class Solution {
4+
public:
5+
int countServers(vector<vector<int>>& grid) {
6+
vector<int> row(grid.size(), 0);
7+
vector<int> col(grid[0].size(), 0);
8+
9+
for (int i = 0; i < grid.size(); i++) {
10+
for (int j = 0; j < grid[0].size(); j++) {
11+
if (grid[i][j]) {
12+
row[i]++;
13+
col[j]++;
14+
}
15+
}
16+
}
17+
int res = 0;
18+
for (int i = 0; i < grid.size(); i++) {
19+
for (int j = 0; j < grid[i].size(); j++) {
20+
if (grid[i][j] && (row[i] > 1 || col[j] > 1)) {
21+
res++;
22+
}
23+
}
24+
}
25+
return res;
26+
}
27+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Runtime: 0 ms
2+
// Memory Usage: 8.5 MB
3+
class Solution {
4+
public:
5+
string freqAlphabets(string s) {
6+
string res;
7+
for (int i = 0; i < s.size(); i++) {
8+
if (i + 2 < s.size() && s[i + 1] != '#' && s[i + 2] == '#') {
9+
res += 'a' - 1 + ((s[i++] - '0') * 10 + s[i++] - '0');
10+
} else {
11+
res += 'a' - 1 + (s[i] - '0');
12+
}
13+
}
14+
return res;
15+
}
16+
};

deepest-leaves-sum.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Runtime: 48 ms
2+
// Memory Usage: 31.3 MB
3+
class Solution {
4+
public:
5+
void solve(TreeNode* root, int &res, int d, int &md) {
6+
if (!root) return;
7+
solve(root->left, res, d + 1, md);
8+
solve(root->right, res, d + 1, md);
9+
if (!root->left && !root->right) {
10+
if (d == md) {
11+
res += root->val;
12+
} else if (d > md) {
13+
res = root->val;
14+
}
15+
}
16+
md = max(d, md);
17+
}
18+
int deepestLeavesSum(TreeNode* root) {
19+
int res = 0;
20+
int md = 0;
21+
solve(root, res, 0, md);
22+
return res;
23+
}
24+
};

delete-leaves-with-a-given-value.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Runtime: 20 ms
2+
// Memory Usage: 21.5 MB
3+
class Solution {
4+
public:
5+
TreeNode* removeLeafNodes(TreeNode* root, int target) {
6+
if (!root) return root;
7+
root->left = removeLeafNodes(root->left, target);
8+
root->right = removeLeafNodes(root->right, target);
9+
10+
if (!root->left && !root->right && root->val == target) {
11+
return NULL;
12+
}
13+
return root;
14+
}
15+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Runtime: 52 ms
2+
// Memory Usage: 30.1 MB
3+
class FindElements {
4+
public:
5+
6+
set<int> st;
7+
8+
void solve(TreeNode* root, int val) {
9+
if (!root) return;
10+
11+
root->val = val;
12+
st.insert(val);
13+
solve(root->left, val * 2 + 1);
14+
solve(root->right, val * 2 + 2);
15+
}
16+
17+
FindElements(TreeNode* root) {
18+
solve(root, 0);
19+
}
20+
21+
bool find(int target) {
22+
return st.find(target) != st.end();
23+
}
24+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Runtime: 4 ms
2+
// Memory Usage: 8.7 MB
3+
class Solution {
4+
public:
5+
vector<int> sumZero(int n) {
6+
vector<int> res;
7+
res.push_back(0);
8+
for (int i = 1; i < n; i++) {
9+
res[0] -= i;
10+
res.push_back(i);
11+
}
12+
return res;
13+
}
14+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Runtime: 124 ms
2+
// Memory Usage: 31.5 MB
3+
class Solution {
4+
public:
5+
6+
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
7+
map<string, int> viewCount;
8+
vector<int> vis(friends.size(), 0);
9+
10+
queue<int >q;
11+
q.push(id);
12+
int d = 0;
13+
vis[id] = 1;
14+
while (!q.empty()) {
15+
int size = q.size();
16+
while (size--) {
17+
int t = q.front();
18+
q.pop();
19+
if (d == level) {
20+
for (string a : watchedVideos[t]) {
21+
viewCount[a]++;
22+
}
23+
}
24+
25+
for (int f : friends[t]) {
26+
if (!vis[f]) {
27+
vis[f] = 1;
28+
q.push(f);
29+
}
30+
}
31+
}
32+
if (d >= level) {
33+
break;
34+
}
35+
d++;
36+
}
37+
38+
vector<pair<string, int> >countVec(viewCount.begin(), viewCount.end());
39+
sort(countVec.begin(), countVec.end(), [](pair<string, int> &a, pair<string, int> &b) {
40+
return a.second == b.second ? a.first < b.first : a.second < b.second;
41+
});
42+
vector<string> res;
43+
for (auto a : countVec) {
44+
res.push_back(a.first);
45+
}
46+
return res;
47+
}
48+
};

iterator-for-combination.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Runtime: 12 ms
2+
// Memory Usage: 14.4 MB
3+
class CombinationIterator {
4+
private:
5+
vector<string> res;
6+
int curr = 0;
7+
void backtrack(string &chars, int idx, string s, int &len) {
8+
if (s.size() == len) {
9+
res.push_back(s);
10+
return;
11+
}
12+
for (int i = idx; i < chars.size(); i++) {
13+
if (s.size() == 0 || s[s.size() - 1] < chars[i]) {
14+
backtrack(chars, i + 1, s + chars[i], len);
15+
}
16+
}
17+
}
18+
public:
19+
CombinationIterator(string characters, int combinationLength) {
20+
backtrack(characters, 0, "", combinationLength);
21+
}
22+
string next() {
23+
return res[curr++];
24+
}
25+
26+
bool hasNext() {
27+
return curr < res.size();
28+
}
29+
};

maximum-69-number.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Runtime: 28 ms
2+
# Memory Usage: 9.3 MB
3+
def maximum69_number (num)
4+
num.to_s.sub('6', '9').to_i
5+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Runtime: 236 ms
2+
// Memory Usage: 64.6 MB
3+
class Solution {
4+
public:
5+
6+
bool allUniq(string &s) {
7+
set<char> st;
8+
for (char a : s) {
9+
if (st.find(a) != st.end()) {
10+
return false;
11+
}
12+
st.insert(a);
13+
}
14+
return true;
15+
}
16+
17+
void solve(vector<string> &arr, string s, int idx, int &res) {
18+
if (allUniq(s)) {
19+
if (res < s.size()) {
20+
res = s.size();
21+
}
22+
} else {
23+
if (s.size() > 0) {
24+
return;
25+
}
26+
}
27+
for (int i = idx; i < arr.size(); i++) {
28+
solve(arr, s + arr[i], i + 1, res);
29+
}
30+
}
31+
32+
int maxLength(vector<string>& arr) {
33+
int res = 0;
34+
string s = "";
35+
solve(arr, s, 0, res);
36+
return res;
37+
}
38+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Runtime: 160 ms
2+
// Memory Usage: 58.3 MB
3+
class Solution {
4+
public:
5+
long res = 0;
6+
long total = 0;
7+
long solve(TreeNode* root) {
8+
if (!root) return 0;
9+
long curr = solve(root->left) + solve(root->right) + root->val;
10+
res = max(res, curr * (total - curr));
11+
return curr;
12+
13+
}
14+
int maxProduct(TreeNode* root) {
15+
total = solve(root);
16+
solve(root);
17+
return res % (int)(1e9 + 7);
18+
}
19+
};

0 commit comments

Comments
 (0)