-
Notifications
You must be signed in to change notification settings - Fork 0
/
1567.cpp
70 lines (62 loc) · 1.03 KB
/
1567.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int dp[100000][2];
int getMaxLen(vector<int>& nums) {
int result;
if (nums[0] > 0) {
result = 1;
dp[0][0] = 0;
dp[0][1] = 1;
}
else {
result = 0;
if (nums[0] == 0) {
dp[0][0] = 0;
}
else {
dp[0][0] = 1;
}
dp[0][1] = 0;
}
for (int i = 1; i < nums.size(); i++)
{
if (nums[i] == 0) {
dp[i][0] = 0;
dp[i][1] = 0;
}
else if (nums[i] > 0) {
// 양수일 경우
if (dp[i - 1][0] == 0) {
dp[i][0] = 0;
}
else {
dp[i][0] = dp[i - 1][0] + 1;
}
dp[i][1] = dp[i - 1][1] + 1;
}
else {
// 음수일 경우
if (dp[i - 1][0] == 0) {
dp[i][1] = 0;
}
else {
dp[i][1] = dp[i - 1][0] + 1;
}
dp[i][0] = dp[i - 1][1] + 1;
}
if (result < dp[i][1]) {
result = dp[i][1];
}
}
return result;
}
};
int main(void) {
Solution sol = Solution();
vector<int> input = { -16,0,-5,2,2,-13,11,8 };
sol.getMaxLen(input);
return 0;
}