-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathFish.cpp
More file actions
50 lines (43 loc) · 1019 Bytes
/
Fish.cpp
File metadata and controls
50 lines (43 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 测试链接:https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/start/
#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int solution(vector<int> &A, vector<int> &B) {
// write your code in C++14 (g++ 6.2.0)
const int N = A.size();
if (N <= 1)
return N;
constexpr int L = 0;
constexpr int R = 1;
stack<int> t;
for (int i = 0; i < N; i++) {
const int D = B[i];
const int S = A[i];
bool has_eat = false;
while (!t.empty() && B[t.top()] == R && D == L) {
// 如果栈顶的鱼比较大,那么把新来的吃掉
if (A[t.top()] > S) {
has_eat = true;
break;
}
t.pop();
}
if (has_eat)
continue;
t.push(i);
}
return t.size();
}