No.1 重新分配字符使所有字符串都相等
解题思路
统计每个字母的个数,判断是否能被字符串的个数整除即可。
代码展示
public class MyActivity extends AppCompatActivity {
@Override //override the function
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
OkhttpManager.getInstance().setTrustrCertificates(getAssets().open("mycer.cer");
OkHttpClient mOkhttpClient= OkhttpManager.getInstance().build();
} catch (IOException e) {
e.printStackTrace();
}
}
No.2 可移除字符的最大数目
解题思路
二分法。对于给定的删除个数,可以 O(N) 地判断删除这些元素后,p 是否 s 的子序列。
代码展示
class Solution {
public int maximumRemovals(String s, String p, int[] removable) {
int l = 0, r = removable.length;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (check(s, p, removable, mid)) {
l = mid;
} else {
r = mid;
}
}
return check(s, p, removable, r) ? r : l;
}
private boolean check(String s, String p, int[] removable, int cnt) {
Set
for (int i = 0; i < cnt; i++) {
remove.add(removable[i]);
}
int j = 0;
for (int i = 0; i < s.length() && j < p.length(); i++) {
if (remove.contains(i)) {
continue;
}
if (s.charAt(i) == p.charAt(j)) {
j++;
}
}
return j == p.length();
}
}
No.3 合并若干三元组以形成目标三元组
解题思路
枚举一遍即可,取最大值。
代码展示
class Solution {
public boolean mergeTriplets(int[][] triplets, int[] target) {
int[] state = new int[3];
for (var triplet : triplets) {
if (triplet[0] <= target[0] && triplet[1] <= target[1] && triplet[2] <= target[2]) {
state[0] = Math.max(state[0], triplet[0]);
state[1] = Math.max(state[1], triplet[1]);
state[2] = Math.max(state[2], triplet[2]);
}
}
return Arrays.equals(state, target);
}
}
No.4 最佳运动员的比拼回合
解题思路
暴力枚举 + 状态压缩。
我们只需记录 firstPlayer 和 secondPlayer 的位置,枚举所有可能的胜负情况。
代码展示
class Solution {
public int[] earliestAndLatest(int n, int firstPlayer, int secondPlayer) {
int t = n, round = 0;
int min = -1, max = -1;
TreeSet
states.add(new Pair(firstPlayer, secondPlayer));
while (!states.isEmpty()) {
round++;
TreeSet
int newT = (t + 1) / 2;
for (var state : states) {
int l = state.first;
int r = state.second;
if (l + r == t + 1) {
min = min < 0 ? round : min;
max = round;
continue;
}
int len = t / 2;
for (int i = 0; i < (1 << len); i++) {
TreeSet
for (int j = 0; j < len; j++) {
rem.add((i & (1 << j)) > 0 ? j + 1 : t - j);
}
if (t % 2 == 1) {
rem.add(newT);
}
if (!rem.contains(l) || !rem.contains(r) || rem.size() != newT) {
continue;
}
int[] seq = rem.stream().mapToInt(elem -> elem.intValue()).toArray();
int nl = -1, nr = -1;
for (int j = 0; j < newT; j++) {
if (seq[j] == l) {
nl = j + 1;
}
if (seq[j] == r) {
nr = j + 1;
}
}
newStates.add(new Pair(nl, nr));
}
}
t = newT;
states = newStates;
}
return new int[] { min, max };
}
static class Pair implements Comparable
int first, second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Pair) {
Pair o = (Pair) obj;
return first == o.first && second == o.second;
}
return false;
}
@Override
public int hashCode() {
return first & second;
}
@Override
public int compareTo(Pair o) {
if (first != o.first) {
return first - o.first;
}
return second - o.second;
}
}
}