-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode763.java
More file actions
162 lines (156 loc) · 5.67 KB
/
LeetCode763.java
File metadata and controls
162 lines (156 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class LeetCode763 {
public static void main(String[] args) {
// 输入:s = "ababcbacadefegdehijhklij"
// 输出:[9,7,8]
System.out.println(Arrays.toString(new Solution763_2().partitionLabels("ababcbacadefegdehijhklij").toArray()));
// 输入:s = "eccbbbbdec"
// 输出:[10]
System.out.println(Arrays.toString(new Solution763_2().partitionLabels("eccbbbbdec").toArray()));
}
}
/**
* 整体思路没问题,但区间结合上复杂度较高【区间合并的思路不够清晰】
*/
class Solution763_1 {
public List<Integer> partitionLabels(String s) {
int[][] counts = new int[26][2]; // 存储a-z出现的最早和最晚位置
for (int i = 0; i < counts.length; i++) {
counts[i][0] = -1;
counts[i][1] = -1;
}
for (int i = 0; i < s.length(); i++) {
int ind = s.charAt(i) - 'a';
if (counts[ind][0] == -1) {
// 还未出现过对应字母
counts[ind][0] = i;
counts[ind][1] = i;
} else {
if (counts[ind][1] < i) {
counts[ind][1] = i;
}
}
}
List<int[]> intervals = new ArrayList<>();
for (int i = 0; i < counts.length; i++) {
if (counts[i][0] == -1) {
continue;
}
intervals.add(new int[] { counts[i][0], counts[i][1] });
}
Collections.sort(intervals, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);
// System.out.println(Arrays.deepToString(intervals.toArray(new
// int[intervals.size()][2])));
List<Integer> starts = new ArrayList<>();
// System.out.println(Arrays.toString(starts.toArray()));
for (int i = 0; i < intervals.size(); i++) {
int start = intervals.get(i)[0];
int end = intervals.get(i)[1];
// System.out.println(start + " " + end);
int ind = 0;
while (ind < starts.size() && start > starts.get(ind)) {
ind++;
}
if (ind == starts.size()) {
starts.add(start);
starts.add(end + 1);
} else {
if (starts.get(ind) == start) {
ind++;
}
while (ind < starts.size() && starts.get(ind) <= end) {
starts.remove(ind);
}
if (ind == starts.size()) {
starts.add(end + 1);
}
}
// System.out.println(Arrays.toString(starts.toArray()));
}
List<Integer> result = new ArrayList<Integer>();
for (int i = 1; i < starts.size(); i++) {
result.add(starts.get(i) - starts.get(i - 1));
}
return result;
}
}
/**
* 直接删除重复的区间,复杂度有所降低
*/
class Solution763_2 {
public List<Integer> partitionLabels(String s) {
int[][] counts = new int[26][2]; // 存储a-z出现的最早和最晚位置
for (int i = 0; i < counts.length; i++) {
counts[i][0] = -1;
counts[i][1] = -1;
}
for (int i = 0; i < s.length(); i++) {
int ind = s.charAt(i) - 'a';
if (counts[ind][0] == -1) {
// 还未出现过对应字母
counts[ind][0] = i;
counts[ind][1] = i;
} else {
if (counts[ind][1] < i) {
counts[ind][1] = i;
}
}
}
List<int[]> intervals = new ArrayList<>();
for (int i = 0; i < counts.length; i++) {
if (counts[i][0] == -1) {
continue;
}
intervals.add(new int[] { counts[i][0], counts[i][1] });
}
Collections.sort(intervals, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0]);
// System.out.println(Arrays.deepToString(intervals.toArray(new
// int[intervals.size()][2])));
int curr = 0;
int next = 1;
while (next < intervals.size()) {
if (intervals.get(curr)[1] >= intervals.get(next)[0]) {
if (intervals.get(curr)[1] < intervals.get(next)[1]) {
intervals.get(curr)[1] = intervals.get(next)[1];
}
intervals.remove(next);
} else {
curr++;
next++;
}
}
// System.out.println(Arrays.deepToString(intervals.toArray(new
// int[intervals.size()][2])));
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < intervals.size(); i++) {
result.add(intervals.get(i)[1] - intervals.get(i)[0] + 1);
}
return result;
}
}
/**
* 可以直接记录区间最大值用来更新区间的边界
*/
class Solution763_3 {
public List<Integer> partitionLabels(String S) {
char[] s = S.toCharArray();
int n = s.length;
int[] last = new int[26];
for (int i = 0; i < n; i++) {
last[s[i] - 'a'] = i; // 每个字母最后出现的下标
}
List<Integer> ans = new ArrayList<>();
int start = 0, end = 0;
for (int i = 0; i < n; i++) {
end = Math.max(end, last[s[i] - 'a']); // 更新当前区间右端点的最大值
if (end == i) { // 当前区间合并完毕
ans.add(end - start + 1); // 区间长度加入答案
start = i + 1; // 下一个区间的左端点
}
}
return ans;
}
}