# Disembarkation algorithm leetcode weekly contest 257 problem solving Report

Time：2021-10-17

Problem solving ideas
Check in questions, just enumerate.

Code display

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 number of weak characters in the game]

Problem solving ideas
Sort according to attack power and defense power from small to large, and then count in reverse order. Pay attention to dealing with situations with the same attack power.

Code display

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 indicates the maximum defense of a character greater than 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 first day after visiting all rooms]

Problem solving ideas
Dynamic planning, DP [i] indicates the minimum number of days to visit the ith room.

Code display

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];``````

}
}

[order of maximum common factor of No.4 array]

Problem solving ideas
As long as there is a common factor between elements, they can be sorted arbitrarily. So we will sort the elements with the same common factor, and finally see whether the sequence is orderly as a whole.

Code display

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<Integer, List<Integer>> sets() {

``````   Map<Integer, List<Integer>> 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<Integer, List<Integer>> 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;``````

}
}

## [hematemesis finishing] Super complete golang interview questions collection + golang Learning Guide + golang knowledge map + growth route

The brain map is constantly updated. Check the address onlineSubsequent articles and contents will be updated toGitHub projectWelcome to pay attention. Directory (Ctrl + F) Basic introduction Novice 50 mistakes that golang novices often make data type I don’t even know that nil slice is different from empty slice? Then the bat interviewer has to […]