力扣秋季赛-战队赛

本文主要介绍了力扣秋季赛(战队赛)中的两道算法题解,包括二叉树节点颜色统计和自行车炫技赛场问题。对于二叉树题目,采用简单的DFS遍历解决;自行车炫技赛场问题,通过DFS和BFS两种方法求解,重点在于避免重复坐标并找到所有速度为1的坐标。作者鼓励读者多加练习以提升算法能力。
摘要由CSDN通过智能技术生成

力扣秋季赛(战队赛)

image-20210928162644806

此次力扣秋季赛(战队赛)进行期间,各项事务繁忙,但仍然抽出时间进行比赛,坚毅之心,仍需坚持。

(此博客只做两题题解)

力扣秋季赛(战队赛)

image-20210928163019908

开幕式焰火

image-20210928163107654

此题思路非常简单,对二叉树的每个分支进行遍历,利用链表的contains方法进行查重即可。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int numColor(TreeNode root) {
        List<Integer> answer =new ArrayList<>();
        dfs(root,answer);
        return answer.size();
        
    }
    
    public void dfs(TreeNode root,List<Integer> answer){
        if(root == null) return;
        else{
            if(!answer.contains(root.val)){
                answer.add(root.val);
            }
        }
        dfs(root.left,answer);
        dfs(root.right,answer);
    }
}

自行车炫技赛场

image-20210928163433985

初见此题,大致有两个思路。

  • dfs深搜取得答案。
  • bfs广搜取得答案。

笔者最初使用深搜进行探索,很可惜代码过于冗长,自己都不能控制。

class Solution {
    int speed = 1;
    public int[][] bicycleYard(int[] position, int[][] terrain, int[][] obstacle) {
        int x = position[0];
        int y = position[1];
        int n = terrain.length;
        int m = terrain[0].length;
        int[][] book = new int[n][m];
        List<Queu> queue = new ArrayList<>();
        book[x][y] = 1;
        dfs(x,y,book,terrain,obstacle,queue,m,n);
        int length = queue.size();
        Collections.sort(queue,new cmp());
        int[][] answer = new int[length][2];
        for(int i = 0;i < length;i++){
            Queu node = queue.get(i);
            answer[i][0] = node.x;
            answer[i][1] = node.y;
        }
        return answer;
        
    }
    
    public boolean up(int x,int y,int[][] book,int[][] terrain,int[][] obstacle,List<Queu> queue){
        if(x-1 >=0 && book[x-1][y] == 0){
            speed = speed + terrain[x][y] - terrain[x-1][y] - obstacle[x-1][y];
            if(speed <= 0){
                return false;
            }
            else if(speed == 1){
                Queu node = new Queu(x-1,y);
                queue.add(node);
                book[x-1][y] = 1;
                return true;
            }
        }
        return false;
    }
    
     public boolean down(int x,int y,int[][] book,int[][] terrain,int[][] obstacle,List<Queu> queue,int n){
        if(x+1 < n && book[x+1][y] == 0){
            speed = speed + terrain[x][y] - terrain[x+1][y] - obstacle[x+1][y];
            if(speed <= 0){
                return false;
            }
            else if(speed == 1){
                Queu node = new Queu(x+1,y);
                queue.add(node);
                book[x+1][y] = 1;
                return true;
            }
        }
        return false;
    }
    
     public boolean left(int x,int y,int[][] book,int[][] terrain,int[][] obstacle,List<Queu> queue){
        if(y-1 >=0 && book[x][y-1] == 0){
            speed = speed + terrain[x][y] - terrain[x][y-1] - obstacle[x][y-1];
            if(speed <= 0){
                return false;
            }
            else if(speed == 1){
                Queu node = new Queu(x,y-1);
                queue.add(node);
                book[x][y-1] = 1;
                return true;
            }
        }
        return false;
    }
    
