题目 376 Wiggle Subsequence

& IPO
题目 376
Aofisaif theand . The first(if one ) may beor . Awith fewer than twoisa.
For , [1,7,4,9,2,5] is athe(6,-3,5,-7,3) areand . In , [1,4,7,2,5] and [1,7,4,5,5] are not, the firstits first twoareand theits lastis zero.
Given aof ,theof thethat is a. Aisbysomeof(, also zero) from the,thein theirorder.
解答
class Solution {public:int wiggleMaxLength(vector& nums) {int size=nums.size(), f=1, d=1;for(int i=1; i; ++i){if(nums[i]>nums[i-1]) f=d+1;else if(nums[i]
以上是高票答案 , 自己写的代码如下
class Solution {public:int wiggleMaxLength(vector& nums) {if(nums.size()<2){return nums.size();}int iMaxPos(1), iMaxNeg(1);for(int i=1; i 0) iMaxPos = iMaxNeg+1;}return max(iMaxNeg, iMaxPos);}};
题目 502 IPO

题目 376 Wiggle Subsequence

文章插图
Hard
will start its IPO soon. In order to sell a good price of itsto,would like to work on sometoitsthe IPO. Since it has, it can onlyat most kthe IPO. Helpthe best way toits totalafterat most k.
You are given. For eachi, it has a purePi and aof Ci isto start the. , you have W . When youa , you willits pureand thewill be added to your total .
To sum up, pick a list of at most kfrom giventoyour final , andyour final.
1:
Input: k=2, W=0, =[1,2,3], =[0,1,1].
【题目 376 Wiggle Subsequence】: 4
: Since youris 0, you can only start the0.
Afterit you will1 and your1.
With1, you canstart the1 or the2.
Since you canat most 2 , you need tothe2 to get the.
题目 376 Wiggle Subsequence

文章插图
,the final, which is 0 + 1 + 3 = 4.
Note:
You mayallin the input are non- .
Theofarray andarray will not50,000.
Theisto fit in a 32-bit.
解答
class Solution {public:int findMaximizedCapital(int k, int W, vector& Profits, vector& Capital) {priority_queue pqLowW;multiset > setUpW;for(int i=0; ifirst <= W); i=setUpW.erase(i))pqLowW.push(i->second);}return W;}};
附录高票答案:
int findMaximizedCapital(int k, int W, vector& P, vector& C) {priority_queue low;// P[i]'s within current Wmultiset> high; // (C[i],P[i])'s' outside current Wfor (int i = 0; i < P.size(); ++i) // initialize low and highif(P[i] > 0) if (C[i] <= W) low.push(P[i]); else high.emplace(C[i], P[i]);while (k-- && low.size()) { W += low.top(), low.pop(); // greedy to work on most profitable firstfor (auto i = high.begin(); high.size() && i->first <= W; i = high.erase(i)) low.push(i->second);}return W;}
分析
解答参考了高票答案 , 路漫漫其修远兮 。针对C++新的功能和容器 , 自己仍然缺乏灵活应用的经验 。套路不够多哦 。