2024.1.19每日一题

LeetCode

2809.使数组和小于等于x的最少时间

2809. 使数组和小于等于 x 的最少时间 - 力扣(LeetCode)

题目描述

给你两个长度相等下标从 0 开始的整数数组 nums1nums2 。每一秒,对于所有下标 0 <= i < nums1.lengthnums1[i] 的值都增加 nums2[i] 。操作 完成后 ,你可以进行如下操作:

  • 选择任一满足 0 <= i < nums1.length 的下标 i ,并使 nums1[i] = 0

同时给你一个整数 x

请你返回使 nums1 中所有元素之和 小于等于 x 所需要的 最少 时间,如果无法实现,那么返回 -1

示例 1:

输入:nums1 = [1,2,3], nums2 = [1,2,3], x = 4
输出:3
解释:
第 1 秒,我们对 i = 0 进行操作,得到 nums1 = [0,2+2,3+3] = [0,4,6] 。
第 2 秒,我们对 i = 1 进行操作,得到 nums1 = [0+1,0,6+3] = [1,0,9] 。
第 3 秒,我们对 i = 2 进行操作,得到 nums1 = [1+1,0+2,0] = [2,2,0] 。
现在 nums1 的和为 4 。不存在更少次数的操作,所以我们返回 3 。

示例 2:

输入:nums1 = [1,2,3], nums2 = [3,3,3], x = 4
输出:-1
解释:不管如何操作,nums1 的和总是会超过 x 。

提示:

  • 1 <= nums1.length <= 103
  • 1 <= nums1[i] <= 103
  • 0 <= nums2[i] <= 103
  • nums1.length == nums2.length
  • 0 <= x <= 106

思路

困难题,睡大觉,cv大法

代码

C++
class Solution {
public:
    int minimumTime(vector<int> &nums1, vector<int> &nums2, int x) {
        int n = nums1.size();
        // 对下标数组排序,避免破坏 nums1 和 nums2 的对应关系
        vector<int> ids(n);
        iota(ids.begin(), ids.end(), 0);
        sort(ids.begin(), ids.end(), [&](const int i, const int j) {
            return nums2[i] < nums2[j];
        });

        vector<int> f(n + 1);
        for (int i = 0; i < n; i++) {
            int a = nums1[ids[i]], b = nums2[ids[i]];
            for (int j = i + 1; j; j--) {
                f[j] = max(f[j], f[j - 1] + a + b * j);
            }
        }

        int s1 = accumulate(nums1.begin(), nums1.end(), 0);
        int s2 = accumulate(nums2.begin(), nums2.end(), 0);
        for (int t = 0; t <= n; t++) {
            if (s1 + s2 * t - f[t] <= x) {
                return t;
            }
        }
        return -1;
    }
};
Java
class Solution {
   
    public int minimumTime(List<Integer> nums1, List<Integer> nums2, int x) {
   
        int n = nums1.size(), s1 = 0, s2 = 0;
        int[][] pairs = new int[n][2];
        for (int i = 0; i < n; i++) {
   
            int a = nums1.get(i);
            int b = nums2.get(i);
            pairs[i][0] = a;
            pairs[i][1] = b;
            s1 += a;
            s2 += b;
        }
        Arrays.sort(pairs, (a, b) -> a[1] - b[1]);

        int[] f = new int[n + 1];
        for (int i = 0; i < n; i++) {
   
            int a = pairs[i][0];
            int b = pairs[i][1];
            for (int j = i + 1; j > 0; j--) {
   
                f[j] = Math.max(f[j], f[j - 1] + a + b * j);
            }
        }

        for (int t = 0; t <= n; t++) {
   
            if (s1 + s2 * t - f[t] <= x) {
   
                return t;
            }
        }
        return -1;
    }
}

image-20240119074922252

image-20240119074935288

相关推荐

  1. 每日】01

    2024-01-19 11:54:01       24 阅读
  2. 每日cf

    2024-01-19 11:54:01       26 阅读
  3. 每日——第二十

    2024-01-19 11:54:01       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-19 11:54:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-19 11:54:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-19 11:54:01       87 阅读
  4. Python语言-面向对象

    2024-01-19 11:54:01       96 阅读

热门阅读

  1. Vue中Props将父组件的数据传递给子组件

    2024-01-19 11:54:01       57 阅读
  2. Python封装tvdi算法为exe并读取xml

    2024-01-19 11:54:01       63 阅读
  3. Spring Boot多环境配置

    2024-01-19 11:54:01       59 阅读
  4. 启动YonBIP中间件控制台日志

    2024-01-19 11:54:01       46 阅读
  5. [GN] 使用vue3+vite+ts+prettier+eslint

    2024-01-19 11:54:01       55 阅读
  6. 科普:大语言模型中的量化是什么意思?

    2024-01-19 11:54:01       61 阅读