keb revised this gist . Go to revision
1 file changed, 36 insertions
foo.md(file created)
@@ -0,0 +1,36 @@ | |||
1 | + | this is me testing opengist | |
2 | + | ||
3 | + | here is some rust code | |
4 | + | ||
5 | + | ```rs | |
6 | + | use std::cmp; | |
7 | + | ||
8 | + | impl Solution { | |
9 | + | pub fn max_sub_array(nums: Vec<i32>) -> i32 { | |
10 | + | let mut current_subarray_sum = nums[0]; | |
11 | + | let mut max_subarray_sum = nums[0]; | |
12 | + | ||
13 | + | for num in &nums[1..] { | |
14 | + | let num = *num; | |
15 | + | ||
16 | + | // as we iterate through the array, | |
17 | + | // we increment by the next number in the "current" subarray | |
18 | + | // if we find that the sum of the current subarray is less | |
19 | + | // than the current number, we "reset" to the current number | |
20 | + | current_subarray_sum = cmp::max( | |
21 | + | num, | |
22 | + | current_subarray_sum + num | |
23 | + | ); | |
24 | + | ||
25 | + | // meanwhile, always be keeping track of the max subarray sum | |
26 | + | // we've found | |
27 | + | max_subarray_sum = cmp::max( | |
28 | + | max_subarray_sum, | |
29 | + | current_subarray_sum | |
30 | + | ); | |
31 | + | } | |
32 | + | ||
33 | + | max_subarray_sum | |
34 | + | } | |
35 | + | } | |
36 | + | ``` |
Newer
Older