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 7, 2024
1 parent c3bd14c commit 3a991c0
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,7 @@ add_task(valid-palindrome)
add_task(valid-palindrome-ii)
add_task(valid-palindrome-iv)
add_task(valid-parentheses)
add_task(valid-parenthesis-string)
add_task(valid-perfect-square)
add_task(valid-sudoku)
add_task(valid-triangle-number)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@
| 671. | [Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [C++](./solutions/second-minimum-node-in-a-binary-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/Binary Tree-7f336d?style=flat-square'/> | |
| 673. | [Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/) | [C++](./solutions/number-of-longest-increasing-subsequence/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'/> <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'/> | |
| 674. | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [C++](./solutions/longest-continuous-increasing-subsequence/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'/> | |
| 678. | [Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/) | [C++](./solutions/valid-parenthesis-string/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/Stack-64337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> | |
| 680. | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [C++](./solutions/valid-palindrome-ii/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Two Pointers-7f6633?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> | |
| 682. | [Baseball Game](https://leetcode.com/problems/baseball-game/) | [C++](./solutions/baseball-game/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/Stack-64337f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 684. | [Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [C++](./solutions/redundant-connection/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/Union Find-337f3b?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?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 @@ -656,6 +656,7 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 671. | [Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [C++](./solutions/second-minimum-node-in-a-binary-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/Binary Tree-7f336d?style=flat-square'/> | |
| 673. | [Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/) | [C++](./solutions/number-of-longest-increasing-subsequence/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'/> <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'/> | |
| 674. | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [C++](./solutions/longest-continuous-increasing-subsequence/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'/> | |
| 678. | [Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/) | [C++](./solutions/valid-parenthesis-string/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/Stack-64337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> | |
| 680. | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [C++](./solutions/valid-palindrome-ii/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Two Pointers-7f6633?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> | |
| 682. | [Baseball Game](https://leetcode.com/problems/baseball-game/) | [C++](./solutions/baseball-game/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/Stack-64337f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 684. | [Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [C++](./solutions/redundant-connection/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/Union Find-337f3b?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> | |
Expand Down Expand Up @@ -1095,4 +1096,3 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 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'/> | |
| 1385. | [Find the Distance Value Between Two Arrays](https://leetcode.com/problems/find-the-distance-value-between-two-arrays/) | [C++](./solutions/find-the-distance-value-between-two-arrays/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/Two Pointers-7f6633?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'/> | |
1 change: 1 addition & 0 deletions solutions/valid-parenthesis-string/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_valid_parenthesis_string test.cpp)
75 changes: 75 additions & 0 deletions solutions/valid-parenthesis-string/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

#include <stack>
#include <string>

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

namespace stack {

// Time: O(N)
// Space: O(N)
class Solution {
public:
static bool checkValidString(std::string s) {
std::stack<int> openBrackets, asterisks;
for (int i = 0; i < std::ssize(s); ++i) {
if (s[i] == '(') {
openBrackets.push(i);
} else if (s[i] == '*') {
asterisks.push(i);
} else if (s[i] == ')') {
if (!openBrackets.empty()) {
openBrackets.pop();
} else if (!asterisks.empty()) {
asterisks.pop();
} else {
return false;
}
} else {
throw;
}
}

while (!openBrackets.empty() && !asterisks.empty()) {
if (asterisks.top() < openBrackets.top()) {
return false;
}
openBrackets.pop();
asterisks.pop();
}
return openBrackets.empty();
}
};

} // namespace stack

namespace opt {

// Time: O(N)
// Space: O(1)
class Solution {
public:
static bool checkValidString(std::string s) {
int open = 0, close = 0;
for (auto c : s) {
if (c == '(') {
++open, ++close;
} else if (c == ')') {
--open, --close;
} else if (c == '*') {
--open, ++close;
} else {
throw;
}
open = std::max(0, open);
if (close < 0) {
return false;
}
}
return open == 0;
}
};

} // namespace opt
39 changes: 39 additions & 0 deletions solutions/valid-parenthesis-string/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 {
std::string s;
bool expected;
};

std::vector<TestCase> test_cases{
{
.s = "()",
.expected = true,
},
{
.s = "(*)",
.expected = true,
},
{
.s = "(*))",
.expected = true,
},
};

SECTION("STACK") {
for (const auto &[s, expected] : test_cases) {
const auto actual = stack::Solution::checkValidString(s);
REQUIRE(expected == actual);
}
}

SECTION("MEMORY OPTIMIZED") {
for (const auto &[s, expected] : test_cases) {
const auto actual = opt::Solution::checkValidString(s);
REQUIRE(expected == actual);
}
}
}

0 comments on commit 3a991c0

Please sign in to comment.