Created
July 29, 2014 04:33
-
-
Save rebornix/ca164b80520c045bbf7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace MinLength | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(minLen(new int[] {10001, 4000, 4000, 3000, 8000})); | |
Console.WriteLine(minLen(new int[] {1000, 4000, 5000, 3000, 4000 })); | |
Console.WriteLine(minLen(new int[] { 4000, 4000, 3000, 2000 })); | |
Console.WriteLine(minLen(new int[] { 4000, 1000, 3000, 2000 })); | |
Console.WriteLine(minLen(new int[] { 10001 })); | |
Console.WriteLine(minLen(new int[] { 4000, 1000, 0, 2000 })); | |
Console.WriteLine(minLen(new int[] { 100, 100, 500, 9000, 500, 9400, 200 })); | |
} | |
public static int minLen(int[] data) | |
{ | |
int amount = data.Count(); | |
int minLength = int.MaxValue; | |
int start = 0; | |
int end = 0; | |
int sum = data[start]; | |
while (start <= end && end < amount) | |
{ | |
if (sum < 10000) | |
{ | |
end++; | |
if (end >= amount) | |
break; | |
else | |
sum += data[end]; | |
} | |
else | |
{ | |
minLength = min(minLength, end - start + 1); | |
sum = sum - data[start]; | |
if (start < end) | |
start++; | |
else | |
{ | |
start++; | |
end++; | |
if (end >= amount) | |
break; | |
else | |
sum = data[start]; | |
} | |
} | |
} | |
return minLength; | |
} | |
public static int min(int a, int b) | |
{ | |
if (a > b) | |
return b; | |
return a; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment