力扣刷题篇之每日一题(2023年12月ing)

系列文章目录

力扣刷题篇之每日一题(2023年11月完成)-CSDN博客


前言

十二月啦,开始!!!!


12.02ing

每日一题

1

2661. 找出叠涂元素 - 力扣(LeetCode)

讲道理,这个题真心绕,看不懂,得亏评论区老哥:按照arr数组从左到右的顺序遍历各个arr[i],涂抹这个值在矩阵中对应位置的网格,一旦你发现它所在的行或者列满员了,就返回这个i

class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        // 初始化变量
        int i = 0, j, m = mat.length, n = mat[0].length, mn = m * n;
        int[] positionRow = new int[mn + 1], positionColumn = new int[mn + 1], row = new int[m];

        // 遍历矩阵的每一列
        while (i < n) {
            j = 0;
            // 遍历矩阵的每一行
            while (j < m) {
                // 记录每个数字在矩阵中的行和列的位置
                positionRow[mat[j][i]] = j;
                positionColumn[mat[j++][i]] = i;
            }
            // 将该列涂色数置为0,继续下一列
            mat[0][i++] = 0;
        }

        // 遍历数组 arr
        for (i = 0; i < mn; i++) {
            // 如果该数字所在行的涂色数达到 n,或者该数字所在列的涂色数达到 m,则满足条件,返回结果
            if (++row[positionRow[arr[i]]] == n) break;
            if (++mat[0][positionColumn[arr[i]]] == m) break;
        }

        // 返回结果
        return i;
    }
}

2

1094. 拼车 - 力扣(LeetCode)

这个不对,想着动态更新车上的人数,但其实不需要,更新上车下车的人就好了,看第二个代码就简单很多。 

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int longest = 0;
        // 找出最远要到的地方
        for (int i = 0; i < trips.length; i++) {
            longest = Math.max(longest, trips[i][2]);
        }

        // 用 save 数组去记录每一个地方有多少人
        int[] save = new int[longest + 1];

        // 遍历每个行程
        for (int i = 0; i < trips.length; i++) {
            for (int j = trips[i][1]; j <= trips[i][2]; j++) {
                // 上车更新人数
                save[j] += trips[i][0];

                // 如果车上的人多于位置的数量 capacity,则返回 false
                if (save[j] > capacity) {
                    return false;
                }

                // 下车更新人数
                if (j == trips[i][2]) {
                    save[j] -= trips[i][0];
                }
            }
        }

        return true;
    }
}

更新车上有的座位数,通过判断capacity是否还大于等于零,说明还能坐得下。否则就是false。 

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int[]n = new int[1001];
        int max = 0;
        for (int[] trip : trips) {
            n[trip[1]] += trip[0];
            n[trip[2]] -= trip[0];
            if(trip[2] > max) max = trip[2];
        }
        for (int i = 0; i <= max ; i++) {
            capacity -= n[i];
            if(capacity < 0) return false;
        }
        return true;
    }
}

3

1423. 可获得的最大点数 - 力扣(LeetCode)

首先,我们计算前 k 张卡片的和,并将其保存在 sum 中。然后,我们从右侧挑选出 k 张卡片,同时从左侧剔除对应的卡片,不断更新 temp 的值,并将其与 sum 比较,保留较大的值。

这样做的原理是:在一共有 n 张卡片中,我们需要挑选 k 张卡片,即从 n-k 张卡片中去掉 k 张,这样剩余的 k 张卡片的分数就是最大的。

实现上的注意点:

  1. 初始化 temp 的值为前 k 张卡片的和,然后在循环中不断更新右侧加入卡片和左侧剔除卡片的操作。

  2. 利用 Math.max 保留最大的分数。

  3. 循环的范围是 1 到 k,因为第一轮循环中已经计算了前 k 张卡片的和。

这个算法的时间复杂度是 O(k),因为我们只进行了一次循环。空间复杂度是 O(1),因为只使用了常数个额外的变量。

class Solution {
    public int maxScore(int[] cardPoints, int k) {
        int n = cardPoints.length;
        int left = 0;
        int right = n - k;
        int sum = 0;
        for (int i  = 0 ; i < k; i++){
            sum += cardPoints[i];
        }
        int temp = sum;
        for(int i = 1; i <= k; i++){
            temp += cardPoints[n - i] - cardPoints[k - i];
            sum = Math.max(temp, sum);
        }
        return sum;
    }
}

4

1038. 从二叉搜索树到更大和树 - 力扣(LeetCode)

在这个算法中,我们使用中序遍历的反向顺序,即右-中-左,来遍历 BST。对于每个节点,我们累加之前的节点的值,将累加的值赋给当前节点。

