Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 28f6e43

Browse files
committed
feat: add solution for maximum subarray
1 parent 4b73a89 commit 28f6e43

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
def maxSubArray(nums: List[int]) -> int:
4+
maxNumber = nums[0]
5+
curSum = 0
6+
7+
for n in nums:
8+
if curSum < 0:
9+
curSum = 0
10+
curSum += n
11+
maxNumber = max(maxNumber, curSum)
12+
13+
return maxNumber
14+
15+
"""
16+
Algorithm - Kadane's Algorithm
17+
Time Complexity - O(n)
18+
Space Complexity - O(1)
19+
20+
Explanation:
21+
1. Initialize maxNumber and curSum to the first element in the array
22+
2. Iterate through the array
23+
3. If curSum is less than 0, set curSum to 0
24+
4. Add the current element to curSum
25+
5. Set maxNumber to the max of maxNumber and curSum
26+
6. Return maxNumber
27+
"""
28+
29+
30+
assert maxSubArray([-2,1,-3,4,-1,2,1,-5,4]) == 6
31+
assert maxSubArray([1]) == 1
32+
assert maxSubArray([5,4,-1,7,8]) == 23
33+
assert maxSubArray([-2,-1]) == -1

‎src/medium/maximum_subarray.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![allow(dead_code)]
2+
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
3+
let mut max_number = nums[0];
4+
let mut cur_sum = 0;
5+
6+
for n in nums {
7+
if cur_sum < 0 {
8+
cur_sum = 0;
9+
}
10+
cur_sum += n;
11+
max_number = max_number.max(cur_sum);
12+
}
13+
14+
max_number
15+
}
16+
17+
#[test]
18+
fn test_max_sub_array() {
19+
assert_eq!(max_sub_array(vec![-2, 1, -3, 4, -1, 2, 1, -5, 4]), 6);
20+
}

‎src/readme.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
- [ ] [45. Jump game II](../src/medium/jump_game_ii.rs) -> [Problem Description](../src/medium/readme.md#45-jump-game-ii)
6363
- [x] [46. Permutations](../src/medium/permutations.rs) -> [Problem Description](../src/medium/readme.md#46-permutations)
6464
- [x] [49. Group anagrams](../src/medium/group_anagrams.rs) -> [Problem Description](../src/medium/readme.md#49-group-anagrams)
65-
- [] [53. Maximum subarray](../src/medium/maximum_subarray.rs) -> [Problem Description](../src/medium/readme.md#53-maximum-subarray)
65+
- [x] [53. Maximum subarray](../src/medium/maximum_subarray.rs) -> [Problem Description](../src/medium/readme.md#53-maximum-subarray)
6666
- [ ] [55. Jump game](../src/medium/jump_game.rs) -> [Problem Description](../src/medium/readme.md#55-jump-game)
6767
- [x] [74. Search a 2D matrix](../src/medium/search_a_2d_matrix.rs) -> [Problem Description](../src/medium/readme.md#74-search-a-2d-matrix)
6868
- [x] [78. Subsets](../src/medium/subsets.rs) -> [Problem Description](../src/medium/readme.md#78-subsets)

‎theory/categories/7.greedy/readme.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Greedy algorithms are implemented using a greedy function that makes the best ch
1515

1616
## Problems
1717

18-
- [] [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | Medium | [Solution](../../../src/medium/maximum_subarray.rs) | [Problem Description](../../../src/medium/readme.md#53-maximum-subarray)
18+
- [x] [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | Medium | [Solution](../../../src/medium/maximum_subarray.rs) | [Problem Description](../../../src/medium/readme.md#53-maximum-subarray)
1919
- [ ] [Jump Game](https://leetcode.com/problems/jump-game/) | Medium | [Solution](../../../src/medium/jump_game.rs) | [Problem Description](../../../src/medium/readme.md#55-jump-game)
2020
- [ ] [Jump Game II](https://leetcode.com/problems/jump-game-ii/) | Medium | [Solution](../../../src/medium/jump_game_ii.rs) | [Problem Description](../../../src/medium/readme.md#45-jump-game-ii)
2121
- [ ] [Gas Station](https://leetcode.com/problems/gas-station/) | Medium | [Solution](../../../src/medium/gas_station.rs) | [Problem Description](../../../src/medium/readme.md#134-gas-station)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /