第一题
leetcode499:迷宫2
思路:宽度优先遍历
public class 第一题15 {
public static void main(String[] args) {
int[][] maze = {{0,0,0,0,0},{1,1,0,0,1},{0,0,0,0,0},{0,1,0,0,1},{0,1,0,0,0}};
int[] hole = {0,1};
int[] ball = {4,3};
String res = findShortestWay(maze, hole, ball);
System.out.println(res);
}
public static int[][] to = {{1,0},{0,-1},{0,1},{-1,0},{0,0}};
public static String[] re = {"d","l","r","u"};
static class Node{
public int x;
public int y;
public int d;//0,1,2,3,4 4 代表初始位置
public String p;//路径
public Node(int x,int y,int d,String p){
this.x = x;
this.y = y;
this.d = d;
this.p = p;
}
}
public static String findShortestWay(int[][] maze,int[] hole,int[] ball){
int n = maze.length;
int m = maze[0].length;
boolean[][][] visit = new boolean[n][m][4];
int s1 = 0;
int s2 = 0;
Node[] p1 = new Node[n * m];
Node[] p2 = new Node[n * m];
s1 = spread(maze,n,m,visit,new Node(ball[0],ball[1],4,""),p1,s1);
while (s1 != 0){
for (int i = 0;i < s1;i++){
Node cur = p1[i];
if (cur.x == hole[0] && cur.y == hole[1]){
return cur.p;
}
s2 = spread(maze,n,m,visit,cur,p2,s2);
}
Node[] temp = p1;
p1 = p2;
p2 = temp;
s1 = s2;
s2 = 0;
}
return "impossible";
}
private static int spread(int[][] maze, int n, int m, boolean[][][] visit, Node cur, Node[] p2, int s2) {
int d = cur.d;
int x = cur.x + to[d][0];
int y = cur.y + to[d][1];
if (d == 4 || x < 0 || x == n || y < 0 || y == m || maze[x][y] != 0){
for (int i = 0;i < 4;i++){
if (i != d){
x = cur.x + to[i][0];
y = cur.y + to[i][1];
if (x >= 0 && x < n && y >= 0 && y < m && !visit[x][y][i]){
visit[x][y][i] = true;
p2[s2++] = new Node(x,y,i,cur.p + re[i]);
}
}
}
}else {
if (!visit[x][y][d]){
visit[x][y][d] = true;
p2[s2++] = new Node(x,y,d,cur.p);
}
}
return s2;
}
}
第二题
leetcode446. 等差数列划分 II - 子序列
public class 第二题15 {
public static int numberOfArithmeticSlices(int[] nums) {
int n = nums.length;
if (n < 3){
return 0;
}
HashMap<Long,Integer>[] dp = new HashMap[n];
for (int i = 0;i < n;i++){
dp[i] = new HashMap<>();
}
int res = 0;
for (int i = 1;i < n;i++){
for (int j = 0;j < i;j++){
long diff = (long) nums[i] - nums[j];
if (diff > Integer.MAX_VALUE || diff < Integer.MIN_VALUE){
continue;
}
dp[i].put(diff,dp[i].getOrDefault(diff,0) + dp[j].getOrDefault(diff,0) + 1);
if (dp[j].containsKey(diff)){
res += dp[j].get(diff);
}
}
}
return res;
}
}
第三题
leetcode489. 扫地机器人
public class 第三题15 {
interface Robot{
public boolean move();
public void turnLeft();
public void turnRight();
public void clean();
}
public static int[][] ds = {{-1,0},{0,1},{1,0},{0,-1}};
public static void cleanRoom(Robot robot){
clean(robot,0,0,0,new HashSet<String>());
}
private static void clean(Robot robot, int x, int y, int d, HashSet<String> visited) {
robot.clean();
visited.add(x + "_" + y);
for (int i = 0;i < 4;i++){
int nd = (d + i) % 4;
int nx = x + ds[i][0];
int ny = y + ds[i][1];
if (robot.move() && !visited.contains(nx + "_" + ny)){
clean(robot,nx,ny,nd,visited);
}
robot.turnRight();
}
robot.turnRight();
robot.turnRight();
robot.move();
robot.turnRight();
robot.turnRight();
}
}
第四题
leetcode875. 爱吃香蕉的珂珂
思路:二分
public class 第四题15 {
public static void main(String[] args) {
int i = 805306368 * 3;
System.out.println(i);
}
public int minEatingSpeed(int[] piles, int h) {
int maxValue = 1;
for (int pile : piles){
maxValue = Math.max(pile,maxValue);
}
int l = 1;
int r = maxValue;
int lastTime = maxValue;
while (l <= r){
int mid = (l + r) / 2;
if (calculateSum(piles,mid) > h){
l = mid + 1;
}else {
lastTime = mid;
r = mid - 1;
}
}
return lastTime;
}
private int calculateSum(int[] piles, int mid) {
int res = 0;
for (int i = 0;i < piles.length;i++){
res += (piles[i] + mid - 1) / mid;
}
return res;
}
}