竞赛整理

This commit is contained in:
huangge1199 2021-10-19 16:06:04 +08:00
parent d1a180bbab
commit dce12c390b
42 changed files with 39 additions and 343 deletions

View File

@ -1,24 +0,0 @@
//package com.code.leet.doubleWeek;
//
//public class MovieRentingSystem {
//
// public MovieRentingSystem(int n, int[][] entries) {
//
// }
//
// public List<Integer> search(int movie) {
//
// }
//
// public void rent(int shop, int movie) {
//
// }
//
// public void drop(int shop, int movie) {
//
// }
//
// public List<List<Integer>> report() {
//
// }
//}

View File

@ -1,136 +0,0 @@
package com.code.leet.week;
import java.util.*;
/**
* @Author: 轩辕龙儿
* @Date: 2021/4/10 15:07
* @Description:
*/
public class Lcp20210410 {
public int storeWater(int[] bucket, int[] vat) {
List<Integer> list = new ArrayList<>();
int count = 0;
int size = bucket.length;
for (int i = 0; i < size; i++) {
if (bucket[i] == 0) {
count++;
list.add(vat[i]);
} else {
list.add(vat[i] / bucket[i]);
}
}
return 0;
}
public static void main(String[] args) {
Lcp20210410 test = new Lcp20210410();
// System.out.println(test.maxGroupNumber(new int[]{2, 2, 2, 3, 4}));
System.out.println(test.electricCarPlan(new int[][]{{0, 4, 2}, {4, 3, 5}, {3, 0, 5}, {0, 1, 5}, {3, 2, 4}, {1, 2, 8}}, 8,
0, 2, new int[]{4, 1, 1, 3, 2}));
}
public int maxGroupNumber(int[] tiles) {
Map<Integer, Integer> map = new HashMap<>();
int min = Integer.MAX_VALUE;
int max = -1;
for (int num : tiles) {
min = Math.min(min, num);
max = Math.max(max, num);
map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1);
}
int count = 0;
int l0 = 0;
int l1 = map.get(min);
int l2 = map.getOrDefault(min + 1, 0);
int cur;
for (int i = min + 2; i <= max; i++) {
if (!map.containsKey(i)) {
count += l0 / 3 + l1 / 3 + l2 / 3;
l0 = l1 % 3;
l1 = l2 % 3;
l2 = 0;
continue;
}
cur = map.get(i);
if (l1 > 0 && l2 > 0) {
int tempMin = Math.min(cur, Math.min(l1, l2));
int tempCount = l1 / 3 + l2 / 3 + cur / 3;
if (tempMin >= tempCount) {
count += tempMin;
l0 = l1 - tempMin;
l1 = l2 - tempMin;
l2 = cur - tempMin;
} else {
count += l1 / 3;
l0 = l1 % 3;
l1 = l2;
l2 = cur;
}
} else {
count += l0 / 3 + l1 / 3;
l0 = l1 % 3;
l1 = l2;
l2 = cur;
}
}
return count + l1 / 3 + l2 / 3;
}
public int electricCarPlan(int[][] paths, int cnt, int start, int end, int[] charge) {
int arrSize = charge.length;
int[][] edges = new int[arrSize][arrSize];
int size = paths.length;
for (int i = 0; i < arrSize; i++) {
for (int j = 0; j < arrSize; j++) {
edges[i][j] = i == j ? 0 : Integer.MAX_VALUE;
}
}
int[] distance = new int[arrSize];
for (int i = 0; i < arrSize; i++) {
distance[i] = i == start ? 0 : Integer.MAX_VALUE;
}
for (int i = 0; i < size; i++) {
int num1 = paths[i][0];
int num2 = paths[i][1];
int tempLength = paths[i][2];
edges[num1][num2] = tempLength * (charge[num1] + 1);
edges[num2][num1] = tempLength * (charge[num2] + 1);
if (num1 == start) {
distance[num2] = edges[num1][num2];
}
}
boolean[] st = new boolean[arrSize];
// 处理从源点到其余顶点的最短路径
for (int i = 0; i < arrSize; ++i) {
int min = Integer.MAX_VALUE;
int index = -1;
// 比较从源点到其余顶点的路径长度
for (int j = 0; j < arrSize; ++j) {
// 从源点到j顶点的最短路径还没有找到
if (!st[j]) {
// 从源点到j顶点的路径长度最小
if (distance[j] < min) {
index = j;
min = distance[j];
}
}
}
//找到源点到索引为index顶点的最短路径长度
if (index != -1) {
st[index] = true;
}
// 更新当前最短路径及距离
for (int w = 0; w < arrSize; w++) {
if (!st[w]) {
if (edges[index][w] != Integer.MAX_VALUE
&& (min + edges[index][w] < distance[w])) {
distance[w] = min + edges[index][w];
}
}
}
}
return distance[end];
}
}

