Skip to content

Commit

Permalink
Even Odd Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Feb 29, 2024
1 parent 6f09a67 commit 64b42d9
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 6 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ add_task(evaluate-boolean-binary-tree)
add_task(evaluate-division)
add_task(evaluate-reverse-polish-notation)
add_task(evaluate-the-bracket-pairs-of-a-string)
add_task(even-odd-tree)
add_task(excel-sheet-column-number)
add_task(excel-sheet-column-title)
add_task(extra-characters-in-a-string)
Expand Down
5 changes: 3 additions & 2 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,7 @@
| 1605. | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [C++](./solutions/find-valid-matrix-given-row-and-column-sums/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(MN) / O(N+M)| <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/Matrix-5e337f?style=flat-square'/> | |
| 1607. | [Sellers With No Sales 🔒](https://leetcode.com/problems/sellers-with-no-sales/) | [SQL](./extra/sql/sellers-with-no-sales/solution.sql), [PY](./extra/pandas/sellers-with-no-sales/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'/> | |
| 1608. | [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/) | [C++](./solutions/special-array-with-x-elements-greater-than-or-equal-x/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/Binary Search-33557f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | |
| 1609. | [Even Odd Tree](https://leetcode.com/problems/even-odd-tree/) | [C++](./solutions/even-odd-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/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 1611. | [Minimum One Bit Operations to Make Integers Zero](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/) | [C++](./solutions/minimum-one-bit-operations-to-make-integers-zero/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(logN) / O(1)| <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Bit Manipulation-337f68?style=flat-square'/> <img src='https://img.shields.io/badge/Memoization-33357f?style=flat-square'/> | |
| 1613. | [Find the Missing IDs 🔒](https://leetcode.com/problems/find-the-missing-ids/) | [SQL](./extra/sql/find-the-missing-ids/solution.sql), [PY](./extra/pandas/find-the-missing-ids/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'/> | |
| 1614. | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/) | [C++](./solutions/maximum-nesting-depth-of-the-parentheses/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Stack-64337f?style=flat-square'/> | |
Expand Down Expand Up @@ -1792,7 +1793,7 @@
| 2926. | [Maximum Balanced Subsequence Sum](https://leetcode.com/problems/maximum-balanced-subsequence-sum/) | [C++](./solutions/maximum-balanced-subsequence-sum/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(NlogN) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Search-33557f?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?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'/> | |
| 2928. | [Distribute Candies Among Children I](https://leetcode.com/problems/distribute-candies-among-children-i/) | [C++](./solutions/distribute-candies-among-children-i/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(L<sup>2</sup>) / O(1)| <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Combinatorics-7f4633?style=flat-square'/> <img src='https://img.shields.io/badge/Enumeration-7f7a33?style=flat-square'/> | |
| 2929. | [Distribute Candies Among Children II](https://leetcode.com/problems/distribute-candies-among-children-ii/) | [C++](./solutions/distribute-candies-among-children-ii/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(1) / O(1)| <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Combinatorics-7f4633?style=flat-square'/> <img src='https://img.shields.io/badge/Enumeration-7f7a33?style=flat-square'/> | |
| 2930. | [Number of Strings Which Can Be Rearranged to Contain Substring](https://leetcode.com/problems/number-of-strings-which-can-be-rearranged-to-contain-substring/) | [C++](./solutions/number-of-strings-which-can-be-rearranged-to-contain-substring/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(logN) / O(1)| <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'/> <img src='https://img.shields.io/badge/Combinatorics-7f4633?style=flat-square'/> | [Inclusion–exclusion principle](https://w.wiki/Dna) |
| 2930. | [Number of Strings Which Can Be Rearranged to Contain Substring](https://leetcode.com/problems/number-of-strings-which-can-be-rearranged-to-contain-substring/) | [C++](./solutions/number-of-strings-which-can-be-rearranged-to-contain-substring/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(logN) / O(1)| <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'/> <img src='https://img.shields.io/badge/Combinatorics-7f4633?style=flat-square'/> | [Inclusion–exclusion principle](https://w.wiki/Dna) |
| 2931. | [Maximum Spending After Buying Items](https://leetcode.com/problems/maximum-spending-after-buying-items/) | [C++](./solutions/maximum-spending-after-buying-items/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(MNlogM) / O(M)| <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'/> | |
| 2932. | [Maximum Strong Pair XOR I](https://leetcode.com/problems/maximum-strong-pair-xor-i/) | [C++](./solutions/maximum-strong-pair-xor-i/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(N<sup>2</sup>) / O(1)| <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/Bit Manipulation-337f68?style=flat-square'/> <img src='https://img.shields.io/badge/Trie-7f333f?style=flat-square'/> <img src='https://img.shields.io/badge/Sliding Window-7f6033?style=flat-square'/> | |
| 2933. | [High-Access Employees](https://leetcode.com/problems/high-access-employees/) | [C++](./solutions/high-access-employees/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NlogN) / O(N)| <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/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | |
Expand All @@ -1816,7 +1817,7 @@
| 2956. | [Find Common Elements Between Two Arrays](https://leetcode.com/problems/find-common-elements-between-two-arrays/) | [C++](./solutions/find-common-elements-between-two-arrays/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(N+M) / O(N+M)| <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'/> | |
| 2957. | [Remove Adjacent Almost-Equal Characters](https://leetcode.com/problems/remove-adjacent-almost-equal-characters/) | [C++](./solutions/remove-adjacent-almost-equal-characters/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/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> | |
| 2958. | [Length of Longest Subarray With at Most K Frequency](https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/) | [C++](./solutions/length-of-longest-subarray-with-at-most-k-frequency/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/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/Sliding Window-7f6033?style=flat-square'/> | |
| 2959. | [Number of Possible Sets of Closing Branches](https://leetcode.com/problems/number-of-possible-sets-of-closing-branches/) | [C++](./solutions/number-of-possible-sets-of-closing-branches/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(2<sup>N</sup>N<sup>3</sup>) / O(N<sup>2</sup>)| <img src='https://img.shields.io/badge/Bit Manipulation-337f68?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> <img src='https://img.shields.io/badge/Enumeration-7f7a33?style=flat-square'/> <img src='https://img.shields.io/badge/Shortest Path-337f55?style=flat-square'/> | [Floyd–Warshall](https://w.wiki/4xCV) |
| 2959. | [Number of Possible Sets of Closing Branches](https://leetcode.com/problems/number-of-possible-sets-of-closing-branches/) | [C++](./solutions/number-of-possible-sets-of-closing-branches/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(2<sup>N</sup>N<sup>3</sup>) / O(N<sup>2</sup>)| <img src='https://img.shields.io/badge/Bit Manipulation-337f68?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> <img src='https://img.shields.io/badge/Enumeration-7f7a33?style=flat-square'/> <img src='https://img.shields.io/badge/Shortest Path-337f55?style=flat-square'/> | [Floyd–Warshall](https://w.wiki/4xCV) |
| 2960. | [Count Tested Devices After Test Operations](https://leetcode.com/problems/count-tested-devices-after-test-operations/) | [C++](./solutions/count-tested-devices-after-test-operations/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?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/Simulation-7e7f33?style=flat-square'/> | |
| 2961. | [Double Modular Exponentiation](https://leetcode.com/problems/double-modular-exponentiation/) | [C++](./solutions/double-modular-exponentiation/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NlogM) / O(1)| <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/Simulation-7e7f33?style=flat-square'/> | |
| 2962. | [Count Subarrays Where Max Element Appears at Least K Times](https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/) | [C++](./solutions/count-subarrays-where-max-element-appears-at-least-k-times/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/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sliding Window-7f6033?style=flat-square'/> | |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# LeetCode

LeetCode С++ solutions with VS Code.
LeetCode C++ solutions with VS Code.

<img width="50%" src="https://leetcard.jacoblin.cool/morev?theme=wtf&ext=contest">

Expand Down
4 changes: 2 additions & 2 deletions leetcode/leetcode/templates/readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ def update_readme_table() -> None:
with open(README_FILE, mode="r") as file:
lines = file.readlines()

with open(README_FILE, mode="w") as readme_file, open(
PROBLEM_LIST_FILE, mode="w"
with open(README_FILE, mode="w", encoding="utf-8") as readme_file, open(
PROBLEM_LIST_FILE, mode="w", encoding="utf-8"
) as list_file:
for line in lines:
readme_file.write(line)
Expand Down
2 changes: 1 addition & 1 deletion leetcode/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "leetcode"
version = "0.0.8"
version = "0.0.9"
authors = [
{name = "Alexandr Morev", email = "[email protected]"},
]
Expand Down
1 change: 1 addition & 0 deletions solutions/even-odd-tree/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_even_odd_tree test.cpp)
40 changes: 40 additions & 0 deletions solutions/even-odd-tree/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <ranges>
#include <vector>

#include <tree_node.h>

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

class Solution {
public:
static bool isEvenOddTree(TreeNode *root) {
std::vector<TreeNode *> nodes{root};
for (int level = 0; !nodes.empty(); ++level) {
if (std::ranges::any_of(nodes, [=](TreeNode *node) {
return (node->val & 1) == (level & 1);
})) {
return false;
}

if (!std::ranges::is_sorted(nodes, [=](TreeNode *lhs, TreeNode *rhs) {
return level & 1 ? lhs->val >= rhs->val : lhs->val <= rhs->val;
})) {
return false;
}

std::vector<TreeNode *> next;
for (auto *node : nodes) {
for (auto *child : {node->left, node->right}) {
if (child) {
next.push_back(child);
}
}
}
nodes.swap(next);
}
return true;
}
};
37 changes: 37 additions & 0 deletions solutions/even-odd-tree/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <catch.hpp>

#include <solution.hpp>

#include <tree_node.h>

TEST_CASE("Simple") {
struct TestCase {
Tree root;
bool expected;
};

std::vector<TestCase> test_cases{
{
.root = {1, 10, 4, 3, std::nullopt, 7, 9, 12, 8, 6, std::nullopt,
std::nullopt, 2},
.expected = true,
},
{
.root = {5, 4, 2, 3, 3, 7},
.expected = false,
},
{
.root = {5, 9, 1, 3, 5, 7},
.expected = false,
},
{
.root = {2, 12, 8, 5, 9, std::nullopt, std::nullopt, 18, 16},
.expected = false,
},
};

for (const auto &[root, expected] : test_cases) {
const auto actual = Solution::isEvenOddTree(root);
REQUIRE(expected == actual);
}
}

0 comments on commit 64b42d9

Please sign in to comment.