3638. Maximum Balanced Shipments Medium
1/**
2 * [3638] Maximum Balanced Shipments
3 *
4 * <p data-end="365" data-start="23">You are given an integer array <code data-end="62" data-start="54">weight of length <code data-end="76" data-start="73">n, representing the weights of <code data-end="109" data-start="106">n parcels arranged in a straight line. A <strong data-end="161" data-start="149">shipment is defined as a contiguous subarray of parcels. A shipment is considered <strong data-end="247" data-start="235">balanced if the weight of the <strong data-end="284" data-start="269">last parcel is strictly less than the <strong data-end="329" data-start="311">maximum weight among all parcels in that shipment.
5 * <p data-end="528" data-start="371">Select a set of <strong data-end="406" data-start="387">non-overlapping, contiguous, balanced shipments such that <strong data-end="496" data-start="449">each parcel appears in at most one shipment (parcels may remain unshipped).
6 * <p data-end="587" data-start="507">Return the <strong data-end="545" data-start="518">maximum possible number of balanced shipments that can be formed.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">weight = [2,5,1,4,3]</span>
11 * Output: <span class="example-io">2</span>
12 * Explanation:
13 * <p data-end="136" data-start="62">We can form the maximum of two balanced shipments as follows:
14 *
15 * <li data-end="163" data-start="140">Shipment 1: [2, 5, 1]
16 *
17 * <li data-end="195" data-start="168">Maximum parcel weight = 5
18 * <li data-end="275" data-start="200">Last parcel weight = 1, which is strictly less than 5. Thus, it's balanced.
19 *
20 *
21 * <li data-end="299" data-start="279">Shipment 2: [4, 3]
22 *
23 * <li data-end="331" data-start="304">Maximum parcel weight = 4
24 * <li data-end="411" data-start="336">Last parcel weight = 3, which is strictly less than 4. Thus, it's balanced.
25 *
26 *
27 *
28 * <p data-end="519" data-start="413">It is impossible to partition the parcels to achieve more than two balanced shipments, so the answer is 2.
29 * </div>
30 * <strong class="example">Example 2:
31 * <div class="example-block">
32 * Input: <span class="example-io">weight = [4,4]</span>
33 * Output: <span class="example-io">0</span>
34 * Explanation:
35 * <p data-end="635" data-start="574">No balanced shipment can be formed in this case:
36 *
37 * <li data-end="772" data-start="639">A shipment [4, 4] has maximum weight 4 and the last parcel's weight is also 4, which is not strictly less. Thus, it's not balanced.
38 * <li data-end="885" data-start="775">Single-parcel shipments [4] have the last parcel weight equal to the maximum parcel weight, thus not balanced.
39 *
40 * <p data-end="958" data-is-last-node="" data-is-only-node="" data-start="887">As there is no way to form even one balanced shipment, the answer is 0.
41 * </div>
42 *
43 * Constraints:
44 *
45 * <li data-end="8706" data-start="8671"><code data-end="8704" data-start="8671">2 <= n <= 10^5
46 * <li data-end="8733" data-start="8709"><code data-end="8733" data-start="8709">1 <= weight[i] <= 10^9
47 *
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/maximum-balanced-shipments/
52// discuss: https://leetcode.com/problems/maximum-balanced-shipments/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57 pub fn max_balanced_shipments(weight: Vec<i32>) -> i32 {
58 0
59 }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn test_3638() {
70 }
71}
72Back
© 2026 bowen.ge All Rights Reserved.