View File

@ -1,121 +0,0 @@
package com.code.leet.week;
import java.util.*;
/**
* @Author: 轩辕龙儿
* @Date: 2021/4/5 15:02
* @Description:
*/
public class LcpSolution {
public static void main(String[] args) {
LcpSolution solution = new LcpSolution();
// System.out.println(solution.orchestraLayout(4, 1, 2));
int[] nums = {2, 2, 3, 5};
int target = 6;
System.out.println(solution.purchasePlans(nums, target));
}
public int magicTower(int[] nums) {
long sum = 1;
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
List<Integer> list = new ArrayList<>();
int size = nums.length;
int count = 0;
for (int i = 0; i < size; i++) {
sum += nums[i];
if (nums[i] < 0) {
while (!stack1.isEmpty() && stack1.peek() < nums[i]) {
stack2.push(stack1.pop());
}
stack1.push(nums[i]);
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
}
if (sum <= 0) {
sum -= stack1.peek();
list.add(stack1.pop());
count++;
}
}
size = list.size();
for (int i = 0; i < size; i++) {
sum += list.get(i);
if (sum <= 0) {
return -1;
}
}
return count;
}
public int orchestraLayout(int num, int xPos, int yPos) {
int round = Math.min(Math.min(xPos, num - xPos - 1), Math.min(yPos, num - yPos - 1));
int result = 4 * num * round - 4 * round * round;
if (xPos == round) {
result += yPos - round + 1;
} else if (yPos == num - 1 - round) {
result += (num - 1 - round * 2) + xPos - round + 1;
} else if (xPos == num - 1 - round) {
result += (num - 1 - round * 2) * 2 + num - round - yPos;
} else {
result += (num - 1 - round * 2) * 3 + num - round - xPos;
}
return result % 9;
}
// public int purchasePlans(int[] nums, int target) {
// long count = 0;
// List<Integer> numList = Arrays.stream(nums).boxed().collect(Collectors.toList());
// Collections.sort(numList);
// Map<Integer, Long> map = new HashMap<>();
// int temp = -1;
// for (int num : numList) {
// if (num >= target) {
// break;
// }
// if (temp == -1) {
// for (int i = 1; i < num; i++) {
// map.put(i, Long.parseLong("0"));
// }
// map.put(num, Long.parseLong("1"));
// } else {
// if (map.containsKey(target - num)) {
// count += map.get(target - num);
// } else {
// count += map.get(temp);
// }
// for (int i = temp + 1; i < num; i++) {
// map.put(i, map.get(temp));
// }
// map.put(num, map.get(temp) + 1);
// }
// temp = num;
// }
// return (int) (count % (Math.pow(10, 9) + 7));
// }
public int purchasePlans(int[] nums, int target) {
int[] sort = new int[target];
long[] count = new long[target];
for (int num : nums) {
if (num < target) {
sort[num] += 1;
}
}
long sum = 0;
for (int i = 1; i < target; i++) {
sum += sort[i];
count[i] = sum;
}
long result = 0;
for (int num : nums) {
if (target > num) {
result += num <= target - num ? count[target - num] - 1 : count[target - num];
}
}
Calendar calendar = Calendar.getInstance();
return (int) (result / 2 % (Math.pow(10, 9) + 7));
}
}

