贪心:Wiggle Subsequence 摇摆序列
生活随笔
收集整理的這篇文章主要介紹了
贪心:Wiggle Subsequence 摇摆序列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個整數序列,如果兩個相鄰元素的差恰好正負(負正)交替出現,則該序列被稱為
搖擺序列。一個小于2個元素的序列直接為搖擺序列。給一個隨機序列,求這個序列滿足搖擺序列定義的最長子序列的長度:
輸入[1,17,5,10,13,15,10,5,16,8],結果為7([1,17,10,13,10,16,8] )
序列 [1, 7, 4, 9, 2, 5],相鄰元素的差 (6, -3, 5, -7, 3),則為搖擺序列,結果為6
序列 [1,4,7,2,5] (3, 3, -5, 3)、 [1,7,4,5,5] (6, -3, 1, 0)不是搖擺序列,結果為2
根據貪心算法的核心:
- 尋找數據規律
- 使用最優的迭代策略
分析:
- 規律
很明顯,搖擺序列的判定并不支持存在連續三個或者更多元素是遞增或者遞減的情況;需要連續三個元素中處于中間的元素要么比兩邊的元素小,要么比兩邊的元素大 - 迭代策略
當出現連續三個或者更多元素是遞增/遞減的情況時,選擇最優(最后一個遞增/遞減的元素作為可以假如搖擺序列的轉折點,因為最后一個元素最大/最小,相比與下一個元素,有更大的可能使得其也滿足搖擺序列)
比如[1,17,5,10,13,15,10,5,16,8] 中 [10,13,15] 三個連續遞增,那么選擇15則會使得下一個10也能夠滿足成為搖擺序列中的元素
實現算法(文末有完整測試代碼):方法一
/*if..else if .. else */
int get_swing_seq(vector<int> &seq) {if(seq.size() <= 1) {return seq.size();}int max_len = 1;int i;for (i = 1; i < seq.size()-1; ++i) {if(seq[i-1] > seq[i] && seq[i] < seq[i+1]) { //滿足遞減轉折時max_len ++;} else if(seq[i-1] < seq[i] && seq[i] > seq[i+1]) {//滿足遞增轉折時max_len ++;}}/*handle the last element*/if(seq[i-2] > seq[i-1] && seq[i-1] < seq[i]) {max_len ++;} else if(seq[i-2] < seq[i-1] && seq[i-1] > seq[i]) {max_len ++;}return max_len;
}
實現算法:方法二
狀態機的方式:
/*state machine: BEGIN,UP,DOWN*/
int get_swing_seq2(vector<int> &seq) {if(seq.size() <= 1) {return seq.size();}static const int BEGIN = 0;static const int UP = 1;static const int DOWN = 2;int STATE = BEGIN;int max_len = 1;for (int i = 1;i < seq.size(); ++i) {switch (STATE){case BEGIN:if(seq[i-1] > seq[i]) {STATE = DOWN;max_len ++;} else if (seq[i-1] < seq[i]) {STATE = UP;max_len ++;}break;case UP:if(seq[i-1] > seq[i]) {STATE = DOWN;max_len ++;}break;case DOWN:if(seq[i-1] < seq[i]) {STATE = UP;max_len ++;}break;default:break;}}return max_len;
}
實現算法:打印搖擺序列
/*if..else if .. else; return the result seq */
vector<int> get_swing_seq3(vector<int> &seq) {if(seq.size() <= 1) {return seq;}vector<int> seq_result;int i;seq_result.push_back(seq[0]);for (i = 1; i < seq.size()-1; ++i) {if(seq[i-1] > seq[i] && seq[i] < seq[i+1]) {seq_result.push_back(seq[i]);} else if(seq[i-1] < seq[i] && seq[i] > seq[i+1]) {seq_result.push_back(seq[i]);}}/*handle the last element*/if(seq[i-2] > seq[i-1] && seq[i-1] < seq[i]) {seq_result.push_back(seq[i]);} else if(seq[i-2] < seq[i-1] && seq[i-1] > seq[i]) {seq_result.push_back(seq[i]);}return seq_result;
}
測試代碼:
#include <iostream>
#include <algorithm>
#include <vector>using namespace std;/*if..else if .. else */
int get_swing_seq(vector<int> &seq) {if(seq.size() <= 1) {return seq.size();}int max_len = 1;int i;for (i = 1; i < seq.size()-1; ++i) {if(seq[i-1] > seq[i] && seq[i] < seq[i+1]) {max_len ++;} else if(seq[i-1] < seq[i] && seq[i] > seq[i+1]) {max_len ++;}}/*handle the last element*/if(seq[i-2] > seq[i-1] && seq[i-1] < seq[i]) {max_len ++;} else if(seq[i-2] < seq[i-1] && seq[i-1] > seq[i]) {max_len ++;}return max_len;
}/*if..else if .. else; return the result seq */
vector<int> get_swing_seq3(vector<int> &seq) {if(seq.size() <= 1) {return seq;}vector<int> seq_result;int i;seq_result.push_back(seq[0]);for (i = 1; i < seq.size()-1; ++i) {if(seq[i-1] > seq[i] && seq[i] < seq[i+1]) {seq_result.push_back(seq[i]);} else if(seq[i-1] < seq[i] && seq[i] > seq[i+1]) {seq_result.push_back(seq[i]);}}/*handle the last element*/if(seq[i-2] > seq[i-1] && seq[i-1] < seq[i]) {seq_result.push_back(seq[i]);} else if(seq[i-2] < seq[i-1] && seq[i-1] > seq[i]) {seq_result.push_back(seq[i]);}return seq_result;
}/*state machine: BEGIN,UP,DOWN*/
int get_swing_seq2(vector<int> &seq) {if(seq.size() <= 1) {return seq.size();}static const int BEGIN = 0;static const int UP = 1;static const int DOWN = 2;int STATE = BEGIN;int max_len = 1;for (int i = 1;i < seq.size(); ++i) {switch (STATE){case BEGIN:if(seq[i-1] > seq[i]) {STATE = DOWN;max_len ++;} else if (seq[i-1] < seq[i]) {STATE = UP;max_len ++;}break;case UP:if(seq[i-1] > seq[i]) {STATE = DOWN;max_len ++;}break;case DOWN:if(seq[i-1] < seq[i]) {STATE = UP;max_len ++;}break;default:break;}}return max_len;
}int main(){vector<int> seq;int tmp;cout << "input seq " << endl;for (int i = 0;i < 10; ++i) {cin >> tmp;seq.push_back(tmp);}cout << "max of the swing seq len is " << get_swing_seq2(seq) << endl;vector<int> result = get_swing_seq3(seq);cout << "max of the swing seq is ";for (int i = 0;i < result.size(); ++i){cout << result[i] << " ";}return 0;
}
輸出如下:
input seq
1 17 5 10 13 15 10 5 16 8
max of the swing seq len is 7
max of the swing seq is 1 17 5 15 5 16 8
總結
以上是生活随笔為你收集整理的贪心:Wiggle Subsequence 摇摆序列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 女性不孕症宫腔镜怎么办
- 下一篇: 复仇者联盟4终局之战开头出现的女人是谁