2020 年「力扣」春季赛前三题

Java 代码:

public class Solution {

    public int minCount(int[] coins) {

        int res = 0;

        for (int coin : coins) {
            if ((coin % 2) == 0) {
                res += coin / 2;
            } else {
                res += (coin + 1) / 2;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        // int[] coins = {4, 2, 1};
        int[] coins = {2, 3, 10};
        int res = solution.minCount(coins);
        System.out.println(res);
    }
}

Java 代码:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {

    public int numWays(int n, int[][] relation, int k) {

        // 构建邻接表
        boolean[][] adj = new boolean[n][n];
        for (int[] r : relation) {
            adj[r[0]][r[1]] = true;
        }

        Queue<Integer> queue = new LinkedList<>();
        queue.add(0);

        while (!queue.isEmpty() && k > 0) {
            int size = queue.size();
            k--;
            for (int i = 0; i < size; i++) {
                Integer head = queue.poll();

                List<Integer> next = next(head, adj, n);
                for (Integer successor : next) {
                    queue.add(successor);
                }
            }
        }

        int target = n - 1;
        int res = 0;
        while (!queue.isEmpty()) {
            Integer head = queue.poll();
            if (head == target) {
                res++;
            }
        }
        return res;
    }

    private List<Integer> next(int i, boolean[][] adj, int n) {
        List<Integer> next = new ArrayList<>();
        for (int j = 0; j < n; j++) {
            if (adj[i][j] && j != i) {
                next.add(j);
            }
        }
        return next;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
//        int n = 5;
//        int[][] relation = {{0, 2}, {2, 1}, {3, 4}, {2, 3}, {1, 4}, {2, 0}, {0, 4}};
//        int k = 3;

        int n = 3;
        int[][] relation = {{0, 2}, {2, 1}};
        int k = 2;

        int res = solution.numWays(n, relation, k);
        System.out.println(res);
    }
}

Java 代码:

import java.util.Arrays;

public class Solution {

    // 二分法,0 <= increase[i] <= 10

    public int[] getTriggerTime(int[][] increase, int[][] requirements) {

        int len = increase.length;
        // 最开始算 1 天
        int[][] preSum = new int[len + 1][3];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < 3; j++) {
                preSum[i + 1][j] = increase[i][j] + preSum[i][j];
            }
        }

        // 对于每一个 requirements,二分查找 满足条件的下标
        int rLen = requirements.length;
        int[] res = new int[rLen];
        Arrays.fill(res, -1);

        for (int i = 0; i < rLen; i++) {
            int[] requirement = requirements[i];

            // 特判,最后一天都不满足,才二分去定位
            if (requirement[0] > preSum[len][0] ||
                    requirement[1] > preSum[len][1] ||
                    requirement[2] > preSum[len][2]) {
                continue;
            }

            // 找数组里第 1 个三项指标全部大于的下标

            int left = 0;
            int right = len;

            while (left < right) {
                int mid = (left + right) >> 1;
                if (requirement[0] <= preSum[mid][0] &&
                        requirement[1] <= preSum[mid][1] &&
                        requirement[2] <= preSum[mid][2]) {
                    // 当前以及当前右边肯定是,下一轮搜索区间是 [left, mid]
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }
            res[i] = left;
        }
        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();

        int[][] increase = {{2, 8, 4}, {2, 5, 0}, {10, 9, 8}};
        int[][] requirements = {{2, 11, 3}, {15, 10, 7}, {9, 17, 12}, {8, 1, 14}};

        // int[][] increase = {{0, 4, 5}, {4, 8, 8}, {8, 6, 1}, {10, 10, 0}};
        // int[][] requirements = {{12, 11, 16}, {20, 2, 6}, {9, 2, 6}, {10, 18, 3}, {8, 14, 9}};

//        int[][] increase = {{1, 1, 1}};
//        int[][] requirements = {{0, 0, 0}};
        int[] res = solution.getTriggerTime(increase, requirements);
        System.out.println(Arrays.toString(res));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值