View File

@ -1,7 +1,6 @@
package com.code.leet;
package contest.y2021.fall.person;
import com.code.leet.entiy.TwoArray;
import javafx.util.Pair;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.code.leet.season.fall2021.contest1;
package contest.y2021.fall.term.contest1;
import java.util.HashSet;
import java.util.Set;

View File

@ -1,9 +1,7 @@
package com.code.leet.season.fall2021.contest2;
package contest.y2021.fall.term.contest2;
import com.code.leet.entiy.TwoArray;
import javafx.util.Pair;
import java.awt.*;
import java.util.*;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.code.leet.season.fall2021.contest3;
package contest.y2021.fall.term.contest3;
import com.code.leet.entiy.TwoArray;

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m04.dw;
import java.util.HashMap;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m04.dw;
/**
* @Author: 轩辕龙儿

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m04.week;
import java.util.*;
import java.util.stream.Collectors;

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m04.week;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m04.week;
import java.util.Arrays;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m05.dw;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m05.dw;
import java.util.*;
import java.util.stream.Collectors;

View File

@ -1,7 +1,6 @@
package com.code.leet.week;
package contest.y2021.m05.week;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Author: 轩辕龙儿

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m05.week;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m05.week;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m05.week;
public class Solution242 {
public static void main(String[] args) {

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m05.week;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m06.dw;
public class SolutionD54 {
public static void main(String[] args) {

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m06.dw;
import java.util.Arrays;

View File

@ -1,6 +1,4 @@
package com.code.leet.week;
import com.code.leet.doubleWeek.SolutionD54;
package contest.y2021.m06.week;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m06.week;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m07.dw;
import javafx.util.Pair;

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m07.dw;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m07.week;
import java.util.Arrays;
import java.util.HashMap;

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m07.week;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m08.dw;
public class SolutionD58 {
public static void main(String[] args) {

View File

@ -1,7 +1,4 @@
package com.code.leet.doubleWeek;
import com.code.leet.entiy.TwoArray;
import javafx.util.Pair;
package contest.y2021.m08.dw;
import java.util.HashMap;
import java.util.Map;

View File

@ -1,7 +1,6 @@
package com.code.leet.week;
package contest.y2021.m08.week;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
/**

View File

@ -1,10 +1,7 @@
package com.code.leet.week;
package contest.y2021.m08.week;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* @description:

View File

@ -1,6 +1,5 @@
package com.code.leet.doubleWeek;
package contest.y2021.m09.dw;
import java.math.BigInteger;
import java.util.*;
public class SolutionD60 {

View File

@ -1,8 +1,5 @@
package com.code.leet.week;
package contest.y2021.m09.week;
import com.code.leet.entiy.TwoArray;
import java.math.BigInteger;
import java.util.*;
/**

View File

@ -1,6 +1,4 @@
package com.code.leet.week;
import javafx.util.Pair;
package contest.y2021.m09.week;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m09.week;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m09.week;
import com.code.leet.entiy.TwoArray;

View File

@ -1,6 +1,5 @@
package com.code.leet.doubleWeek;
package contest.y2021.m10.dw;
import java.math.BigInteger;
import java.util.*;
public class SolutionD62 {

View File

@ -1,4 +1,4 @@
package com.code.leet.doubleWeek;
package contest.y2021.m10.dw;
import java.util.*;

View File

@ -1,8 +1,6 @@
package com.code.leet.week;
package contest.y2021.m10.week;
import com.code.leet.entiy.TwoArray;
import java.util.*;
/**

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m10.week;
import java.util.ArrayList;
import java.util.Arrays;

View File

@ -1,8 +1,7 @@
package com.code.leet.week;
package contest.y2021.m10.week;
import com.code.leet.entiy.TwoArray;
import java.math.BigInteger;
import java.util.*;
/**

View File

@ -1,4 +1,4 @@
package com.code.leet.week;
package contest.y2021.m10.week;
import java.util.HashMap;
import java.util.Map;