Skip to content

Commit bb36eaf

Browse files
add 763
1 parent 6bcd6f5 commit bb36eaf

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|763|[Partition Labels](https://leetcode.com/problems/partition-labels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_763.java) | O(n) | O(1) | |Medium|
2526
|760|[Find Anagram Mappings](https://leetcode.com/problems/find-anagram-mappings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_760.java) | O(n^2) | O(1) | |Easy|
2627
|755|[Reach a Number](https://leetcode.com/problems/reach-a-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_755.java) | O(n) | O(1) | |Medium| Math
2728
|750|[Number Of Corner Rectangles](https://leetcode.com/problems/number-of-corner-rectangles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_750.java) | O((m*n)^2) | O(1) | |Medium|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 763. Partition Labels
8+
*
9+
* A string S of lowercase letters is given.
10+
* We want to partition this string into as many parts as possible so that each letter appears
11+
* in at most one part, and return a list of integers representing the size of these parts.
12+
13+
Example 1:
14+
Input: S = "ababcbacadefegdehijhklij"
15+
Output: [9,7,8]
16+
Explanation:
17+
The partition is "ababcbaca", "defegde", "hijhklij".
18+
This is a partition so that each letter appears in at most one part.
19+
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
20+
21+
Note:
22+
S will have length in range [1, 500].
23+
S will consist of lowercase letters ('a' to 'z') only.
24+
*/
25+
public class _763 {
26+
27+
public static class Solution1 {
28+
public List<Integer> partitionLabels(String S) {
29+
List<Integer> result = new ArrayList<>();
30+
int[] last = new int[26];
31+
/**This is the key step:
32+
* we find the last occurrence of each letter and record them in last[]*/
33+
for (int i = 0; i < S.length(); i++) {
34+
last[S.charAt(i) - 'a'] = i;
35+
}
36+
int start = -1;
37+
int end = -1;
38+
for (int i = 0; i < S.length(); i++) {
39+
if (start == -1) {
40+
start = i;
41+
}
42+
end = Math.max(end, last[S.charAt(i) - 'a']);
43+
if (end == i) {
44+
result.add(end - start + 1);
45+
start = -1;
46+
}
47+
}
48+
return result;
49+
}
50+
}
51+
52+
}
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._763;
4+
import java.util.Arrays;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _763Test {
11+
private static _763.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _763.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(Arrays.asList(9, 7, 8), solution1.partitionLabels("ababcbacadefegdehijhklij"));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(Arrays.asList(9, 7, 8), solution1.partitionLabels("ababcbacadefegdehijhklij"));
26+
}
27+
}

0 commit comments

Comments
 (0)