Skip to content

Latest commit

 

History

History
218 lines (178 loc) · 5 KB

File metadata and controls

218 lines (178 loc) · 5 KB

English Version

题目描述

传送带上的包裹必须在 days 天内从一个港口运送到另一个港口。

传送带上的第 i 个包裹的重量为 weights[i]。每一天,我们都会按给出重量(weights)的顺序往传送带上装载包裹。我们装载的重量不会超过船的最大运载重量。

返回能在 days 天内将传送带上的所有包裹送达的船的最低运载能力。

 

示例 1:

输入:weights = [1,2,3,4,5,6,7,8,9,10], days = 5
输出:15
解释:
船舶最低载重 15 就能够在 5 天内送达所有包裹,如下所示:
第 1 天:1, 2, 3, 4, 5
第 2 天:6, 7
第 3 天:8
第 4 天:9
第 5 天:10

请注意,货物必须按照给定的顺序装运,因此使用载重能力为 14 的船舶并将包装分成 (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) 是不允许的。 

示例 2:

输入:weights = [3,2,2,4,1,4], days = 3
输出:6
解释:
船舶最低载重 6 就能够在 3 天内送达所有包裹,如下所示:
第 1 天:3, 2
第 2 天:2, 4
第 3 天:1, 4

示例 3:

输入:weights = [1,2,3,1,1], days = 4
输出:3
解释:
第 1 天:1
第 2 天:2
第 3 天:3
第 4 天:1, 1

 

提示:

  • 1 <= days <= weights.length <= 5 * 104
  • 1 <= weights[i] <= 500

解法

二分查找。

二分枚举运载能力 capacity,找到能在 D 天内送达的最小运载。

Python3

class Solution:
    def shipWithinDays(self, weights: List[int], D: int) -> int:
        def check(capacity):
            cnt, t = 1, 0
            for w in weights:
                if w > capacity:
                    return False
                if t + w <= capacity:
                    t += w
                else:
                    cnt += 1
                    t = w
            return cnt <= D

        left, right = 1, 25000000
        while left < right:
            mid = (left + right) >> 1
            if check(mid):
                right = mid
            else:
                left = mid + 1
        return left

Java

class Solution {
    public int shipWithinDays(int[] weights, int days) {
        int left = 1, right = Integer.MAX_VALUE;
        while (left < right) {
            int mid = (left + right) >> 1;
            if (canCarry(weights, days, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }

    public boolean canCarry(int[] weights, int days, int carry) {
        int useDay = 1;
        int curCarry = 0;
        for (int weight : weights) {
            if (weight > carry) {
                return false;
            }
            if ((carry - curCarry) >= weight) {
                curCarry += weight;
            } else {
                curCarry = weight;
                useDay++;
            }
        }
        return useDay <= days;
    }
}

C++

class Solution {
public:
    int shipWithinDays(vector<int>& weights, int days) {
        int left = 1, right = 25000000;
        while (left < right) {
            int mid = left + right >> 1;
            if (check(weights, days, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }

    bool check(vector<int>& weights, int days, int capacity) {
        int cnt = 1, t = 0;
        for (auto w : weights) {
            if (w > capacity) {
                return false;
            }
            if (t + w <= capacity) {
                t += w;
            } else {
                ++cnt;
                t = w;
            }
        }
        return cnt <= days;
    }
};

Go

func shipWithinDays(weights []int, days int) int {
	left, right := 1, 25000000
	for left < right {
		mid := (left + right) >> 1
		if check(weights, days, mid) {
			right = mid
		} else {
			left = mid + 1
		}
	}
	return left
}

func check(weights []int, days, capacity int) bool {
	cnt, t := 1, 0
	for _, w := range weights {
		if w > capacity {
			return false
		}
		if t+w <= capacity {
			t += w
		} else {
			cnt++
			t = w
		}
	}
	return cnt <= days
}

...