-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximun-subarray.cs
47 lines (40 loc) · 1.07 KB
/
maximun-subarray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Time limit exceeded
using System;
public class Program
{
public static void Main(string[] args)
{
Solution sol = new Solution();
//int[] array = new int[1]{1};
//int[] array = new int[5]{5,4,-1,7,8};
int[] array = new int[2]{-2,1};
Console.WriteLine(sol.MaxSubArray(array));
}
}
public class Solution {
public int MaxSubArray(int[] nums) {
int[] sumas = new int[nums.Length];
sumas[0] = nums[0];
int maxSum = sumas[0];
int index1 = 0;
int index2 = 0;
for(int i=1; i<nums.Length;i++){
sumas[i] = sumas[i-1]+nums[i];
if(sumas[i]> maxSum){
maxSum = sumas[i];
index2 = i;
}
}
for(int i=nums.Length-1; i>0;i--){
for(int j=0; j<i;j++){
int actual = sumas[i]-sumas[j];
if(actual>maxSum){
maxSum = actual;
index1 = j+1;
index2 = i;
}
}
}
return maxSum;
}
}