LeetCode每日一题(1665. Minimum Initial Energy to Finish Tasks)

You are given an array tasks where tasks[i] = [actuali, minimumi]:

actuali is the actual amount of energy you spend to finish the ith task.
minimumi is the minimum amount of energy you require to begin the ith task.
For example, if the task is [10, 12] and your current energy is 11, you cannot start this task. However, if your current energy is 13, you can complete this task, and your energy will be 3 after finishing it.

You can finish the tasks in any order you like.

Return the minimum initial amount of energy you will need to finish all the tasks.

Example 1:

Input: tasks = [[1,2],[2,4],[4,8]]
Output: 8

Explanation:
Starting with 8 energy, we finish the tasks in the following order:

  • 3rd task. Now energy = 8 - 4 = 4.
  • 2nd task. Now energy = 4 - 2 = 2.
  • 1st task. Now energy = 2 - 1 = 1.
    Notice that even though we have leftover energy, starting with 7 energy does not work because we cannot do the 3rd task.

Example 2:

Input: tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]
Output: 32

Explanation:
Starting with 32 energy, we finish the tasks in the following order:

  • 1st task. Now energy = 32 - 1 = 31.
  • 2nd task. Now energy = 31 - 2 = 29.
  • 3rd task. Now energy = 29 - 10 = 19.
  • 4th task. Now energy = 19 - 10 = 9.
  • 5th task. Now energy = 9 - 8 = 1.

Example 3:

Input: tasks = [[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]]
Output: 27

Explanation:
Starting with 27 energy, we finish the tasks in the following order:

  • 5th task. Now energy = 27 - 5 = 22.
  • 2nd task. Now energy = 22 - 2 = 20.
  • 3rd task. Now energy = 20 - 3 = 17.
  • 1st task. Now energy = 17 - 1 = 16.
  • 4th task. Now energy = 16 - 4 = 12.
  • 6th task. Now energy = 12 - 6 = 6.

Constraints:

  • 1 <= tasks.length <= 105
  • 1 <= actual​i <= minimumi <= 104

单纯是自己想的, 不保证没有绕弯路。

  1. 最后一步完成后所剩余的能量是完全浪费的, 所浪费的能量就是 tasks[i][1] - tasks[i][0], 所以为了初始值最小, 我们需要挑选这个差值最小的 task 作为最后一步, 如果存在最小差值相同的情况, 我们要挑选 tasks[i][1]最大的那个, 因为如果在完成倒数第二步之后所剩余的能量大于最后一步所需能量, 我们前面给初始能量增加的值有可能也是浪费的

  2. 然后再将 tasks 按(tasks[i][1] - tasks[i][0], tasks[i][1])这个键值对进行反向排序, 一个一个的解决 task, 初始能量为 task[i][1]的最大值, 如果当前能量不够, 那就补充到 task[i][1], 这部分差值就是初始能量需要添加的值



impl Solution {
    pub fn minimum_effort(mut tasks: Vec<Vec<i32>>) -> i32 {
        let mut init = tasks.iter().map(|v| v[1]).max().unwrap();
        tasks.sort_by_key(|v| (v[1] - v[0], -v[1]));
        let last_step = tasks.remove(0);
        tasks.sort_by_key(|v| (v[1] - v[0], v[1]));
        let mut curr = init;
        for i in (0..tasks.len()).rev() {
            let cost = tasks[i][0];
            let required = tasks[i][1];
            if curr < required {
                init += required - curr;
                curr = required;
            }
            curr -= cost;
        }
        if curr < last_step[1] {
            init += last_step[1] - curr;
        }
        init
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值