From 4a59c755074321ae5d10f8f03b3eca267bbcb8d1 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Sun, 8 Aug 2021 22:18:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=AD=E7=BB=83=E8=90=A55?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leet/contest/conterst5/Solution1.java | 27 +++++++++ .../leet/contest/conterst5/Solution2.java | 58 ++++++++++++++++++ .../leet/contest/conterst5/Solution3.java | 50 ++++++++++++++++ .../leet/contest/conterst5/Solution4.java | 59 +++++++++++++++++++ .../leet/contest/conterst5/Solution5.java | 40 +++++++++++++ .../leet/contest/conterst5/Solution6.java | 39 ++++++++++++ 6 files changed, 273 insertions(+) create mode 100644 src/main/java/com/code/leet/contest/conterst5/Solution1.java create mode 100644 src/main/java/com/code/leet/contest/conterst5/Solution2.java create mode 100644 src/main/java/com/code/leet/contest/conterst5/Solution3.java create mode 100644 src/main/java/com/code/leet/contest/conterst5/Solution4.java create mode 100644 src/main/java/com/code/leet/contest/conterst5/Solution5.java create mode 100644 src/main/java/com/code/leet/contest/conterst5/Solution6.java diff --git a/src/main/java/com/code/leet/contest/conterst5/Solution1.java b/src/main/java/com/code/leet/contest/conterst5/Solution1.java new file mode 100644 index 0000000..2c556f4 --- /dev/null +++ b/src/main/java/com/code/leet/contest/conterst5/Solution1.java @@ -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]; + } +} + diff --git a/src/main/java/com/code/leet/contest/conterst5/Solution2.java b/src/main/java/com/code/leet/contest/conterst5/Solution2.java new file mode 100644 index 0000000..afaf9ef --- /dev/null +++ b/src/main/java/com/code/leet/contest/conterst5/Solution2.java @@ -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 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]; + } +} diff --git a/src/main/java/com/code/leet/contest/conterst5/Solution3.java b/src/main/java/com/code/leet/contest/conterst5/Solution3.java new file mode 100644 index 0000000..b60543d --- /dev/null +++ b/src/main/java/com/code/leet/contest/conterst5/Solution3.java @@ -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 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; + } +} diff --git a/src/main/java/com/code/leet/contest/conterst5/Solution4.java b/src/main/java/com/code/leet/contest/conterst5/Solution4.java new file mode 100644 index 0000000..5235529 --- /dev/null +++ b/src/main/java/com/code/leet/contest/conterst5/Solution4.java @@ -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 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> 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 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; + } +} diff --git a/src/main/java/com/code/leet/contest/conterst5/Solution5.java b/src/main/java/com/code/leet/contest/conterst5/Solution5.java new file mode 100644 index 0000000..1f33479 --- /dev/null +++ b/src/main/java/com/code/leet/contest/conterst5/Solution5.java @@ -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[]> 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 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[] 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; + } +} diff --git a/src/main/java/com/code/leet/contest/conterst5/Solution6.java b/src/main/java/com/code/leet/contest/conterst5/Solution6.java new file mode 100644 index 0000000..5c782ec --- /dev/null +++ b/src/main/java/com/code/leet/contest/conterst5/Solution6.java @@ -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> map = new HashMap<>(); + for (int[] interval : intervals) { + List 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> map, int n, int count, int index) { + if (index == n) { + max = Math.max(max, count); + return; + } + + List list = map.getOrDefault(index, new ArrayList<>()); + for (Integer integer : list) { + dfs(map, n, count + 1, integer); + } + } +}