题目

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.

More practice:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle

 
代码:oj测试通过 Runtime: 80 ms
 class Solution:
# @param A, a list of integers
# @return an integer
def maxSubArray(self, A):
curr_sum = 0
max_sum = -100000
for i in range(len(A)):
if curr_sum < 0 :
curr_sum = 0
curr_sum = curr_sum + A[i]
max_sum = max(curr_sum, max_sum)
return max_sum

思路

想不出来这种精妙的算法。

隔了一天Google到了一篇解释日志:

http://blog.csdn.net/linhuanmars/article/details/21314059

这个把这道题的背后思想解释的很好,有些受益。

leetcode 【 Maximum Subarray 】python 实现的更多相关文章

  1. [leetcode]Maximum Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...

  2. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  3. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  4. [LeetCode]Maximum Subarray题解

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

  5. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

  6. 53. Maximum Subarray@python

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  7. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. LeetCode——Maximum Subarray

    Description: Find the contiguous subarray within an array (containing at least one number) which has ...

  9. 53. [LeetCode] Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  10. Python3解leetcode Maximum Subarray

    问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...

随机推荐

  1. React 官网列子学习

    一个有状态的组件 除了接受输入数据(通过 this.props ),组件还可以保持内部状态数据(通过this.state ).当一个组件的状态数据的变化,展现的标记将被重新调用render() 更新. ...

  2. python3基础06(随机数的使用)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import osimport randomimport string la=[0,1,2,3,4,5,6,7, ...

  3. 【远程重启】使用windows自带的shutdown命令远程重启服务器(测试不行,此文作废)

    net use \\IP \ipc$ "password" /user:"username" shutdown -r -m \\IP -t 0 -f 添加远程关 ...

  4. UVA1363 - Joseph's Problem(数学,迷之优化)

    题意:给出n和k,1≤n,k≤1e9,计算 切入点是k/i 和 k/(i+1)差距不大.令pi = k/i, ri = k%i.如果pi+1 == pi,那么ri+1 == k - pi(i+1) = ...

  5. PAT (Basic Level) Practise (中文)- 1006. 换个格式输出整数 (15)

    http://www.patest.cn/contests/pat-b-practise/1006 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换 ...

  6. AngularJS 应用

    AngularJS模块(Module)定义了AngularJS的应用. AngularJS控制器(Controller)用于控制AngularJS应用. ng-app指令定义了应用,ng-contro ...

  7. java算法面试题:编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔。

    package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...

  8. 解决使用Application Loader上传ipa提示“上传appstore失败”

    试了好多次使用Application Loader上传ipa,一直提示上传失败,用其他mac电脑却可以,那就是环境有问题,笔者试过重装xcode,都无法解决问题, 查看日志类似是jdk版本问题,换了所 ...

  9. ajax全局变量的使用

    var username; $.ajax({ type:"post", url:"a.action", data: {}, dataType: 'text',  ...

  10. k8s的ingress资源简述

    ingress controller是独立与controller-manager的Ingress的主要作用是可以利用nginx,haproxy,envoy,traefik等负载均衡器来暴露集群内部服务 ...