-
Notifications
You must be signed in to change notification settings - Fork 2
/
SubSequence.cpp
140 lines (121 loc) · 4.12 KB
/
SubSequence.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//Copyright [2018] [Haibo Yan]
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//
// Created by haibo on 3/27/18.
//
#include "SubSequence.h"
#include <algorithm>
#include <iostream>
vector<int> SubSequence::longestCommon(vector<int> &other) {
int dp[nums.size() + 1][other.size() + 1];
for (int i = 1; i <= other.size(); i++) {
dp[0][i] = 0;
}
for (int i = 1; i <= nums.size(); i++) {
dp[i][0] = 0;
}
for (int i = 1; i <= nums.size(); i++) {
for (int j = 1; j <= other.size(); j++) {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if (nums[i] == other[j]) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
}
}
}
vector<int> commons;
int i = nums.size(), j = other.size();
while (i > 0 && j > 0) {
if (dp[i][j] == dp[i - 1][j]) {
i--;
} else if (dp[i][j] == dp[i][j - 1]) {
j--;
} else {
commons.push_back(nums[i - 1]);
i--;
j--;
}
}
reverse(commons.begin(), commons.end());
return commons;
}
vector<int> SubSequence::longestIncreasing() {
vector<int> l;
}
/**
* see https://leetcode.com/problems/wiggle-subsequence
* A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate
* between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence
* with fewer than two elements is trivially a wiggle sequence.
*
*For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and
* negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two
* differences are positive and the second because its last difference is zero.
*
*Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence
* is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the
* remaining elements in their original order.
*
*Examples:
*Input: [1,7,4,9,2,5]
*Output: 6
*The entire sequence is a wiggle sequence.
*
*Input: [1,17,5,10,13,15,10,5,16,8]
*Output: 7
*There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
*
*Input: [1,2,3,4,5,6,7,8,9]
*Output: 2
*Follow up:
*Can you do it in O(n) time?
*/
int SubSequence::wiggleMaxLength() {
if (nums.size() == 0)
return 0;
vector<int> asc;
vector<int> desc;
asc.push_back(nums[0]);
desc.push_back(nums[0]);
cout << endl;
for (int i = 1; i < nums.size(); i++) {
int v = nums[i];
int ascl = asc.size();
int descl = desc.size();
if (v != asc[ascl - 1]) {
if (ascl == 1 && v > asc[ascl - 1]) {
asc.push_back(v);
} else if (ascl > 1) {
int wiggle = (asc[ascl - 1] - asc[ascl - 2]) ^ (v - asc[ascl - 1]);
if (wiggle < 0) {
asc.push_back(v);
} else {
asc[ascl - 1] = v;
}
}
}
if (v != desc[descl - 1]) {
if (descl == 1 && v < desc[descl - 1]) {
desc.push_back(v);
} else if (descl > 1) {
int wiggle = (desc[descl - 1] - desc[descl - 2]) ^ (v - desc[descl - 1]) ;
if (wiggle < 0) {
desc.push_back(v);
} else {
desc[descl - 1] = v;
}
}
}
}
return max(asc.size(), desc.size());
}