diff --git a/solution/3600-3699/3638.Maximum Balanced Shipments/README.md b/solution/3600-3699/3638.Maximum Balanced Shipments/README.md index 284ce834bfdd9..3c06b5821b1c5 100644 --- a/solution/3600-3699/3638.Maximum Balanced Shipments/README.md +++ b/solution/3600-3699/3638.Maximum Balanced Shipments/README.md @@ -87,32 +87,99 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3638.Ma -### 方法一 +### 方法一:贪心 + +我们维护当前遍历的数组的最大值 $\text{mx}$,并遍历数组中的每个元素 $x$。如果 $x < \text{mx}$,则说明当前元素可以作为一个平衡装运的最后一个包裹,因此我们就将答案加一,并将 $\text{mx}$ 重置为 0。否则,我们更新 $\text{mx}$ 为当前元素 $x$ 的值。 + +遍历结束后,返回答案即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$,只使用了常数级别的额外空间。 #### Python3 ```python - +class Solution: + def maxBalancedShipments(self, weight: List[int]) -> int: + ans = mx = 0 + for x in weight: + mx = max(mx, x) + if x < mx: + ans += 1 + mx = 0 + return ans ``` #### Java ```java - +class Solution { + public int maxBalancedShipments(int[] weight) { + int ans = 0; + int mx = 0; + for (int x : weight) { + mx = Math.max(mx, x); + if (x < mx) { + ++ans; + mx = 0; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxBalancedShipments(vector& weight) { + int ans = 0; + int mx = 0; + for (int x : weight) { + mx = max(mx, x); + if (x < mx) { + ++ans; + mx = 0; + } + } + return ans; + } +}; ``` #### Go ```go +func maxBalancedShipments(weight []int) (ans int) { + mx := 0 + for _, x := range weight { + mx = max(mx, x) + if x < mx { + ans++ + mx = 0 + } + } + return +} +``` +#### TypeScript + +```ts +function maxBalancedShipments(weight: number[]): number { + let [ans, mx] = [0, 0]; + for (const x of weight) { + mx = Math.max(mx, x); + if (x < mx) { + ans++; + mx = 0; + } + } + return ans; +} ``` diff --git a/solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md b/solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md index 7e4896c85bde9..a549ac9a93046 100644 --- a/solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md +++ b/solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md @@ -85,32 +85,99 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3638.Ma -### Solution 1 +### Solution 1: Greedy + +We maintain the maximum value $\text{mx}$ of the currently traversed array, and iterate through each element $x$ in the array. If $x < \text{mx}$, it means the current element can serve as the last parcel of a balanced shipment, so we increment the answer by one and reset $\text{mx}$ to 0. Otherwise, we update $\text{mx}$ to the value of the current element $x$. + +After the traversal, we return the answer. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$, using only constant extra space. #### Python3 ```python - +class Solution: + def maxBalancedShipments(self, weight: List[int]) -> int: + ans = mx = 0 + for x in weight: + mx = max(mx, x) + if x < mx: + ans += 1 + mx = 0 + return ans ``` #### Java ```java - +class Solution { + public int maxBalancedShipments(int[] weight) { + int ans = 0; + int mx = 0; + for (int x : weight) { + mx = Math.max(mx, x); + if (x < mx) { + ++ans; + mx = 0; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxBalancedShipments(vector& weight) { + int ans = 0; + int mx = 0; + for (int x : weight) { + mx = max(mx, x); + if (x < mx) { + ++ans; + mx = 0; + } + } + return ans; + } +}; ``` #### Go ```go +func maxBalancedShipments(weight []int) (ans int) { + mx := 0 + for _, x := range weight { + mx = max(mx, x) + if x < mx { + ans++ + mx = 0 + } + } + return +} +``` +#### TypeScript + +```ts +function maxBalancedShipments(weight: number[]): number { + let [ans, mx] = [0, 0]; + for (const x of weight) { + mx = Math.max(mx, x); + if (x < mx) { + ans++; + mx = 0; + } + } + return ans; +} ``` diff --git a/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.cpp b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.cpp new file mode 100644 index 0000000000000..67ee29fd360e8 --- /dev/null +++ b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int maxBalancedShipments(vector& weight) { + int ans = 0; + int mx = 0; + for (int x : weight) { + mx = max(mx, x); + if (x < mx) { + ++ans; + mx = 0; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.go b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.go new file mode 100644 index 0000000000000..989bfcef6fecf --- /dev/null +++ b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.go @@ -0,0 +1,11 @@ +func maxBalancedShipments(weight []int) (ans int) { + mx := 0 + for _, x := range weight { + mx = max(mx, x) + if x < mx { + ans++ + mx = 0 + } + } + return +} \ No newline at end of file diff --git a/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.java b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.java new file mode 100644 index 0000000000000..72a5729978893 --- /dev/null +++ b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public int maxBalancedShipments(int[] weight) { + int ans = 0; + int mx = 0; + for (int x : weight) { + mx = Math.max(mx, x); + if (x < mx) { + ++ans; + mx = 0; + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.py b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.py new file mode 100644 index 0000000000000..299ce7badf3e8 --- /dev/null +++ b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def maxBalancedShipments(self, weight: List[int]) -> int: + ans = mx = 0 + for x in weight: + mx = max(mx, x) + if x < mx: + ans += 1 + mx = 0 + return ans diff --git a/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.ts b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.ts new file mode 100644 index 0000000000000..99168eff4a8a0 --- /dev/null +++ b/solution/3600-3699/3638.Maximum Balanced Shipments/Solution.ts @@ -0,0 +1,11 @@ +function maxBalancedShipments(weight: number[]): number { + let [ans, mx] = [0, 0]; + for (const x of weight) { + mx = Math.max(mx, x); + if (x < mx) { + ans++; + mx = 0; + } + } + return ans; +}