diff --git a/cpp/2304/230413.cpp b/cpp/2304/230413.cpp new file mode 100644 index 0000000..ea966f2 --- /dev/null +++ b/cpp/2304/230413.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +/** + * 946. Validate Stack Sequences + * + * Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise. + */ + +class Solution { +public: + static bool validateStackSequences(const std::vector&, const std::vector&); +}; + +bool Solution::validateStackSequences(const std::vector& i, const std::vector& o) { + std::bitset<1024> pushed; + std::stack s; + const int n = i.size(); + for (int pi = 0, po = 0; po < n; ++po, s.pop()) { + for (; !pushed[o[po]]; pushed[i[pi++]] = true) + s.push(i[pi]); + if (s.empty() || s.top() != o[po]) + return false; + } + return s.empty(); +} + +int main() { + Solution::validateStackSequences({1,2,3,4,5},{4,3,5,1,2}); + return 0; +} diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt index 49567ad..d1cefb5 100644 --- a/cpp/2304/CMakeLists.txt +++ b/cpp/2304/CMakeLists.txt @@ -4,4 +4,4 @@ PROJECT(2304) SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_EXPORT_COMPILE_COMMANDS true) -ADD_EXECUTABLE(2304 230413-CN.cpp) +ADD_EXECUTABLE(2304 230413.cpp)