     public boolean right(int x,int y,int[][] book,int[][] terrain,int[][] obstacle,List<Queu> queue,int m){
        if(y+1 <m && book[x][y+1] == 0 ){
            speed = speed + terrain[x][y] - terrain[x][y+1] - obstacle[x][y+1];
            if(speed <= 0){
                return false;
            }
            else if(speed == 1){
                Queu node = new Queu(x,y+1);
                queue.add(node);
                book[x][y+1] = 1;
                return true;
            }
        }
        return false;
    }
    
    public void dfs(int x,int y,int[][] book,int[][] terrain,int[][] obstacle,List<Queu> queue,int m,int n){
        if(up(x,y,book,terrain,obstacle,queue)){
            dfs(x-1,y,book,terrain,obstacle,queue,m,n);
        }
         if(down(x,y,book,terrain,obstacle,queue,n)){
            dfs(x+1,y,book,terrain,obstacle,queue,m,n);
        }
         if(left(x,y,book,terrain,obstacle,queue)){
            dfs(x,y-1,book,terrain,obstacle,queue,m,n);
        }
         if(right(x,y,book,terrain,obstacle,queue,m)){
            dfs(x,y+1,book,terrain,obstacle,queue,m,n);
        }
    }
}

class Queu{
    int x;
    int y;
    Queu(int x,int y){
        this.x = x;
        this.y = y;
    }
}

class cmp implements Comparator<Queu>{
    public int compare(Queu o1,Queu o2){
        if(o1.x - o2.x != 0) return o1.x - o2.x;
        return o1.y - o2.y;
    }
}

那么此处我们稍微梳理一下思路。

首先,我们要求的是自行车所有速度为一时,所在的坐标。那么对于自行车而言,它有很多中走法去到不同的点,速度为一,但我们不需要关注走法,我们只需要确认没有相同的坐标进入答案即可。

bfs基本步骤(dfs类似)

  • 以当前坐标向四周延伸,判断是否可行
  • 如果可行,加入队列,并判断速度是否为一,是否与已有答案重复,决定是否加入答案数组
  • 对该队列进行逐个尝试,直至队列为空,得到答案
class Solution {
    
    int[] dx = {-1, 1, 0, 0};
    int[] dy = {0, 0, -1, 1};
    List<int[]> ans;
    Set<String> mem;
    boolean[][] global;
    int[] p;

    public int[][] bicycleYard(int[] position, int[][] terrain, int[][] obstacle) {
        ans = new ArrayList<>();
        mem = new HashSet<>();
        global = new boolean[terrain.length][terrain[0].length];
        p = position;
        bfs(1, position[0], position[1], terrain, obstacle);
        int[][] res = new int[ans.size()][2];
        for (int i = 0; i < ans.size(); i++) {
            res[i] = ans.get(i);
        }
        Arrays.sort(res, (o1, o2) -> {
            if (o1[0] == o2[0]) {
                return Integer.compare(o1[1], o2[1]);
            } else {
                return Integer.compare(o1[0], o2[0]);
            }
        });
        return res;
    }

    public void bfs(int curr, int x, int y, int[][] terrain, int[][] obstacle) {
        if (x >= 0 && y >= 0 && x < terrain.length && y < terrain[0].length) {
            for (int i = 0; i < 4; i++) {
                int newX = x + dx[i];
                int newY = y + dy[i];
                if (newX >= 0 && newY >= 0 && newX < terrain.length && newY < terrain[0].length) {
                    int next = curr + terrain[x][y] - terrain[newX][newY] - obstacle[newX][newY];
                    // i, j, v 作为唯一 key
                    String k = newX + "-" + newY + "-" + next;
                    if (mem.contains(k)) {
                        continue;
                    }
                    mem.add(k);
                    if (next >= 1) {
                        if (next == 1 && (newX != p[0] || newY != p[1]) && !global[newX][newY]) {
                            global[newX][newY] = true;
                            ans.add(new int[]{newX, newY});
                        }
                        bfs(next, newX, newY, terrain, obstacle);
                    }
                }
            }
        }
    }
}
只要勤于练习,总有一天你我都能成为算法大牛!

2021.09.28
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值