Voyz's Studio.

LeetCode算法笔记--最大子序和

字数统计: 129阅读时长: 1 min
2020/12/16 Share

LeetCode算法笔记–Day43

55. 跳跃游戏

题目:

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

example
input: nums = [2,3,1,1,4]
output: true

input: nums = [3,2,1,0,4]
output: false

Analyze:

方法一:贪心算法

My Answer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
let sum = 0,
res = nums[0];

nums.forEach((num)=>{
if(sum <= 0){
sum = num;
}else {
sum += num;
}
res = Math.max(res,sum)
})

return res;
};
CATALOG
  1. 1. LeetCode算法笔记–Day43
  2. 2. 55. 跳跃游戏
    1. 2.1. 题目:
    2. 2.2. Analyze:
      1. 2.2.1. 方法一:贪心算法
    3. 2.3. My Answer: