# 上岸算法LeetCode Weekly Contest 257解题报告

【 NO.1 统计特殊四元组】

class Solution {

int n = nums.length;
int res = 0;
for (int a = 0; a < n; a++) {
for (int b = a + 1; b < n; b++) {
for (int c = b + 1; c < n; c++) {
for (int d = c + 1; d < n; d++) {
if (nums[a] + nums[b] + nums[c] == nums[d]) {
res++;
}
}
}
}
}
return res;

}
}

【 NO.2 游戏中弱角色的数量】

class Solution {
public int numberOfWeakCharacters(int[][] properties) {

Arrays.sort(properties, (a, b) -> {
if (a[0] == b[0]) {
return a[1] - b[1];
}
return a[0] - b[0];
});
int res = 0;
int lastAttack = properties[properties.length - 1][0];
int lastDefense = properties[properties.length - 1][1];
int maxDefense = 0; // maxDefense 表示大于 lastAttack 的角色中，最大的防御力
for (int i = properties.length - 2; i >= 0; i--) {
if (properties[i][0] < lastAttack) {
maxDefense = Math.max(maxDefense, lastDefense);
lastAttack = properties[i][0];
lastDefense = properties[i][1];
}
if (properties[i][1] < maxDefense) {
res++;
}
}
return res;

}
}

【 NO.3 访问完所有房间的第一天】

class Solution {
public int firstDayBeenInAllRooms(int[] nextVisit) {

int n = nextVisit.length;
long[] dp = new long[n];
long P = (long) (1e9 + 7);
for (int i = 1; i < n; ++i) {
dp[i] = (2 * dp[i - 1] - dp[nextVisit[i - 1]] + 2 + P) % P;
}
return (int) dp[n - 1];

}
}

【 NO.4 数组的最大公因数排序】

class UnionFind {
public UnionFind(int size) {

f = new int[size];
Arrays.fill(f, -1);

}

public int find(int x) {

if (f[x] < 0)
return x;
return f[x] = find(f[x]);

}

public boolean merge(int a, int b) {

int fa = find(a);
int fb = find(b);
if (fa == fb)
return false;
f[fa] = fb;
return true;

}

public Map> sets() {

Map> res = new HashMap<>();
for (int i = 0; i < f.length; i++) {
int fi = find(i);
if (!res.containsKey(fi)) {
res.put(fi, new ArrayList<>());
}
}
return res;

}

private int[] f;
}

class Solution {
public boolean gcdSort(int[] nums) {

Map> set = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
for (int j = 1; j * j <= nums[i]; j++) {
if (nums[i] % j == 0) {
if (!set.containsKey(j)) {
set.put(j, new ArrayList<>());
}
if (j * j < nums[i]) {
int k = nums[i] / j;
if (!set.containsKey(k)) {
set.put(k, new ArrayList<>());
}
}
}
}
}

UnionFind uf = new UnionFind(nums.length);
for (var e : set.entrySet()) {
if (e.getKey() < 2) {
continue;
}
var list = e.getValue();
for (int i = 1; i < list.size(); i++) {
uf.merge(list.get(i - 1), list.get(i));
}
}
var sets = uf.sets();
int[] res = new int[nums.length];
for (var e : sets.entrySet()) {
var list = e.getValue();
var sortedList = new ArrayList<>(list);
sortedList.sort(Comparator.comparingInt(a -> nums[a]));
for (int i = 0; i < list.size(); i++) {
res[list.get(i)] = nums[sortedList.get(i)];
}
}
for (int i = 1; i < res.length; i++) {
if (res[i] < res[i - 1]) {
return false;
}
}
return true;

}
}