add: 230413

This commit is contained in:
Eatswap 2023-04-13 12:06:34 +08:00
parent d77ba88558
commit 2deff47e80
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 33 additions and 1 deletions

32
cpp/2304/230413.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <bitset>
#include <vector>
#include <stack>
/**
* 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<int>&, const std::vector<int>&);
};
bool Solution::validateStackSequences(const std::vector<int>& i, const std::vector<int>& o) {
std::bitset<1024> pushed;
std::stack<int> 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;
}

View File

@ -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)