成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[Leetcode] Maximum Subarray 子序列最大和

summerpxy / 3476人閱讀

摘要:最新更新請(qǐng)見(jiàn)原題鏈接動(dòng)態(tài)規(guī)劃復(fù)雜度時(shí)間空間思路這是一道非常典型的動(dòng)態(tài)規(guī)劃題,為了求整個(gè)字符串最大的子序列和,我們將先求較小的字符串的最大子序列和。而最大子序列和的算法和上個(gè)解法還是一樣的。

Maximum Subarray 最新更新請(qǐng)見(jiàn):https://yanjia.me/zh/2019/02/...
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6.

原題鏈接

動(dòng)態(tài)規(guī)劃 復(fù)雜度

時(shí)間 O(N) 空間 O(N)

思路

這是一道非常典型的動(dòng)態(tài)規(guī)劃題,為了求整個(gè)字符串最大的子序列和,我們將先求較小的字符串的最大子序列和。這里我們從后向前、從前向后計(jì)算都是可以的。在從前向后計(jì)算的方法中,我們將第i個(gè)元素之前最大的子序列和存入一個(gè)一維數(shù)組dp中,對(duì)第i+1個(gè)元素來(lái)說(shuō),它的值取決于dp[i],如果dp[i]是負(fù)數(shù),那就沒(méi)有必要加上它,因?yàn)檫@只會(huì)拖累子序列的最大和。如果是正數(shù)就加上它。最后我們?cè)僦v第i+1個(gè)元素自身加進(jìn)去,就得到了第i+1個(gè)元素之前最大的子序列和。

代碼
public class Solution {
    public int maxSubArray(int[] nums) {
        int[] dp = new int[nums.length];
        int max = nums[0];
        dp[0] = nums[0];
        for(int i = 1; i < nums.length; i++){
            dp[i] = dp[i-1]>0? dp[i-1] + nums[i] : nums[i];
            max = Math.max(dp[i],max);
        }
        return max;
    }
}
掃描算法 復(fù)雜度

時(shí)間 O(N) 空間 O(1)

思路

仔細(xì)觀察上面的代碼,我們其實(shí)只用到了dp[i-1]這個(gè)量,所以如果用一個(gè)變量記錄上次的值,那么這O(N)的空間是可以省略的。我們要做的就是掃描一遍數(shù)組,遍歷每個(gè)數(shù)的時(shí)候都更新這個(gè)變量。而最大子序列和的算法和上個(gè)解法還是一樣的。

代碼
public class Solution {
    public int maxSubArray(int[] nums) {
        int max = nums[0];
        int sum = nums[0];
        for(int i = 1; i < nums.length; i++){
            sum = sum < 0 ? nums[i] : sum + nums[i];
            max = Math.max(sum, max);
        }
        return max;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/66167.html

相關(guān)文章

  • leetcode53 Maximum Subarray 最大連續(xù)數(shù)組

    摘要:我們可以分別得出這三種情況下的最大子數(shù)列和,并比較得出最大的那個(gè)。我們只需要考慮左子列的最大和以及跨越了左右的中子列的最大值。 題目要求 Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given ...

    Bamboy 評(píng)論0 收藏0
  • leetcode152 Maximum Product Subarray

    摘要:題目要求從一個(gè)整數(shù)數(shù)組中找到一個(gè)子數(shù)組,該子數(shù)組中的所有元素的乘積最大。比如數(shù)組的最大乘積子數(shù)組為思路與代碼這題目考察了動(dòng)態(tài)編程的思想。至于為什么還要比較,是因?yàn)槿绻且粋€(gè)負(fù)數(shù)的,那么之前的最小乘積在這里可能就成為了最大的乘積了。 題目要求 Find the contiguous subarray within an array (containing at least one num...

    Arno 評(píng)論0 收藏0
  • leetcode_53 Maximum Subarray

    摘要:如果單開(kāi)元素加和更大判斷前面的子數(shù)組和是不是小于。此元素就成為了子數(shù)組的第一個(gè)元素。每次操作都要判斷,當(dāng)前是否是最大值,更新值。 題目詳情 Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given t...

    y1chuan 評(píng)論0 收藏0
  • leetcode 643 Maximum Average Subarray I

    摘要:題目詳情輸入一個(gè)數(shù)組和一個(gè)整數(shù)。要求找出輸入數(shù)組中長(zhǎng)度為的子數(shù)組,并且要求子數(shù)組元素的加和平均值最大。 題目詳情 Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need ...

    SwordFly 評(píng)論0 收藏0
  • [LintCode/LeetCode] Maximum Product Subarray

    摘要:這是一道簡(jiǎn)單的動(dòng)規(guī)題目,同步更新數(shù)組解決了為負(fù)數(shù)的問(wèn)題。即使是求最小乘積子序列,也可以通過(guò)取和的最小值獲得。 Problem Find the contiguous subarray within an array (containing at least one number) which has the largest product. Example For example, g...

    meteor199 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<