commit 7315df8a528f60b18edc41f7163c05d4f4b7800a parent 9c035c488707d7eeb0f3df4c72197a8392f1346d Author: Dimitrije Dobrota <mail@dimitrijedobrota.com> Date: Fri, 19 Apr 2024 21:42:38 +0200 1 Random Problem Diffstat:
A | Problems/1541.cpp | | | 29 | +++++++++++++++++++++++++++++ |
M | README.md | | | 1 | + |
2 files changed, 30 insertions(+), 0 deletions(-)
diff --git a/Problems/1541.cpp b/Problems/1541.cpp @@ -0,0 +1,29 @@ +class Solution { + public: + int minInsertions(string s) const { + const int n = size(s); + int res = 0, cnt = 0; + + s.push_back(' '); + for (int i = 0; i < n; i++) { + if (s[i] == '(') + cnt++; + else { + if (cnt) { + if (s[i + 1] == ')') + i++; + else + res++; + cnt--; + } else { + if (s[i + 1] == ')') + res++, i++; + else + res += 2; + } + } + } + + return res + cnt * 2; + } +}; diff --git a/README.md b/README.md @@ -828,6 +828,7 @@ for solving problems. | 1531 | Hard | [String Compression II](Problems/1531.cpp) | | 1535 | Medium | [Find the Winner of an Array Game](Problems/1535.cpp) | | 1539 | Easy | [Kth Missing Positive Number](Problems/1539.cpp) | +| 1541 | Medium | [Minimum Insertions to Balance a Parentheses String](Problems/1541.cpp) | | 1544 | Easy | [Make The String Great](Problems/1544.cpp) | | 1545 | Medium | [Find Kth Bit in Nth Binary String](Problems/1545.cpp) | | 1547 | Hard | [Minimum Cost to Cut a Stick](Problems/1547.cpp) |