训练营5
This commit is contained in:
parent
1673d5c539
commit
4a59c75507
27
src/main/java/com/code/leet/contest/conterst5/Solution1.java
Normal file
27
src/main/java/com/code/leet/contest/conterst5/Solution1.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.code.leet.contest.conterst5;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class Solution1 {
|
||||
public static void main(String[] args) {
|
||||
Solution1 solution = new Solution1();
|
||||
}
|
||||
|
||||
public int maximumGoodsValue(int[][] goods, int maxWeight) {
|
||||
int[][] dp = new int[goods.length + 1][maxWeight + 1];
|
||||
for (int i = 1; i <= goods.length; i++) {
|
||||
for (int j = 1; j <= maxWeight; j++) {
|
||||
if (j >= goods[i - 1][0]) {
|
||||
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - goods[i - 1][0]] + goods[i - 1][1]);
|
||||
} else {
|
||||
dp[i][j] = dp[i - 1][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[goods.length][maxWeight];
|
||||
}
|
||||
}
|
||||
|
58
src/main/java/com/code/leet/contest/conterst5/Solution2.java
Normal file
58
src/main/java/com/code/leet/contest/conterst5/Solution2.java
Normal file
@ -0,0 +1,58 @@
|
||||
package com.code.leet.contest.conterst5;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class Solution2 {
|
||||
public static void main(String[] args) {
|
||||
Solution2 solution = new Solution2();
|
||||
System.out.println(solution.solve(new String[]{"XSXX", "X...", "XX..", "EX.."}));
|
||||
}
|
||||
|
||||
public int solve(String[] maze) {
|
||||
char[][] chs = new char[maze.length][maze[0].length()];
|
||||
int[][] use = new int[maze.length][maze[0].length()];
|
||||
for (int[] ints : use) {
|
||||
Arrays.fill(ints, -1);
|
||||
}
|
||||
Queue<int[]> queue = new LinkedList<>();
|
||||
for (int i = 0; i < maze.length; i++) {
|
||||
String str = maze[i];
|
||||
chs[i] = str.toCharArray();
|
||||
int index = str.indexOf("S");
|
||||
if (index >= 0) {
|
||||
queue.add(new int[]{i, index, 0});
|
||||
use[i][index] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int[] xArr = new int[]{-1, 1, 0, 0};
|
||||
int[] yArr = new int[]{0, 0, -1, 1};
|
||||
|
||||
int ex = 0;
|
||||
int ey = 0;
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
int[] arr = queue.poll();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int x = arr[0] + xArr[i];
|
||||
int y = arr[1] + yArr[i];
|
||||
if (x < 0 || x >= maze.length || y < 0 || y >= maze[0].length()) {
|
||||
continue;
|
||||
}
|
||||
int temp = arr[2] + (chs[arr[0]][arr[1]] == 'X' ? 1 : 0);
|
||||
if (use[x][y] == -1 || use[x][y] > arr[2]) {
|
||||
use[x][y] = temp;
|
||||
if (chs[x][y] != 'E') {
|
||||
queue.add(new int[]{x, y, temp});
|
||||
} else {
|
||||
ex = x;
|
||||
ey = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return use[ex][ey];
|
||||
}
|
||||
}
|
50
src/main/java/com/code/leet/contest/conterst5/Solution3.java
Normal file
50
src/main/java/com/code/leet/contest/conterst5/Solution3.java
Normal file
@ -0,0 +1,50 @@
|
||||
package com.code.leet.contest.conterst5;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
public class Solution3 {
|
||||
public static void main(String[] args) {
|
||||
Solution3 solution = new Solution3();
|
||||
System.out.println(solution.minimumSteps(new int[]{2, 5, 1, 2, 1, 1, 7}));
|
||||
}
|
||||
|
||||
public int minimumSteps(int[] nums) {
|
||||
Queue<Integer> queue = new LinkedList<>();
|
||||
queue.add(0);
|
||||
boolean[] use = new boolean[nums.length];
|
||||
use[0] = true;
|
||||
int count = 1;
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
int num = queue.poll();
|
||||
int step = nums[num];
|
||||
if (num + step >= nums.length - 1) {
|
||||
return count;
|
||||
}
|
||||
if (step > 0) {
|
||||
for (int j = 1; j <= step; j++) {
|
||||
if (!use[num + j]) {
|
||||
use[num + j] = true;
|
||||
queue.add(num + j);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int j = 1; j <= -step && j >= 0; j++) {
|
||||
if (!use[num - j]) {
|
||||
use[num - j] = true;
|
||||
queue.add(num - j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
59
src/main/java/com/code/leet/contest/conterst5/Solution4.java
Normal file
59
src/main/java/com/code/leet/contest/conterst5/Solution4.java
Normal file
@ -0,0 +1,59 @@
|
||||
package com.code.leet.contest.conterst5;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class Solution4 {
|
||||
public static void main(String[] args) {
|
||||
Solution4 solution = new Solution4();
|
||||
}
|
||||
|
||||
public int getDistance(String[] maze) {
|
||||
int[] xIndex = new int[]{-1, 1, 0, 0};
|
||||
int[] yIndex = new int[]{0, 0, -1, 1};
|
||||
char[][] chs = new char[maze.length][maze[0].length()];
|
||||
for (int i = 0; i < maze.length; i++) {
|
||||
chs[i] = maze[i].toCharArray();
|
||||
}
|
||||
boolean[][] use = new boolean[maze.length][maze[0].length()];
|
||||
Pair<Integer, Integer> start = null;
|
||||
for (int i = 0; i < chs.length; i++) {
|
||||
for (int j = 0; j < chs[0].length; j++) {
|
||||
if (chs[i][j] == 'X') {
|
||||
use[i][j] = true;
|
||||
}
|
||||
if (chs[i][j] == 'S') {
|
||||
use[i][j] = true;
|
||||
start = new Pair<>(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||
queue.add(start);
|
||||
int count = 0;
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
count++;
|
||||
for (int i = 0; i < size; i++) {
|
||||
Pair<Integer, Integer> pair = queue.poll();
|
||||
for (int j = 0; j < 4; j++) {
|
||||
int x = pair.getKey() + xIndex[j];
|
||||
int y = pair.getValue() + yIndex[j];
|
||||
if (x < 0 || x >= chs.length || y < 0 || y >= chs[0].length || use[x][y]) {
|
||||
continue;
|
||||
}
|
||||
if (chs[x][y] == '.') {
|
||||
queue.add(new Pair<>(x, y));
|
||||
use[x][y] = true;
|
||||
}
|
||||
if(chs[x][y]=='E'){
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
40
src/main/java/com/code/leet/contest/conterst5/Solution5.java
Normal file
40
src/main/java/com/code/leet/contest/conterst5/Solution5.java
Normal file
@ -0,0 +1,40 @@
|
||||
package com.code.leet.contest.conterst5;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Solution5 {
|
||||
public static void main(String[] args) {
|
||||
Solution5 solution = new Solution5();
|
||||
solution.shiftMatrix(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{3,1,9},{4,5,2},{7,8,6}});
|
||||
}
|
||||
|
||||
public int shiftMatrix(int[][] source, int[][] target) {
|
||||
Map<Integer, Pair<Integer, Integer>[]> map = new HashMap<>();
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
if (source[i][j] != target[i][j]) {
|
||||
Pair<Integer,Integer> pair = new Pair<>(i,j);
|
||||
Pair[] temp = map.getOrDefault(source[i][j], new Pair[2]);
|
||||
temp[0] = pair;
|
||||
map.put(source[i][j],temp);
|
||||
temp = map.getOrDefault(target[i][j], new Pair[2]);
|
||||
temp[1] = pair;
|
||||
map.put(target[i][j],temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
int max = 0;
|
||||
for (int key : map.keySet()) {
|
||||
Pair<Integer, Integer>[] pairs = map.get(key);
|
||||
int x = pairs[1].getKey() - pairs[0].getKey();
|
||||
int y = pairs[1].getValue() - pairs[0].getValue();
|
||||
x = x >= 0 ? x : x + 3;
|
||||
y = y >= 0 ? y : y + 3;
|
||||
max = Math.max(x + y, max);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
39
src/main/java/com/code/leet/contest/conterst5/Solution6.java
Normal file
39
src/main/java/com/code/leet/contest/conterst5/Solution6.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.code.leet.contest.conterst5;
|
||||
|
||||
import com.code.leet.entiy.TwoArray;
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Solution6 {
|
||||
public static void main(String[] args) {
|
||||
Solution6 solution = new Solution6();
|
||||
TwoArray twoArray = new TwoArray("[[2,3],[1,3],[3,4],[1,2]]", true);
|
||||
System.out.print(solution.solve(twoArray.getArr()));
|
||||
}
|
||||
|
||||
int max = Integer.MIN_VALUE;
|
||||
|
||||
public int solve(int[][] intervals) {
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
for (int[] interval : intervals) {
|
||||
List<Integer> list = map.getOrDefault(interval[0], new ArrayList<>());
|
||||
list.add(interval[1]);
|
||||
map.put(interval[0], list);
|
||||
}
|
||||
dfs(map, intervals.length, 0, 1);
|
||||
return max == Integer.MIN_VALUE ? -1 : max;
|
||||
}
|
||||
|
||||
private void dfs(Map<Integer, List<Integer>> map, int n, int count, int index) {
|
||||
if (index == n) {
|
||||
max = Math.max(max, count);
|
||||
return;
|
||||
}
|
||||
|
||||
List<Integer> list = map.getOrDefault(index, new ArrayList<>());
|
||||
for (Integer integer : list) {
|
||||
dfs(map, n, count + 1, integer);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user