内容简介:动态规划题第五天
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Automate This
Christopher Steiner / Portfolio / 2013-8-9 / USD 25.95
"The rousing story of the last gasp of human agency and how today's best and brightest minds are endeavoring to put an end to it." It used to be that to diagnose an illness, interpret legal docume......一起来看看 《Automate This》 这本书的介绍吧!