Leetcode刷题5—最大子序和

一、题目要求

二、题目背景

动态规划(英语:Dynamic programming,简称 DP)是一种在数学、管理科学、计算机科学、经济学和生物信息学中使用的,通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法。

动态规划常常适用于有重叠子问题和最优子结构性质的问题,动态规划方法所耗时间往往远少于朴素解法。

动态规划背后的基本思想非常简单。大致上,若要解一个给定问题,我们需要解其不同部分(即子问题),再根据子问题的解以得出原问题的解。动态规划往往用于优化递归问题,例如斐波那契数列,如果运用递归的方式来求解会重复计算很多相同的子问题,利用动态规划的思想可以减少计算量。

通常许多子问题非常相似,为此动态规划法试图仅仅解决每个子问题一次,具有天然剪枝的功能,从而减少计算量:一旦某个给定子问题的解已经算出,则将其记忆化存储,以便下次需要同一个子问题解之时直接查表。这种做法在重复子问题的数目关于输入的规模呈指数增长时特别有用。

链接:https://leetcode-cn.com/tag/dynamic-programming/
来源:力扣(LeetCode)

三、解法

(1)看到这个题目首先想到的是一层一层计算,第一层找到最大的值对应的索引;第二层找两两数组之和的最大索引,如果都小于0则退出循环;第三层找连续三个数组之和,如果都小于0等退出循环,依次类推

(2)采用动态规划的方法

初始化sum=0,ans=num[0],不断向右累加,如果sum大于等于0,则记录,否则sum置为num[i]

原文地址:https://www.cnblogs.com/bethansy/p/11993467.html

时间: 2024-08-30 13:20:11

Leetcode刷题5—最大子序和的相关文章

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef

【leetcode刷题笔记】Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

【leetcode刷题笔记】Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 题解:需要找到二叉搜索树中乱序的两个节点,并把它们交换回来

leetcode 刷题之路 77 Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. Permutations 的升级版,依旧是全排列问题,但是序列中可能会出现重复数字. 思路:采用字典序的非递归方

leetcode 刷题之路 78 Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 非递归方式实现二叉树的后序遍历. 思路:使用栈辅助实现,首先将根节点入栈,然后以栈是否为

LeetCode刷题总结-链表

LeetCode刷题总结-链表 一.链表     链表分为单向链表.单向循环链表和双向链表,一下以单向链表为例实现单向链表的节点实现和单链表的基本操作. 单向链表 单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域.这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值. 表元素域elem用来存放具体的数据: 链接域next用来存放下一个节点的位置(python中的标识): 变量p指向链表的头节点(首节点)的位置,从p出发能找到表