第一题
public class 第一题01 {
public static class TimePoint{
public int time;//开始时间
public int end;//结束时间
public boolean add;//true:开 false:闭
public TimePoint(int time,int end,boolean add){
this.time = time;
this.end = end;
this.add = add;
}
}
public static boolean canDo(int[][] jobs){
int n = jobs.length;
TimePoint[] arr = new TimePoint[2 * n];
PriorityQueue<Integer> queue = new PriorityQueue<>();
for (int i = 0;i < n;i++){
arr[i] = new TimePoint(jobs[i][0],jobs[i][1],true);
arr[i + n] = new TimePoint(jobs[i][1],jobs[i][1],false);
}
Arrays.sort(arr,(a,b) -> a.time - b.time);
for (int i = 0,lastTime = arr[0].time;i < arr.length;i++){
if (arr[i].add){
queue.add(arr[i].end);
}else {
int curTime = arr[i].time;
for (int j = lastTime;j < curTime;j++){
if (queue.isEmpty()){
break;
}
queue.poll();
}
lastTime = curTime;
if (!queue.isEmpty() && queue.peek() <= curTime){
return false;
}
}
}
return true;
}
public static void main(String[] args) {
int[][] jobs = {{1,3},{1,3},{2,5},{3,6}};
boolean res = canDo(jobs);
System.out.println(res);
}
}
第二题
public class 第二题01 {
public static void main(String[] args) {
int[] arr = {1,-2,3,4,-6,5,7,-12,8};
int res = maxSumReverse(arr);
System.out.println(res);
}
public static int maxSumReverse(int[] arr){
int n = arr.length;
int[] dp = new int[n];
dp[n - 1] = arr[n - 1];
for (int i = n - 2;i >= 0;i--){
dp[i] = arr[i] + Math.max(dp[i + 1],0);
}
int ans = dp[0];
int suffix = arr[0];
int maxSuffix = arr[0];
for (int i = 1;i < n;i++){
ans = Math.max(ans,dp[i] + maxSuffix);
suffix = arr[i] + Math.max(0,suffix);
maxSuffix = Math.max(maxSuffix,suffix);
}
ans = Math.max(ans,maxSuffix);
return ans;
}
}
第三题
public class 第三题01 {
public static void main(String[] args) {
int[] arr = {1,5,7,3,6,2,5,3};
String res = maxNum3(arr);
System.out.println(res);
}
public static String maxNum3(int[] arr){
int n = arr.length;
int sum = 0;
for (int i = 0;i < n;i++) {
sum += arr[i];
}
if (sum % 3 == 0){
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
for (int i = n - 1;i >= 0;i--){
sb.append(arr[i]);
}
return sb.toString();
}else {
PriorityQueue<Integer> queue_1 = new PriorityQueue<>((a, b) -> arr[a] - arr[b]);
PriorityQueue<Integer> queue_2 = new PriorityQueue<>((a, b) -> arr[a] - arr[b]);
PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> arr[b] - arr[a]);
if (sum % 3 == 1){
for (int i = 0;i < n;i++){
if (arr[i] % 3 == 1){
queue_1.add(i);
}else if (arr[i] % 3 == 2){
queue_2.add(i);
}else {
queue.add(i);
}
}
if (!queue_1.isEmpty()){
queue_1.poll();
}else {
queue_2.poll();
queue_2.poll();
}
while (!queue_1.isEmpty()){
queue.add(queue_1.poll());
}
while (!queue_2.isEmpty()){
queue.add(queue_2.poll());
}
StringBuilder sb = new StringBuilder();
while (!queue.isEmpty()){
sb.append(arr[queue.poll()]);
}
return sb.toString();
}else {
for (int i = 0;i < n;i++){
if (arr[i] % 3 == 2){
queue_1.add(i);
}else if (arr[i] % 3 == 1){
queue_2.add(i);
}else {
queue.add(i);
}
}
if (!queue_1.isEmpty()){
queue_1.poll();
}else {
queue_2.poll();
queue_2.poll();
}
while (!queue_1.isEmpty()){
queue.add(queue_1.poll());
}
while (!queue_2.isEmpty()){
queue.add(queue_2.poll());
}
StringBuilder sb = new StringBuilder();
while (!queue.isEmpty()){
sb.append(arr[queue.poll()]);
}
return sb.toString();
}
}
}
}
第四题
public class 第四题01 {
public static void main(String[] args) {
int[] A = {3,2,5,0,7};
int[] B = {1,4,2,8,9};
boolean res = canSort1(A, B);
System.out.println(res);
}
//递归
public static boolean canSort1(int[] A,int[] B){
if (A == null || A.length < 2){
return true;
}
return process1(A,B,1,true) || process1(A,B,1,false);
}
private static boolean process1(int[] A, int[] B, int index, boolean swap) {
if (index == A.length){
return true;
}
int pre = (swap ? B[index - 1] : A[index - 1]);
boolean p1 = (B[index] >= pre ? process1(A,B,index + 1,true) : false);
boolean p2 = (A[index] >= pre ? process1(A,B,index + 1,false) : false);
return p1 || p2;
}
}
第五题
public class 第五题01 {
//样本进来会包一层,叫做元素
public static class Element {
public int value;
public int x_max;
public int x_min;
public int y_max;
public int y_min;
public Element(int value,int[] local,int radius) {
this.value = value;
this.x_max = local[0] + radius;
this.x_min = local[0] - radius;
this.y_max = local[1] + radius;
this.y_min = local[1] - radius;
}
}
public static class UnionFindSet {
public HashMap<Integer, Element> elementMap;
//key 某个元素 value 该元素的父亲
public HashMap<Element, Element> fatherMap;
//key 某个集合的代表元素,value 该集合的大小
public HashMap<Element, Integer> sizeMap;
public UnionFindSet(int[][] holes,int radius) {
elementMap = new HashMap<>();
fatherMap = new HashMap<>();
sizeMap = new HashMap<>();
for (int i = 0;i < holes.length;i++){
Element element = new Element(i,holes[i], radius);
elementMap.put(i, element);
fatherMap.put(element, element);
sizeMap.put(element, 1);
}
}
//给定一个ele,往上一直找,将代表元素返回
private Element findHead(Element element) {
Stack<Element> path = new Stack<>();
while (element != fatherMap.get(element)) {
path.push(element);
element = fatherMap.get(element);
}
//将遍历的整条链的父都设置为找到的代表元素
//即将其扁平化(向上找优化)
while (!path.isEmpty()) {
fatherMap.put(path.pop(), element);
}
return element;
}
public boolean isSameSet(Integer a, Integer b) {
if (elementMap.containsKey(a) && elementMap.containsKey(b)) {
return findHead(elementMap.get(a)) == findHead(elementMap.get(b));
}
return false;
}
public void union(Integer a, Integer b) {
if (elementMap.containsKey(a) && elementMap.containsKey(b)) {
Element aF = findHead(elementMap.get(a));
Element bF = findHead(elementMap.get(b));
if (aF != bF) {
Element big = sizeMap.get(aF) >= sizeMap.get(bF) ? aF : bF;
Element small = big == aF ? bF : aF;
big.x_max = Math.max(big.x_max,small.x_max);
big.x_min = Math.min(big.x_min,small.x_min);
big.y_max = Math.max(big.y_max,small.y_max);
big.y_min = Math.min(big.y_min,small.y_min);
fatherMap.put(small, big);
sizeMap.put(big, sizeMap.get(aF) + sizeMap.get(bF));
sizeMap.remove(small);
}
}
}
}
public static int findMinlen(int[][] holes,int x,int y){
int maxlen = Math.max(x,y);
int l = 0;
int r = maxlen;
while (l < r){
int mid = (l + r + 1) / 2;
if (isReceived(holes,mid,x,y)){
l = mid;
}else {
r = mid - 1;
}
}
return l;
}
/**
* 是否可以到达
* @param holes
* @param mid
* @param x
* @param y
* @return
*/
private static boolean isReceived(int[][] holes, int mid, int x, int y) {
int n = holes.length;
UnionFindSet unionFindSet = new UnionFindSet(holes, mid);
for (int i = 0;i < n;i++){
for (int j = i + 1;j < n;j++){
if (isSameRound(holes[i],holes[j],mid)){
unionFindSet.union(i,j);
}
}
}
Set<Element> elements = unionFindSet.sizeMap.keySet();
for (Element element : elements){
if (!isHasGap(element,x,y)){
return false;
}
}
return true;
}
/**
* 两个圆是否相交
* @param hole
* @param hole1
* @param mid
* @return
*/
private static boolean isSameRound(int[] hole, int[] hole1, int mid) {
double sqrt = Math.sqrt((hole[0] - hole1[0]) * (hole[0] - hole1[0]) + (hole[1] - hole1[1]) * (hole[1] - hole1[1]));
if (sqrt >= mid){
return true;
}
return false;
}
/**
* 是否有间隙
* @param element
* @param x
* @param y
* @return
*/
private static boolean isHasGap(Element element, int x, int y) {
if (element.x_min <= 0 && element.x_max >= x){
return false;
}
if (element.y_min <= 0 && element.y_max >= y){
return false;
}
if (element.x_min <= 0 && element.y_min <= 0 && element.x_max >= 0 && element.y_max >= 0){
return false;
}
if (element.x_max >= x && element.y_max >= y && element.x_min <= x && element.y_min <= y){
return false;
};
return true;
}
public static void main(String[] args) {
int[][] holes = {{3,5},{6,9}};
int minlen = findMinlen(holes, 12, 12);
System.out.println(minlen);
/* boolean sameRound1 = isSameRound1(3, 5, 6, 9, 4);
System.out.println(sameRound1);*/
}
private static boolean isSameRound1(int x1, int y1,int x2,int y2, int mid) {
double sqrt = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (sqrt >= mid){
return true;
}
return false;
}
}