# Disembarkation algorithm | leetcode weekly contest problem solving report for game 256

Time：2021-10-22

[No.1 minimum difference of student scores]

Problem solving ideas

Sort, and then enumerate every successive k elements.

Code display

class Solution {
public int minimumDifference(int[] nums, int k) {

``````   if (nums.length < 2 || k == 1) {
return 0;
}
Arrays.sort(nums);
int res = nums[k - 1] - nums[0];
for (int i = k; i < nums.length; i++) {
res = Math.min(res, nums[i] - nums[i - k + 1]);
}
return res;``````

}
}
[No.2 find the k-th integer in the array]

Problem solving ideas

Sort the values from large to small.

Code display

class Solution {
public String kthLargestNumber(String[] nums, int k) {

``````   Arrays.sort(nums, (a, b) -> {
if (a.length() != b.length()) {
return b.length() - a.length();
}
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) != b.charAt(i)) {
return b.charAt(i) - a.charAt(i);
}
}
return 0;
});
return nums[k - 1];``````

}
}
[No.3 minimum working time to complete the task]

Problem solving ideas

State compression dynamic programming, and DP [i] represents the minimum working time required when the remaining task set is I.

State transition is to enumerate the tasks to be done in the next working time period, that is, DP [i] = min (DP [J]) + 1, where the task represented by set I minus set J can be completed in one working time period.

Code display

class Solution {
public int minSessions(int[] tasks, int sessionTime) {

``````   int[] mem = new int[1 << tasks.length];
Arrays.fill(mem, -1);
mem[0] = 0;

}

private int dp(int i, int[] tasks, int sessionTime, int[] mem) {

``````if (mem[i] >= 0) {
return mem[i];
}
//Enumerate which tasks are completed in this time period
for (int j = 1; j < (1 << tasks.length); j++) {
if ((i | j) != i) {
continue;
}
int tot = 0;
int ni = i;
for (int k = 0; k < tasks.length; k++) {
if (((1 << k) & j) > 0) {
ni -= 1 << k;
}
}
if (tot <= sessionTime) {
mem[i] = Math.min(mem[i], dp(ni, tasks, sessionTime, mem) + 1);
}
}
return mem[i];``````

}
}
However, the above code will time out, so we add a small Optimization: enumerating J in reverse order, that is, enumerating larger sets first, and judging before recursive calculation, if a set containing J has been recursive, recursive calculation will not be carried out again – greedy idea.
class Solution {
public int minSessions(int[] tasks, int sessionTime) {

``````   int[] mem = new int[1 << tasks.length];
Arrays.fill(mem, -1);
mem[0] = 0;

}

private int dp(int i, int[] tasks, int sessionTime, int[] mem) {

``````   if (mem[i] >= 0) {
return mem[i];
}
List<Integer> visited = new ArrayList<>();
for (int j = (1 << tasks.length) - 1; j > 0; j--) {
if ((i | j) != i) {
continue;
}
boolean skip = false;
for (int v : visited) {
if ((v | j) == v) {
skip = true;
break;
}
}
if (skip) {
continue;
}
int tot = 0;
int ni = i;
for (int k = 0; k < tasks.length; k++) {
if (((1 << k) & j) > 0) {
ni -= 1 << k;
}
}
if (tot <= sessionTime) {
mem[i] = Math.min(mem[i], dp(ni, tasks, sessionTime, mem) + 1);
}
}
return mem[i];``````

}
}
[No.4 restore array from the sum of subsets]

Problem solving ideas

This topic is equivalent to the follow up of different subsequences II

You can refer to the official solution of this topic first

Code display

class Solution {
public int numberOfUniqueGoodSubsequences(String binary) {

``````   final long mod = (long) (1e9 + 7);
long res = 0;
long[] last = {0, 0};
for (char c : binary.toCharArray()) {
int i = c - '0';
long cur = (res + i - last[i] + mod) % mod;
res = (res + cur) % mod;
last[i] = (last[i] + cur) % mod;
}
if (binary.contains("0")) {
res = (res + 1) % mod;
}
return (int) res;``````

}
}

## [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 […]