内容简介:动态规划题第五天
2 0 1 9 -5 -22 星 期三 开 始 吧
动态规划题第五天
题 目 描 述
给定一个数组,找出最长增加的子序列。
题目解析
首先他并不是求连续的,只要后面的数比前面的大,那么他就是可以被添加到子序列中的,只是说每次应该加入较小的数字,才能给后面腾出更多的位置。
DP[i]=max(DP[i],DP[j]+1) j<i 当前DP[i]的数和$nums[j]<$nums[$i],dp+1的数最比较得出当前最大子序列
/**
* @param Integer[] $nums
* @return Integer
*/
function lengthOfLIS($nums) {
if(empty($nums)){
return 0;
}
$res=1;
for($i=0;$i<count($nums);$i++){
$dp[$i]=1;
for($j=0;$j<$i;++$j){
if($nums[$j]<$nums[$i]){
$dp[$i]=max($dp[$i],$dp[$j]+1);
}
$res=max($res,$dp[$i]);
}
}
return $res;
}
Github整理地址: https://github.com/wuqinqiang/leetcode-php
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Intersectional Internet
Safiya Umoja Noble、Brendesha M. Tynes / Peter Lang Publishing / 2016
From race, sex, class, and culture, the multidisciplinary field of Internet studies needs theoretical and methodological approaches that allow us to question the organization of social relations that ......一起来看看 《The Intersectional Internet》 这本书的介绍吧!