Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update cpp solutions #3923

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lcci/04.01.Route Between Nodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public:
for (auto& e : graph) {
g[e[0]].push_back(e[1]);
}
function<bool(int)> dfs = [&](int i) {
auto dfs = [&](this auto&& dfs, int i) -> bool {
if (i == target) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion lcci/04.01.Route Between Nodes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public:
for (auto& e : graph) {
g[e[0]].push_back(e[1]);
}
function<bool(int)> dfs = [&](int i) {
auto dfs = [&](this auto&& dfs, int i) -> bool {
if (i == target) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions lcci/04.01.Route Between Nodes/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Solution {
for (auto& e : graph) {
g[e[0]].push_back(e[1]);
}
function<bool(int)> dfs = [&](int i) {
auto dfs = [&](this auto&& dfs, int i) -> bool {
if (i == target) {
return true;
}
Expand All @@ -23,4 +23,4 @@ class Solution {
};
return dfs(start);
}
};
};
2 changes: 1 addition & 1 deletion lcci/04.02.Minimum Height Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Solution {
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
function<TreeNode*(int, int)> dfs = [&](int l, int r) -> TreeNode* {
auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* {
if (l > r) {
return nullptr;
}
Expand Down
14 changes: 7 additions & 7 deletions lcci/04.02.Minimum Height Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ Given sorted array: [-10,-3,0,5,9],



One possible answer is: [0,-3,9,-10,null,5],which represents the following tree:
One possible answer is: [0,-3,9,-10,null,5],which represents the following tree:



0
0

/ \
/ \

-3 9
-3 9

/ /
/ /

-10 5
-10 5

</pre>

Expand Down Expand Up @@ -127,7 +127,7 @@ class Solution {
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
function<TreeNode*(int, int)> dfs = [&](int l, int r) -> TreeNode* {
auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* {
if (l > r) {
return nullptr;
}
Expand Down
4 changes: 2 additions & 2 deletions lcci/04.02.Minimum Height Tree/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
function<TreeNode*(int, int)> dfs = [&](int l, int r) -> TreeNode* {
auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* {
if (l > r) {
return nullptr;
}
Expand All @@ -19,4 +19,4 @@ class Solution {
};
return dfs(0, nums.size() - 1);
}
};
};
6 changes: 3 additions & 3 deletions solution/0100-0199/0131.Palindrome Partitioning/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ public:
}
vector<vector<string>> ans;
vector<string> t;
function<void(int)> dfs = [&](int i) {
auto dfs = [&](this auto&& dfs, int i) -> void {
if (i == n) {
ans.push_back(t);
ans.emplace_back(t);
return;
}
for (int j = i; j < n; ++j) {
if (f[i][j]) {
t.push_back(s.substr(i, j - i + 1));
t.emplace_back(s.substr(i, j - i + 1));
dfs(j + 1);
t.pop_back();
}
Expand Down
6 changes: 3 additions & 3 deletions solution/0100-0199/0131.Palindrome Partitioning/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ public:
}
vector<vector<string>> ans;
vector<string> t;
function<void(int)> dfs = [&](int i) {
auto dfs = [&](this auto&& dfs, int i) -> void {
if (i == n) {
ans.push_back(t);
ans.emplace_back(t);
return;
}
for (int j = i; j < n; ++j) {
if (f[i][j]) {
t.push_back(s.substr(i, j - i + 1));
t.emplace_back(s.substr(i, j - i + 1));
dfs(j + 1);
t.pop_back();
}
Expand Down
8 changes: 4 additions & 4 deletions solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class Solution {
}
vector<vector<string>> ans;
vector<string> t;
function<void(int)> dfs = [&](int i) {
auto dfs = [&](this auto&& dfs, int i) -> void {
if (i == n) {
ans.push_back(t);
ans.emplace_back(t);
return;
}
for (int j = i; j < n; ++j) {
if (f[i][j]) {
t.push_back(s.substr(i, j - i + 1));
t.emplace_back(s.substr(i, j - i + 1));
dfs(j + 1);
t.pop_back();
}
Expand All @@ -27,4 +27,4 @@ class Solution {
dfs(0);
return ans;
}
};
};
2 changes: 1 addition & 1 deletion solution/1100-1199/1120.Maximum Average Subtree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class Solution {
public:
double maximumAverageSubtree(TreeNode* root) {
double ans = 0;
function<pair<int, int>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<int, int> {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair<int, int> {
if (!root) {
return {0, 0};
}
Expand Down
4 changes: 2 additions & 2 deletions solution/1100-1199/1120.Maximum Average Subtree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tags:
<pre>
<strong>Input:</strong> root = [5,6,1]
<strong>Output:</strong> 6.00000
<strong>Explanation:</strong>
<strong>Explanation:</strong>
For the node with value = 5 we have an average of (5 + 6 + 1) / 3 = 4.
For the node with value = 6 we have an average of 6 / 1 = 6.
For the node with value = 1 we have an average of 1 / 1 = 1.
Expand Down Expand Up @@ -163,7 +163,7 @@ class Solution {
public:
double maximumAverageSubtree(TreeNode* root) {
double ans = 0;
function<pair<int, int>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<int, int> {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair<int, int> {
if (!root) {
return {0, 0};
}
Expand Down
4 changes: 2 additions & 2 deletions solution/1100-1199/1120.Maximum Average Subtree/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Solution {
public:
double maximumAverageSubtree(TreeNode* root) {
double ans = 0;
function<pair<int, int>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<int, int> {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair<int, int> {
if (!root) {
return {0, 0};
}
Expand All @@ -27,4 +27,4 @@ class Solution {
dfs(root);
return ans;
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,19 @@ public:
int maxSumBST(TreeNode* root) {
int ans = 0;
const int inf = 1 << 30;

function<vector<int>(TreeNode*)> dfs = [&](TreeNode* root) {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> array<int, 4> {
if (!root) {
return vector<int>{1, inf, -inf, 0};
return {1, inf, -inf, 0};
}
auto l = dfs(root->left);
auto r = dfs(root->right);
int v = root->val;
if (l[0] && r[0] && l[2] < v && v < r[1]) {
int s = l[3] + r[3] + v;
ans = max(ans, s);
return vector<int>{1, min(l[1], v), max(r[2], v), s};
return {1, min(l[1], v), max(r[2], v), s};
}
return vector<int>(4);
return {0};
};
dfs(root);
return ans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,19 @@ public:
int maxSumBST(TreeNode* root) {
int ans = 0;
const int inf = 1 << 30;

function<vector<int>(TreeNode*)> dfs = [&](TreeNode* root) {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> array<int, 4> {
if (!root) {
return vector<int>{1, inf, -inf, 0};
return {1, inf, -inf, 0};
}
auto l = dfs(root->left);
auto r = dfs(root->right);
int v = root->val;
if (l[0] && r[0] && l[2] < v && v < r[1]) {
int s = l[3] + r[3] + v;
ans = max(ans, s);
return vector<int>{1, min(l[1], v), max(r[2], v), s};
return {1, min(l[1], v), max(r[2], v), s};
}
return vector<int>(4);
return {0};
};
dfs(root);
return ans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ class Solution {
int maxSumBST(TreeNode* root) {
int ans = 0;
const int inf = 1 << 30;

function<vector<int>(TreeNode*)> dfs = [&](TreeNode* root) {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> array<int, 4> {
if (!root) {
return vector<int>{1, inf, -inf, 0};
return {1, inf, -inf, 0};
}
auto l = dfs(root->left);
auto r = dfs(root->right);
int v = root->val;
if (l[0] && r[0] && l[2] < v && v < r[1]) {
int s = l[3] + r[3] + v;
ans = max(ans, s);
return vector<int>{1, min(l[1], v), max(r[2], v), s};
return {1, min(l[1], v), max(r[2], v), s};
}
return vector<int>(4);
return {0};
};
dfs(root);
return ans;
}
};
};
2 changes: 1 addition & 1 deletion solution/1500-1599/1559.Detect Cycles in 2D Grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public:
int m = grid.size(), n = grid[0].size();
vector<vector<bool>> vis(m, vector<bool>(n));
const vector<int> dirs = {-1, 0, 1, 0, -1};
function<bool(int, int, int, int)> dfs = [&](int x, int y, int px, int py) {
auto dfs = [&](this auto&& dfs, int x, int y, int px, int py) -> bool {
vis[x][y] = true;
for (int k = 0; k < 4; ++k) {
int nx = x + dirs[k], ny = y + dirs[k + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public:
int m = grid.size(), n = grid[0].size();
vector<vector<bool>> vis(m, vector<bool>(n));
const vector<int> dirs = {-1, 0, 1, 0, -1};
function<bool(int, int, int, int)> dfs = [&](int x, int y, int px, int py) {
auto dfs = [&](this auto&& dfs, int x, int y, int px, int py) -> bool {
vis[x][y] = true;
for (int k = 0; k < 4; ++k) {
int nx = x + dirs[k], ny = y + dirs[k + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Solution {
int m = grid.size(), n = grid[0].size();
vector<vector<bool>> vis(m, vector<bool>(n));
const vector<int> dirs = {-1, 0, 1, 0, -1};
function<bool(int, int, int, int)> dfs = [&](int x, int y, int px, int py) {
auto dfs = [&](this auto&& dfs, int x, int y, int px, int py) -> bool {
vis[x][y] = true;
for (int k = 0; k < 4; ++k) {
int nx = x + dirs[k], ny = y + dirs[k + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,21 @@ class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
unordered_set<int> s;
for (auto node : nodes) s.insert(node->val);
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
if (!root || s.count(root->val)) return root;
for (auto node : nodes) {
s.insert(node->val);
}
auto dfs = [&](this auto&& dfs, TreeNode* root) -> TreeNode* {
if (!root || s.contains(root->val)) {
return root;
}
auto left = dfs(root->left);
auto right = dfs(root->right);
if (!left) return right;
if (!right) return left;
if (!left) {
return right;
}
if (!right) {
return left;
}
return root;
};
return dfs(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,21 @@ class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
unordered_set<int> s;
for (auto node : nodes) s.insert(node->val);
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
if (!root || s.count(root->val)) return root;
for (auto node : nodes) {
s.insert(node->val);
}
auto dfs = [&](this auto&& dfs, TreeNode* root) -> TreeNode* {
if (!root || s.contains(root->val)) {
return root;
}
auto left = dfs(root->left);
auto right = dfs(root->right);
if (!left) return right;
if (!right) return left;
if (!left) {
return right;
}
if (!right) {
return left;
}
return root;
};
return dfs(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
unordered_set<int> s;
for (auto node : nodes) s.insert(node->val);
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
if (!root || s.count(root->val)) return root;
for (auto node : nodes) {
s.insert(node->val);
}
auto dfs = [&](this auto&& dfs, TreeNode* root) -> TreeNode* {
if (!root || s.contains(root->val)) {
return root;
}
auto left = dfs(root->left);
auto right = dfs(root->right);
if (!left) return right;
if (!right) return left;
if (!left) {
return right;
}
if (!right) {
return left;
}
return root;
};
return dfs(root);
}
};
};
Loading