以下是算法的主要步骤:

  1. 初始化一个全局变量 sum 为 0,用于保存累加的值。

  2. 递归地遍历 BST,但是顺序是右-中-左。这可以确保我们在访问当前节点时,已经访问了所有较大值的节点。

  3. 对于每个节点,先递归遍历右子树,然后更新 sum,累加当前节点的值,最后递归遍历左子树。

  4. 返回根节点。

这个算法的时间复杂度是 O(N),其中 N 是 BST 中的节点数,因为我们需要访问所有节点。空间复杂度是 O(H),其中 H 是树的高度,因为递归调用会使用栈空间。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int sum = 0;
    public TreeNode bstToGst(TreeNode root) {
        dfs(root);
        return root;
    }
    private void dfs(TreeNode root){
        if(root == null){
            return;
        }
        dfs(root.right);
        root.val += sum;
        sum = root.val;
        dfs(root.left);
    }
}

5

2477. 到达首都的最少油耗 - 力扣(LeetCode)

在这个问题中,有一个包含 n 个城市的网络,城市之间由道路连接。每个城市中有一个旅行者,他们需要一起旅行。每个城市中有一辆车,最大载客量为 seats。目标是在每个城市选择一个旅行者来驾驶车辆,以最小化总的燃料成本。

使用深度优先搜索(DFS)来遍历城市网络,并计算每个城市的最小燃料成本。在每个城市,对于每条与之相连的道路,递归地计算与之相连的城市的总旅行者数。然后,根据最大载客量 seats,计算需要的车辆数量,将其加到全局变量 ans 中。最后,返回该城市的总旅行者数。

以下是算法的主要步骤:

  1. 创建一个邻接表来表示城市网络,使用数组 heads 表示每个城市的邻接链表。

  2. minimumFuelCost 方法中,遍历道路数组,构建邻接表。

  3. 使用深度优先搜索(DFS)遍历城市网络。在DFS过程中,对于每个城市,递归计算与之相连的城市的总旅行者数,并根据最大载客量 seats 计算需要的车辆数量,将其加到全局变量 ans 中。

  4. 返回总的燃料成本 ans

这个算法的时间复杂度取决于城市网络的大小,因为需要遍历所有的城市和道路。DFS 的时间复杂度为 O(N),其中 N 是城市的数量。空间复杂度也与城市网络的大小相关。

/**
 * Definition for a directed road with two cities.
 * Each road connects two cities and can be traversed in both directions.
 */
class Solution {
    int[] edges = null; // 存储边的数组,用于表示每个城市的邻接链表
    int[] next = null;  // 存储下一个相邻城市的索引的数组
    int[] heads = null; // 存储每个城市的邻接链表的头节点索引的数组

    int total = 1;      // 总边数,初始值为1
    int capacity = 0;   // 车辆的最大载客量
    long ans = 0L;      // 最小燃料成本,初始值为0

    /**
     * Helper method to add an undirected road between two cities to the adjacency list.
     */
    private void add(int u, int v) {
        edges[++total] = v;  // 将城市 v 添加到城市 u 的邻接链表中
        next[total] = heads[u];  // 更新城市 u 的邻接链表头节点
        heads[u] = total;  // 更新城市 u 的邻接链表头节点为新添加的边的索引
    }

    /**
     * Main method to calculate the minimum fuel cost.
     */
    public long minimumFuelCost(int[][] roads, int seats) {
        int n = roads.length + 1; // 总城市数,即道路数组的长度加1
        edges = new int[n << 1];  // 初始化边的数组,长度为城市数的两倍
        next = new int[n << 1];   // 初始化下一个相邻城市的索引数组
        heads = new int[n];       // 初始化每个城市的邻接链表头节点数组

        for (int[] road : roads) {
            add(road[0], road[1]);  // 添加道路的两个方向的边到邻接链表
            add(road[1], road[0]);
        }

        this.capacity = seats;  // 初始化车辆最大载客量
        dfs(-1, 0);  // 开始深度优先搜索
        return ans;  // 返回最小燃料成本
    }

    /**
     * Depth-first search (DFS) method to traverse the city network and calculate the minimum fuel cost.
     */
    private int dfs(int root, int node) {
        int sum = 1;  // 初始化当前城市的总旅行者数为1
        for (int choice = heads[node]; choice > 0; choice = next[choice]) {
            if ((choice ^ 1) == root) {
                continue;
            }
            sum += dfs(choice, edges[choice]);  // 递归计算与之相连的城市的总旅行者数
        }
        if (node != 0) {
            ans += (sum - 1) / capacity + 1;  // 根据最大载客量计算需要的车辆数量,加到总燃料成本中
        }
        return sum;  // 返回当前城市的总旅行者数
    }
}


总结

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值