Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8cba8f2

Browse files
refactor 1493
1 parent c08f4ad commit 8cba8f2

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

+1
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+
|1493|[Longest Subarray of 1's After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1493.java) | |Medium|Array|
1112
|1492|[The kth Factor of n](https://leetcode.com/problems/the-kth-factor-of-n/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1492.java) | |Medium|Math|
1213
|1491|[Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1491.java) | |Easy|Array, Sort|
1314
|1487|[Making File Names Unique](https://leetcode.com/problems/making-file-names-unique/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1487.java) | |Medium|HashTable, String|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1493 {
7+
public static class Solution1 {
8+
public int longestSubarray(int[] nums) {
9+
List<int[]> brackets = new ArrayList<>();
10+
for (int i = 0; i < nums.length; i++) {
11+
if (nums[i] == 1) {
12+
int right = i + 1;
13+
while (right < nums.length && nums[right] == 1) {
14+
right++;
15+
}
16+
if (right < nums.length && nums[right] == 1) {
17+
brackets.add(new int[]{i, right});
18+
} else {
19+
brackets.add(new int[]{i, right - 1});
20+
}
21+
i = right;
22+
}
23+
}
24+
int longest = 0;
25+
for (int[] bracket : brackets) {
26+
if (bracket[1] == nums.length - 1 && bracket[0] == 0) {
27+
return nums.length - 1;
28+
}
29+
longest = Math.max(bracket[1] - bracket[0] + 1, longest);
30+
}
31+
for (int i = 0; i < brackets.size() - 1; i++) {
32+
int[] first = brackets.get(i);
33+
int[] second = brackets.get(i + 1);
34+
if (first[1] + 2 == second[0]) {
35+
int connected = (first[1] - first[0] + 1) + (second[1] - second[0] + 1);
36+
longest = Math.max(longest, connected);
37+
}
38+
}
39+
return longest;
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1493;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1493Test {
10+
private static _1493.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1493.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 1, 0, 1};
21+
assertEquals(3, solution1.longestSubarray(nums));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)