Skip to content

Commit 8a73f70

Browse files
add 1708
1 parent a9fd697 commit 8a73f70

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1708|[Largest Subarray Length K](https://leetcode.com/problems/largest-subarray-length-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1708.java) ||Easy|Array, Greedy|
1112
|1704|[Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1704.java) ||Easy|String|
1213
|1700|[Number of Students Unable to Eat Lunch](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1700.java) ||Easy|Array|
1314
|1694|[Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1694.java) ||Easy|String|
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1708 {
4+
public static class Solution1 {
5+
public int[] largestSubarray(int[] nums, int k) {
6+
int max = nums[0];
7+
int beginIndex = 0;
8+
for (int i = 1; i <= nums.length - k; i++) {
9+
if (nums[i] > max) {
10+
max = nums[i];
11+
beginIndex = i;
12+
}
13+
}
14+
int[] result = new int[k];
15+
for (int i = beginIndex, j = 0; i < i + k && j < k; i++) {
16+
result[j++] = nums[i];
17+
}
18+
return result;
19+
}
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1708;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1708Test {
10+
private static _1708.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1708.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{5, 2, 3}, solution1.largestSubarray(new int[]{1, 4, 5, 2, 3}, 3));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new int[]{5}, solution1.largestSubarray(new int[]{1, 4, 5, 2, 3}, 1));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)