LeetCode 70 _ Maximum Subarray 最大子数组 (Easy)

Description:

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Note: Given n will be a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

Solution:

这道题说的是,每次可以走1步或2步,问n步有多少种走法

当看到这种后一步的结果和前一步的方法相关的时候,大多就是使用Dynamic Programming方法去解决。

我们随意举个栗子,假设一共需要走8步:

1  2   3   4   5  6  7   8

— — — — — — — —

到达第1格的时候,只能从起点走1步过去,即1种方法;

到达第2格的时候,可从起点走2步过去和从第1格走1步过去,即2种方法;

那么到达第3格的时候呢?可以从第1格走2步,和第2格走一步,走到第1格只有1种方法,因此途径第1格到第3格一共只有1种方法;而走到第2格有2种方法,因此途径第2格到第3格有2种方法;这样相加起来一共3种;

向后继续推断此我们可以发现,其实每一步都和它前面的两步相关,再继续向后尝试,发现它其实就是一个斐波那契数列啦~

1  2   3   4   5  6  7   8

— — — — — — — —

1  2   3  5   8  .....

Code:

public int climbStairs(int n) {
    if (n <= 1){
        return 1;
    }
    int[] res = new int[n];
    res[0] = 1; res[1] = 2;
    for (int i = 2; i < n; i++){
        res[i] = res[i-1] + res[i-2];
    }
    return res[n-1];
}

提交情况:

Runtime: 0 ms, faster than 100.00% of Java online submissions for Climbing Stairs.
Memory Usage: 31.9 MB, less than 100.00% of Java online submissions for Climbing Stairs.

原文地址:https://www.cnblogs.com/zingg7/p/10679070.html

时间: 2024-10-14 03:47:18

LeetCode 70 _ Maximum Subarray 最大子数组 (Easy)的相关文章

[LeetCode] Maximum Subarray 最大子数组

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. click to show more practice. Mor

lintcode 中等题:maximum subarray最大子数组II

题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, 3, -1, 2, -1, 2],这两个子数组分别为[1, 3]和[2, -1, 2]或者[1, 3, -1, 2]和[2],它们的最大和都是7 注意 子数组最少包含一个数 挑战 要求时间复杂度为O(n) 解题 最大子数组I 这个题目是求一个数组中一个最大子数组的和,而本题目是求数组中的前

【LeetCode每天一题】Maximum Subarray(最大子数组)

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example:        Input: [-2,1,-3,4,-1,2,1,-5,4],            Output: 6                 Explanation: [4,-1,2,1] has

[LintCode] Maximum Subarray 最大子数组

Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarray should contain at least one number. Have you met this question in a real interview? Yes Example Given the array [−2,2,−3,4,−1,2,1,−5,3], the contigu

Maximum Subarray (最大子数组)

题目描述: 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. 这道题很经典,<算法导论>上有相关讨论,但是没有

53. Maximum Subarray最大子数组

[抄题]: 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. 方法一:贪心算法greedy [一句话思路]: 每次

(leetcode题解)Maximum Subarray

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. 这是一道经典的题目,给定一个数组求和最大的子数组.算法导论对这道

[Leetcode][Python]53: Maximum Subarray

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 53: Maximum Subarrayhttps://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given t

【LeetCode】053. Maximum Subarray

题目: 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. 题解: 由于是连续子序列这个限制,所以如果k+1这个元素