leetcode - Maximum SubarrayFind the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given ...
分类:
其他好文 时间:
2015-08-20 12:17:24
阅读次数:
130
Dynamic ProgrammingThere is a nice introduction to the DP algorithm in thisWikipedia article. The idea is to maintain a running maximumsmaxand a curre...
分类:
其他好文 时间:
2015-08-16 16:22:54
阅读次数:
128
Given an array ofnpositive integers and a positive integers, find the minimal length of a subarray of which the sum ≥s. If there isn't one, return 0 i...
分类:
其他好文 时间:
2015-08-16 12:04:38
阅读次数:
80
思路:
和求Max Subarray类似,维护一个当前元素可以跳至的最大值,每循环一次更新reach=Math.max(nums[i]+1,reach),当i>reach或i>=nums.length的时候循环终止,最后看循环是否到达了最后,到达最后则返回true,否则,返回false....
分类:
其他好文 时间:
2015-08-13 10:06:40
阅读次数:
110
思路:
1.Jump Game思路:和求Max Subarray类似,维护一个当前元素可以跳至的最大值,每循环一次更新reach=Math.max(nums[i]+1,reach),当i>reach或i>=nums.length的时候循环终止,最后看循环是否到达了最后,到达最后则返回true,否则,返回false.
2.和Jump Game不同的是,Jump Game II 让求的是跳过所有的元素至少需要几步,这需要维护一个局部变量edge为上一个reach,当i<=reach时,每次仍然通过Math.ma...
分类:
其他好文 时间:
2015-08-13 10:03:50
阅读次数:
107
题目:
数组中有一个数字出现的次数超过数组长度的一半,请找出这一个数字。输入一个长度为9的数组{1,2,3,2,2,2,5,4,2},
数字2在数组中出现的次数大于数组长度的一半,返回2。
这道题类似以前做过的Maximum Subarray。
按着以前的思路:
确定某一个数字,遇到相同的数字加1,遇到不同的数字减1,当累加器为0时,我们重新开始计算即可。
完成后继续判断这个数...
分类:
编程语言 时间:
2015-08-12 13:18:44
阅读次数:
160
leetcode
minimum size subarray sum...
分类:
其他好文 时间:
2015-08-11 18:48:33
阅读次数:
126
August 10, 2015有二种解法, Greedy算法, 和DP算法. 二种算法都通过leetcode online judge. 贪婪算法, 花了一个小时理解代码, 修改后很好理解.Problem statement:Find the contiguous subarray within a...
分类:
其他好文 时间:
2015-08-11 09:44:20
阅读次数:
127
题目描述: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,?...
分类:
编程语言 时间:
2015-08-09 20:16:09
阅读次数:
134
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return
0 instead.
For example, given the array [2,3...
分类:
其他好文 时间:
2015-08-08 12:11:52
阅读次数:
168