首页 > 其他分享 >376. 摆动序列c

376. 摆动序列c

时间:2024-03-10 17:23:57浏览次数:18  
标签:numsSize return int max 序列 摆动 dp 376

动态规划yyds!虽然写不出来TT

int max(int i,int j){
    if(i>j) return i;
    return j;
}

int wiggleMaxLength(int* nums, int numsSize){
    int dp[1000][2]={0};//dp[i][j] 表示到0-i为止的最大子序列,1表示最后是上升,0表示最后是下降
    if(numsSize==1) return 1;
    dp[0][0]=1;
    dp[0][1]=1;
    for(int i=1;i<numsSize;i++){
        if(nums[i]==nums[i-1]){
            dp[i][0]=dp[i-1][0];
            dp[i][1]=dp[i-1][1];
        }else if(nums[i] >nums[i-1]){
            dp[i][1]=dp[i-1][0]+1;
            dp[i][0]=dp[i-1][0];
        }else{
            dp[i][0]=dp[i-1][1]+1;
            dp[i][1]=dp[i-1][1];
        }
    }
    return max(dp[numsSize-1][0],dp[numsSize-1][1]);    
}

结果:

标签:numsSize,return,int,max,序列,摆动,dp,376
From: https://www.cnblogs.com/llllmz/p/18064425

相关文章

  • abc344E 维护元素唯一的序列
    给定序列A[N],元素值各不相同,有Q个操作,格式如下:1xy:在元素x后面插入元素y,保证插入时x唯一。2x:将元素x从序列中删除,保证删除时x唯一。输出所有操作完成后的序列。1<=N,Q<=2E5;1<=A[i]<=1E9;A[i]!=A[j]用链表来快速插入和删除,另外还需要map来快速定位,类似LRU的实现。......
  • 491. 非递减子序列c
    复试的人真的搞心态啊,怎么能这么变态,刷题这么块。哭了。要是难一点的重复问题还是写for循环好点。/***Returnanarrayofarraysofsize*returnSize.*Thesizesofthearraysarereturnedas*returnColumnSizesarray.*Note:Bothreturnedarrayand*columnSi......
  • 【力扣】非递减子序列
    题目代码如下:classSolution{public:vector<vector<int>>res;vector<int>path;booloccured(vector<int>&nums,intkey,intstartindex){for(inti=startindex;i<key;i++){if(nums[key]==nums[i]){......
  • php xss 反序列化漏洞
    介绍反序列化漏洞,利用了后端服务的设计缺陷序列化和反序列化对象=(序列化)》字符串对象《(反序列化)=字符串序列化字符串构建首先,理解下对象序列化后是个嘛玩意儿上代码<?phpclasstest{public$a='hello';private$b='hello';protected$c='h......
  • day58 动态规划part15 代码随想录算法训练营 392. 判断子序列
    题目:392.判断子序列我的感悟:理解难点:听课笔记:我的代码:通过截图:代码易错点:老师代码:扩展写法-双指针:classSolution:defisSubsequence(self,s:str,t:str)->bool:#初始化两个指针,分别指向s和t的第一个字符i,j=0,0#......
  • LeetCodeHot100 1.两数之和 46.字母异位词分组 128.最长连续序列
    1.两数之和https://leetcode.cn/problems/two-sum/description/?envType=study-plan-v2&envId=top-100-likedpublicint[]twoSum(int[]nums,inttarget){HashMap<Integer,Integer>map=newHashMap<>();for(inti=0;i<nums.l......
  • spring-JSON序列化
    1,使用场景。SpringBoot默认json为JackJson。在Controller需要返回Json数据时,我们使用了RestController,如果想对返回的数据进行一定的处理,也就是序列化对象为Json时使用。反序列化,就是当接收的参数想做一定处理,获取到处理后的数据时候。2,JsonSerializer序列化1,自定......
  • day57 动态规划part14 代码随想录算法训练营 1143. 最长公共子序列
    题目:1143.最长公共子序列我的感悟:你永远不知道自己有多厉害!加油!理解难点:递推公式如何想,通过图,来记忆。听课笔记:我的代码:classSolution:deflongestCommonSubsequence(self,text1:str,text2:str)->int:#假设text1为内层,text2为外层n......
  • P8058 [BalkanOI2003] Farey 序列 题解
    分析考虑二分答案。对于当前二分的答案\(x\),设\(cnt\)表示Farey序列中\(\frac{p}{q}\lex\)的满足条件的数量。对于一组\((i,j)\),若\(\frac{j}{i}\lex\),则\(j\le\lfloori\timesx\rfloor\)。得到暴力式子:\[cnt=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{\lfloo......
  • Spring反序列化失败 Type definition error: [simple type, class xxx.xxx.xxx]
    也许更好的阅读体验Typedefinitionerror:[simpletype,classcom.elm.po.CommonResult];nestedexceptioniscom.fasterxml.jackson.databind.exc.InvalidDefinitionException:Cannotconstructinstanceofcom.elm.po.CommonResult(noCreators,likedefaultconstru......