双周赛79
This commit is contained in:
parent
bdfccc7f4e
commit
28523261e6
79
src/main/java/contest/y2022/m5/dw/BookMyShow.java
Normal file
79
src/main/java/contest/y2022/m5/dw/BookMyShow.java
Normal file
@ -0,0 +1,79 @@
|
||||
package contest.y2022.m5.dw;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* leet-code
|
||||
*
|
||||
* @author 轩辕龙儿
|
||||
* @date 2022/5/28 下午11:27
|
||||
*/
|
||||
public class BookMyShow {
|
||||
TreeSet<int[]> treeSet = new TreeSet<>((o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0]);
|
||||
|
||||
public BookMyShow(int n, int m) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
treeSet.add(new int[]{i, j});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int[] gather(int k, int maxRow) {
|
||||
Stack<int[]> stack = new Stack<>();
|
||||
int nums = k;
|
||||
for (int i = 0; i <= maxRow && nums > 0; i++) {
|
||||
for (int[] arr : treeSet.tailSet(new int[]{i, 0})) {
|
||||
if (stack.isEmpty()) {
|
||||
stack.add(arr);
|
||||
nums--;
|
||||
} else {
|
||||
int[] tmp = stack.peek();
|
||||
if (tmp[0] > maxRow) {
|
||||
break;
|
||||
}
|
||||
if (tmp[0] == arr[0] && arr[1] - tmp[1] == 1) {
|
||||
stack.add(arr);
|
||||
nums--;
|
||||
} else {
|
||||
stack = new Stack<>();
|
||||
stack.add(arr);
|
||||
nums = k - 1;
|
||||
}
|
||||
}
|
||||
if (nums == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
int[] arr = new int[2];
|
||||
if (nums == 0) {
|
||||
arr = stack.firstElement();
|
||||
while (!stack.isEmpty()) {
|
||||
treeSet.remove(stack.pop());
|
||||
}
|
||||
}
|
||||
return nums == 0 ? arr : new int[0];
|
||||
}
|
||||
|
||||
public boolean scatter(int k, int maxRow) {
|
||||
if (treeSet.size() < k) {
|
||||
return false;
|
||||
}
|
||||
Stack<int[]> stack = new Stack<>();
|
||||
int nums = k;
|
||||
while (!treeSet.isEmpty() && nums > 0) {
|
||||
int[] arr = treeSet.first();
|
||||
if (arr[0] > maxRow) {
|
||||
while (!stack.isEmpty()) {
|
||||
treeSet.add(stack.pop());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
stack.add(arr);
|
||||
treeSet.remove(arr);
|
||||
nums--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
72
src/main/java/contest/y2022/m5/dw/Solution79.java
Normal file
72
src/main/java/contest/y2022/m5/dw/Solution79.java
Normal file
@ -0,0 +1,72 @@
|
||||
package contest.y2022.m5.dw;
|
||||
|
||||
import com.code.leet.entiy.TwoArray;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* leet-code
|
||||
*
|
||||
* @author 轩辕龙儿
|
||||
* @date 2022/5/28 下午10:18
|
||||
*/
|
||||
public class Solution79 {
|
||||
public boolean digitCount(String num) {
|
||||
int[] arrs = new int[10];
|
||||
for (char ch : num.toCharArray()) {
|
||||
arrs[ch - '0']++;
|
||||
}
|
||||
for (int i = 0; i < num.length(); i++) {
|
||||
if (arrs[i] != (num.charAt(i) - '0')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String largestWordCount(String[] messages, String[] senders) {
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < senders.length; i++) {
|
||||
map.put(senders[i], map.getOrDefault(senders[i], 0) + messages[i].split(" ").length);
|
||||
}
|
||||
List<String> list = new ArrayList<>();
|
||||
int max = 0;
|
||||
for (String key : map.keySet()) {
|
||||
if (map.get(key) > max) {
|
||||
max = map.get(key);
|
||||
list = new ArrayList<>();
|
||||
list.add(key);
|
||||
} else if (map.get(key) == max) {
|
||||
list.add(key);
|
||||
}
|
||||
}
|
||||
Collections.sort(list);
|
||||
return list.get(list.size() - 1);
|
||||
}
|
||||
|
||||
public long maximumImportance(int n, int[][] roads) {
|
||||
int[] nums = new int[n];
|
||||
for (int[] road : roads) {
|
||||
nums[road[0]]++;
|
||||
nums[road[1]]++;
|
||||
}
|
||||
Arrays.sort(nums);
|
||||
long result = 0;
|
||||
int num = 1;
|
||||
for (int i = 0; i < n; i++, num++) {
|
||||
result += (long) nums[i] * num;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Solution79 solution = new Solution79();
|
||||
// TwoArray roads = new TwoArray("[[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]",true);
|
||||
// System.out.println(solution.maximumImportance(5, roads.getArr()));
|
||||
BookMyShow bookMyShow = new BookMyShow(2,5);
|
||||
bookMyShow.gather(4,0);
|
||||
bookMyShow.gather(2,0);
|
||||
bookMyShow.scatter(5,1);
|
||||
bookMyShow.scatter(5,1);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user