diff --git a/src/problem/mod.rs b/src/problem/mod.rs index c2856a2..ca68ad9 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -171,4 +171,5 @@ mod p46_permutations; mod p39_combination_sum; mod p22_generate_parentheses; mod p79_word_search; -mod p108_convert_sorted_array_to_binary_search_tree; \ No newline at end of file +mod p108_convert_sorted_array_to_binary_search_tree; +mod p53_maximum_subarray; \ No newline at end of file diff --git a/src/problem/p53_maximum_subarray.rs b/src/problem/p53_maximum_subarray.rs new file mode 100644 index 0000000..b7879f1 --- /dev/null +++ b/src/problem/p53_maximum_subarray.rs @@ -0,0 +1,32 @@ +/** + * [53] Maximum Subarray + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn max_sub_array(nums: Vec) -> i32 { + let mut pre = 0; + let mut result = nums[0]; + + for i in nums { + pre = i.max(pre + i); + result = result.max(pre); + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_53() { + } +}