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

53. Maximum Subarray #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from maximum-subarray
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ continually updating 😃.
* [26. Remove Duplicates from Sorted Array](./src/0026_remove_duplicates_from_sorted_array/rdfsa.go)   *`double index;`*  *`array`*
* [27. Remove Element](src/0027_remove_element/remove_element.go)   *`double index;`*  *`array`*
* [48. Rotate Image](src/0048_rotate_image/rotate_image.go)
* [53. Maximum Subarray](src/0053_maximum_subarray/maximum_subarray.go)   *`dynamic programming`*
* [75. Sort Colors](./src/0075_sort_colors/sort_colors.go)   *`sort;`*  *`array`*
* [80. Remove Duplicates from Sorted Array II](./src/0080_remove_duplicates_from_sorted_array2/rdfsa2.go)   *`double index;`*  *`array`*
* [88. Merge Sorted Array](./src/0088_merge_sorted_array/msa.go)   *`sort;`*  *`array`*
Expand Down
42 changes: 42 additions & 0 deletions src/0053_maximum_subarray/maximum_subarray.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
53. Maximum Subarray
https://leetcode.com/problems/maximum-subarray/

Given an integer array nums,
find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
*/
// time: 2019年01月02日

package maximumsubarray

import (
"fmt"
"math"
)

// time complexity: O(n)
// space complexity: O(1)
func maxSubArray(nums []int) int {
var (
max = math.MinInt32
sum int
start, end, f int
)
for i, j := range nums {
sum += j
if sum > max {
if f != 0 {
start = f + 1
f = 0
}
max = sum
end = i
}
if sum < 0 {
f = i
sum = 0
}
}
fmt.Printf("start index is %d, end index is %d\n", start, end)
return max
}
12 changes: 12 additions & 0 deletions src/0053_maximum_subarray/maximum_subarray_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package maximumsubarray

import "testing"

func TestMaxSubarray(t *testing.T) {
nums := []int{-2, 1, -3, 4, -1, 2, 1, -5, 4}
expected := 6

if res := maxSubArray(nums); res != expected {
t.Errorf("expected %d, got %d", expected, res)
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
|0034|[Find First and Last Position of Element in Sorted Array](0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)|Medium|*`binary search`*|
|0035|[35. Search Insert Position](0035_search_insert_position/search_insert_position.go)|Easy|*`binary search`*|
|0048|[48. Rotate Image](0048_rotate_image/rotate_image.go)|Medium|*`array`*|
|0053|[53. Maximum Subarray](0053_maximum_subarray/maximum_subarray.go)|Easy|*`dynamic programming`*|
|0061|[Rotate List](./0061_rotate_list/rotate_list.go)|Medium|*`linked list`*|
|0062|[Unique Paths](./0062_unique_paths/unique_paths.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
|0063|[Unique Paths 2](./0063_unique_paths_2/unique_paths2.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
Expand Down

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