Skip to content

Commit

Permalink
Solve Daily Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Apr 16, 2024
1 parent 3a991c0 commit bd4d3a5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_task(accounts-merge)
add_task(add-binary)
add_task(add-bold-tag-in-string)
add_task(add-digits)
add_task(add-one-row-to-tree)
add_task(add-strings)
add_task(add-to-array-form-of-integer)
add_task(add-two-integers)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@
| 620. | [Not Boring Movies](https://leetcode.com/problems/not-boring-movies/) | [SQL](./extra/sql/not-boring-movies/solution.sql), [PY](./extra/pandas/not-boring-movies/solution.py) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 621. | [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | [C++](./solutions/task-scheduler/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> <img src='https://img.shields.io/badge/Counting-7f7333?style=flat-square'/> | |
| 622. | [Design Circular Queue](https://leetcode.com/problems/design-circular-queue/) | [C++](./solutions/design-circular-queue/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Linked List-77337f?style=flat-square'/> <img src='https://img.shields.io/badge/Design-51337f?style=flat-square'/> <img src='https://img.shields.io/badge/Queue-6b337f?style=flat-square'/> | |
| 623. | [Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | [C++](./solutions/add-one-row-to-tree/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 624. | [Maximum Distance in Arrays 🔒](https://leetcode.com/problems/maximum-distance-in-arrays/) | [C++](./solutions/maximum-distance-in-arrays/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(1)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> | |
| 626. | [Exchange Seats](https://leetcode.com/problems/exchange-seats/) | [SQL](./extra/sql/exchange-seats/solution.sql), [PY](./extra/pandas/exchange-seats/solution.py) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 627. | [Swap Salary](https://leetcode.com/problems/swap-salary/) | [SQL](./extra/sql/swap-salary/solution.sql), [PY](./extra/pandas/swap-salary/solution.py) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 620. | [Not Boring Movies](https://leetcode.com/problems/not-boring-movies/) | [SQL](./extra/sql/not-boring-movies/solution.sql), [PY](./extra/pandas/not-boring-movies/solution.py) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 621. | [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | [C++](./solutions/task-scheduler/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> <img src='https://img.shields.io/badge/Counting-7f7333?style=flat-square'/> | |
| 622. | [Design Circular Queue](https://leetcode.com/problems/design-circular-queue/) | [C++](./solutions/design-circular-queue/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Linked List-77337f?style=flat-square'/> <img src='https://img.shields.io/badge/Design-51337f?style=flat-square'/> <img src='https://img.shields.io/badge/Queue-6b337f?style=flat-square'/> | |
| 623. | [Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | [C++](./solutions/add-one-row-to-tree/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 624. | [Maximum Distance in Arrays 🔒](https://leetcode.com/problems/maximum-distance-in-arrays/) | [C++](./solutions/maximum-distance-in-arrays/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(1)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> | |
| 626. | [Exchange Seats](https://leetcode.com/problems/exchange-seats/) | [SQL](./extra/sql/exchange-seats/solution.sql), [PY](./extra/pandas/exchange-seats/solution.py) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 627. | [Swap Salary](https://leetcode.com/problems/swap-salary/) | [SQL](./extra/sql/swap-salary/solution.sql), [PY](./extra/pandas/swap-salary/solution.py) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
Expand Down Expand Up @@ -1095,4 +1096,3 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 1380. | [Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix/) | [C++](./solutions/lucky-numbers-in-a-matrix/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Matrix-5e337f?style=flat-square'/> | |
| 1381. | [Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/) | [C++](./solutions/design-a-stack-with-increment-operation/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(Q) / O(Q)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Stack-64337f?style=flat-square'/> <img src='https://img.shields.io/badge/Design-51337f?style=flat-square'/> | |
| 1382. | [Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree/) | [C++](./solutions/balance-a-binary-search-tree/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Divide and Conquer-717f33?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Search Tree-7f3366?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 1383. | [Maximum Performance of a Team](https://leetcode.com/problems/maximum-performance-of-a-team/) | [C++](./solutions/maximum-performance-of-a-team/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> | |
1 change: 1 addition & 0 deletions solutions/add-one-row-to-tree/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_add_one_row_to_tree test.cpp)
23 changes: 23 additions & 0 deletions solutions/add-one-row-to-tree/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <tree_node.h>

// Time: O(N)
// Space: O(N)

class Solution {
public:
static TreeNode *addOneRow(TreeNode *root, int val, int depth,
bool left = true) {
if (depth == 1) {
return left ? new TreeNode(val, root, nullptr)
: new TreeNode(val, nullptr, root);
}
if (!root) {
return nullptr;
}
root->left = addOneRow(root->left, val, depth - 1, true);
root->right = addOneRow(root->right, val, depth - 1, false);
return root;
}
};
48 changes: 48 additions & 0 deletions solutions/add-one-row-to-tree/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <catch.hpp>

#include <solution.hpp>

#include <tree_node.h>

TEST_CASE("Simple") {
struct TestCase {
Tree root;
int val;
int depth;
Tree expected;
};

std::vector<TestCase> test_cases{
{
.root = {4, 2, 6, 3, 1, 5},
.val = 1,
.depth = 2,
.expected = {4, 1, 1, 2, std::nullopt, std::nullopt, 6, 3, 1, 5},
},
{
.root = {4, 2, std::nullopt, 3, 1},
.val = 1,
.depth = 3,
.expected = {4, 2, std::nullopt, 1, 1, 3, std::nullopt, std::nullopt,
1},
},
{
.root = {5},
.val = 1,
.depth = 1,
.expected = {1, 5},
},
{
.root = {1, 2, 3, 4},
.val = 5,
.depth = 4,
.expected = {1, 2, 3, 4, std::nullopt, std::nullopt, std::nullopt, 5,
5},
},
};

for (const auto &[root, val, depth, expected] : test_cases) {
const Tree actual = Solution::addOneRow(Copy(root), val, depth);
REQUIRE(expected == actual);
}
}

0 comments on commit bd4d3a5

Please sign in to comment.