leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 57c5f7030c9da7fa303bdbff777c2bc433641256
parent 3435beb8a91a4268a04a1a21b8df67cba35a3ff8
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 13 Apr 2023 14:56:59 +0200

Daily Problem

Diffstat:
AProblems/0946.cpp | 26++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/Problems/0946.cpp b/Problems/0946.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + bool validateStackSequences(vector<int> &pushed, vector<int> &popped) { + int n = pushed.size(), m = popped.size(); + int i = 0, j = 0; + stack<int> st; + + while (i < n || j < m) { + if (st.empty()) { + if (i < n) + st.push(pushed[i++]); + else + return false; + } else { + if (st.top() == popped[j]) + st.pop(), j++; + else if (i < n) + st.push(pushed[i++]); + else + return false; + } + } + + return true; + } +}; diff --git a/README.md b/README.md @@ -362,6 +362,7 @@ for solving problems. | 0938 | Easy | [Range Sum of BST](Problems/0938.cpp) | | 0941 | Easy | [Valid Mountain Array](Problems/0941.cpp) | | 0944 | Easy | [Delete Columns to Make Sorted](Problems/0944.cpp) | +| 0946 | Medium | [Validate Stack Sequences](Problems/0946.cpp) | | 0947 | Medium | [Most Stones Removed with Same Row or Column](Problems/0947.cpp) | | 0950 | Medium | [Reveal Cards In Increasing Order](Problems/0950.cpp) | | 0953 | Easy | [Verifying an Alien Dictionary](Problems/0953.cpp) |