8000 add 2765 · githubniraj/Leetcode@a975ded · GitHub
[go: up one dir, main page]

Skip to content
< 65F7 header class="HeaderMktg header-logged-out js-details-container js-header Details f4 py-3" role="banner" data-is-top="true" data-color-mode=light data-light-theme=light data-dark-theme=dark>

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a975ded

Browse files
add 2765
1 parent cf5b5e9 commit a975ded

File tree

3 files changed

+68
-1
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+68
-1
lines changed

paginated_contents/algorithms/3rd_thousand/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
| 2864 | [Maximum Odd Binary Number](https://leetcode.com/problems/maximum-odd-binary-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2864.java) | | Easy |Greedy
88
| 2824 | [Count Pairs Whose Sum is Less than Target](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2824.java) | | Easy |
99
| 2812 | [Find the Safest Path in a Grid](https://leetcode.com/problems/find-the-safest-path-in-a-grid/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2812.java) | | Medium |BFS
10-
| 2769 | [Find the Maximum Achievable Number](https://leetcode.com/problems/find-the-maximum-achievable-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2769.java) | | Easy |
10+
| 2769 | [Find the Maximum Achievable Number](https://leetcode.com/problems/find-the-maximum-achievable-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2769.java) | | Easy |
11+
| 2765 | [Longest Alternating Subarray](https://leetcode.com/problems/longest-alternating-subarray/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2765.java) | | Easy |
1112
| 2751 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2751.java) | | Hard | Stack, Simulation
1213
| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2716.java) | [:tv:](https://youtu.be/aMJ3T0K8LjI) | Easy |
1314
| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2710.java) | | Easy |
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
public class _2765 {
4+
public static class Solution1 {
5+
public int alternatingSubarray(int[] nums) {
6+
int len = nums.length;
7+
int maxLen = -1;
8+
for (int i = 0; i < len; i++) {
9+
for (int j = i + 1; j < len; j++) {
10+
if (j - i + 1 > maxLen && alternating(nums, i, j)) {
11+
maxLen = j - i + 1;
12+
}
13+
}
14+
}
15+
return maxLen;
16+
}
17+
18+
private boolean alternating(int[] nums, int start, int finish) {
19+
int expected = 1;
20+
for (int i = start, j = start + 1; i < finish && j <= finish; i++, j++) {
21+
if (nums[j] - nums[i] == expected) {
22+
expected = -expected;
23+
} else {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2765;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _2765Test {
10+
private static _2765.Solution1 solution1;
11+< C280 div class="diff-text-inner">
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2765.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.alternatingSubarray(new int[]{2, 3, 4, 3, 4}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(2, solution1.alternatingSubarray(new int[]{4, 5, 6}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(4, solution1.alternatingSubarray(new int[]{31, 32, 31, 32, 33}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(3, solution1.alternatingSubarray(new int[]{13, 14, 15, 14}));
35+
}
36+
}

0 commit comments

Comments
 (0)
0