Leetcode - Arrays | Subarray
53. Maximum Subarray[E]
https://leetcode.com/problems/maximum-subarray/
Description
given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
1 | Input: [-2,1,-3,4,-1,2,1,-5,4], |
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
子列表元素之和的最大值
说明:子列表指的是列表中索引(下标)连续的元素构成的列表;列表中的元素是int类型,可能包含正整数、0、负整数;程序输入列表中的元素,输出子列表元素求和的最大值,例如:
输入:1 -2 3 5 -3 2
输出:8
输入:0 -2 3 5 -1 2
输出:9
输入:-9 -2 -3 -5 -3
输出:-2
Solution
https://leetcode.com/problems/maximum-subarray/discuss/20396/Easy-Python-Way
1 | class Solution: |
1 | ## 动态规划例子 |
说明:这个题目最容易想到的解法是使用二重循环,但是代码的时间性能将会变得非常的糟糕。使用动态规划的思想,仅仅是多用了两个变量,就将原来复杂度的问题变成了。
152. Maximum Product Subarray[M]
https://leetcode.com/problems/maximum-product-subarray/
Description
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
1 | Input: [2,3,-2,4] |
Example 2:
1 | Input: [-2,0,-1] |
Solution
560. Subarray Sum Equals K[M]
https://leetcode.com/problems/subarray-sum-equals-k/
Description
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
Example 1:
1 | Input:nums = [1,1,1], k = 2 |
Constraints:
- The length of the array is in range [1, 20,000].
- The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
Solution
https://leetcode.com/problems/subarray-sum-equals-k/solution/
1 | class Solution: |
713. Subarray Product Less Than K[M]
https://leetcode.com/problems/subarray-product-less-than-k/
Description
Your are given an array of positive integers nums.
Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.
Example 1:
1 | Input: nums = [10, 5, 2, 6], k = 100 |
Note:
0 < nums.length <= 50000.
0 < nums[i] < 1000.
0 <= k < 10^6.
Solution
1 |
https://leetcode.com/problems/subarray-product-less-than-k/solution/
581. Shortest Unsorted Continuous Subarray[E]
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/
Description
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.
You need to find the shortest such subarray and output its length.
Example 1:
1 | Input: [2, 6, 4, 8, 10, 9, 15] |
Note:
- Then length of the input array is in range [1, 10,000].
- The input array may contain duplicates, so ascending order here means <=.
Solution
1 |
974. Subarray Sums Divisible by K[M]
https://leetcode.com/problems/subarray-sums-divisible-by-k/
Description
iven an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
1 | Input: A = [4,5,0,-2,-3,1], K = 5 |
Note:
1 <= A.length <= 30000-10000 <= A[i] <= 100002 <= K <= 10000
Solution
1 |





