balls
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
public class Main
|
||||
{
|
||||
public static int smallestSubarrayLength(int[] arr, int target)
|
||||
{
|
||||
int smallest = 0;
|
||||
|
||||
for (int i = 0; i < arr.length; i++)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int j = i; j < arr.length; j++)
|
||||
{
|
||||
if (sum + arr[j] > target) break;
|
||||
else if (sum + arr[j] == target)
|
||||
{
|
||||
smallest = j - (i - 1);
|
||||
break;
|
||||
}
|
||||
else sum += arr[j];
|
||||
}
|
||||
}
|
||||
|
||||
return smallest;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MainTest
|
||||
{
|
||||
@Test
|
||||
public void test_1()
|
||||
{
|
||||
assertEquals(2, Main.smallestSubarrayLength(new int[]{2, 3, 1, 2, 4, 3}, 7));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_2()
|
||||
{
|
||||
assertEquals(0, Main.smallestSubarrayLength(new int[]{1, 2, 3, 4}, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_3()
|
||||
{
|
||||
assertEquals(2, Main.smallestSubarrayLength(new int[]{1, 2, 3, 4, 5}, 9));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user