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 23, 2024
1 parent 840b042 commit 03a22a4
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ add_task(minimum-equal-sum-of-two-arrays-after-replacing-zeros)
add_task(minimum-falling-path-sum)
add_task(minimum-flips-to-make-a-or-b-equal-to-c)
add_task(minimum-genetic-mutation)
add_task(minimum-height-trees)
add_task(minimum-hours-of-training-to-win-a-competition)
add_task(minimum-increment-operations-to-make-array-beautiful)
add_task(minimum-index-sum-of-two-lists)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@
| 306. | [Additive Number](https://leetcode.com/problems/additive-number/) | [C++](./solutions/additive-number/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>2</sup>) / O(N)| <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Backtracking-3d337f?style=flat-square'/> | |
| 307. | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [C++](./solutions/range-sum-query-mutable/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N+QlogN) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Design-51337f?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Indexed Tree-7f3359?style=flat-square'/> <img src='https://img.shields.io/badge/Segment Tree-7f3353?style=flat-square'/> | [FenwickTree](https://w.wiki/8Cy3), [SegmentTree](https://w.wiki/8Cy4) |
| 309. | [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | [C++](./solutions/best-time-to-buy-and-sell-stock-with-cooldown/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/Dynamic Programming-37337f?style=flat-square'/> | |
| 310. | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [C++](./solutions/minimum-height-trees/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/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/Graph-3d7f33?style=flat-square'/> <img src='https://img.shields.io/badge/Topological Sort-337f42?style=flat-square'/> | |
| 311. | [Sparse Matrix Multiplication 🔒](https://leetcode.com/problems/sparse-matrix-multiplication/) | [C++](./solutions/sparse-matrix-multiplication/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NMK) / O(MK+KN)| <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/Matrix-5e337f?style=flat-square'/> | |
| 312. | [Burst Balloons](https://leetcode.com/problems/burst-balloons/) | [C++](./solutions/burst-balloons/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N<sup>3</sup>) / O(N<sup>2</sup>)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> | |
| 313. | [Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | [C++](./solutions/super-ugly-number/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NK) / O(N+K)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?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 @@ -388,6 +388,7 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 306. | [Additive Number](https://leetcode.com/problems/additive-number/) | [C++](./solutions/additive-number/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>2</sup>) / O(N)| <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Backtracking-3d337f?style=flat-square'/> | |
| 307. | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [C++](./solutions/range-sum-query-mutable/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N+QlogN) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Design-51337f?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Indexed Tree-7f3359?style=flat-square'/> <img src='https://img.shields.io/badge/Segment Tree-7f3353?style=flat-square'/> | [FenwickTree](https://w.wiki/8Cy3), [SegmentTree](https://w.wiki/8Cy4) |
| 309. | [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | [C++](./solutions/best-time-to-buy-and-sell-stock-with-cooldown/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/Dynamic Programming-37337f?style=flat-square'/> | |
| 310. | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [C++](./solutions/minimum-height-trees/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/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/Graph-3d7f33?style=flat-square'/> <img src='https://img.shields.io/badge/Topological Sort-337f42?style=flat-square'/> | |
| 311. | [Sparse Matrix Multiplication 🔒](https://leetcode.com/problems/sparse-matrix-multiplication/) | [C++](./solutions/sparse-matrix-multiplication/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NMK) / O(MK+KN)| <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/Matrix-5e337f?style=flat-square'/> | |
| 312. | [Burst Balloons](https://leetcode.com/problems/burst-balloons/) | [C++](./solutions/burst-balloons/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N<sup>3</sup>) / O(N<sup>2</sup>)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> | |
| 313. | [Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | [C++](./solutions/super-ugly-number/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NK) / O(N+K)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> | |
Expand Down Expand Up @@ -1095,4 +1096,3 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 1378. | [Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier/) | [SQL](./extra/sql/replace-employee-id-with-the-unique-identifier/solution.sql), [PY](./extra/pandas/replace-employee-id-with-the-unique-identifier/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'/> | |
| 1379. | [Find a Corresponding Node of a Binary Tree in a Clone of That Tree](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/) | [C++](./solutions/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?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/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 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'/> | |
1 change: 1 addition & 0 deletions solutions/minimum-height-trees/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_minimum_height_trees test.cpp)
45 changes: 45 additions & 0 deletions solutions/minimum-height-trees/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <vector>

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

class Solution {
public:
static std::vector<int>
findMinHeightTrees(int n, const std::vector<std::vector<int>> &edges) {
if (n == 1) {
return {0};
}

std::vector<int> degree(n);
std::vector<std::vector<int>> adj(n);
for (const auto &edge : edges) {
const auto u = edge[0], v = edge[1];
++degree[u], ++degree[v];
adj[u].push_back(v), adj[v].push_back(u);
}

std::vector<int> nodes;
for (int u = 0; u < n; ++u) {
if (degree[u] == 1) {
nodes.push_back(u);
}
}

while (n > 2) {
n -= nodes.size();
std::vector<int> next;
for (auto u : nodes) {
for (auto v : adj[u]) {
if (--degree[v] == 1) {
next.push_back(v);
}
}
}
nodes.swap(next);
}
return nodes;
}
};
39 changes: 39 additions & 0 deletions solutions/minimum-height-trees/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
int n;
std::vector<std::vector<int>> edges;
std::vector<int> expected;
};

std::vector<TestCase> test_cases{
{
.n = 4,
.edges{{1, 0}, {1, 2}, {1, 3}},
.expected{1},
},
{
.n = 6,
.edges{{3, 0}, {3, 1}, {3, 2}, {3, 4}, {5, 4}},
.expected{3, 4},
},
{
.n = 1,
.edges{},
.expected{0},
},
{
.n = 3,
.edges{{0, 1}, {0, 2}},
.expected{0},
},
};

for (const auto &[n, edges, expected] : test_cases) {
const auto actual = Solution::findMinHeightTrees(n, edges);
REQUIRE(expected == actual);
}
}

0 comments on commit 03a22a4

Please sign in to comment.