diff --git a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README_EN.md b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README_EN.md index 1eecdef3bc971..d6251f9aa6dc8 100644 --- a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README_EN.md +++ b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README_EN.md @@ -25,7 +25,7 @@ tags:
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
-Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
+Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Example 1:
diff --git a/solution/0500-0599/0593.Valid Square/README.md b/solution/0500-0599/0593.Valid Square/README.md index 1b818e3a203d9..ff04df42eea6f 100644 --- a/solution/0500-0599/0593.Valid Square/README.md +++ b/solution/0500-0599/0593.Valid Square/README.md @@ -29,7 +29,7 @@ tags:输入: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] -输出: True +输出: true
示例 2:
diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md b/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md index cc09771a73c64..06b75b6a92e88 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md @@ -22,7 +22,7 @@ tags:和谐数组是指一个数组里元素的最大值和最小值之间的差别 正好是 1
。
给你一个整数数组 nums
,请你在所有可能的子序列中找到最长的和谐子序列的长度。
给你一个整数数组 nums
,请你在所有可能的 子序列 中找到最长的和谐子序列的长度。
数组的 子序列 是一个由数组派生出来的序列,它可以通过删除一些元素或不删除元素、且不改变其余元素的顺序而得到。
diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md b/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md index cb21a5cbe29d1..606ae0f82eff8 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md @@ -22,9 +22,7 @@ tags:We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly 1
.
Given an integer array nums
, return the length of its longest harmonious subsequence among all its possible subsequences.
A subsequence of array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.
+Given an integer array nums
, return the length of its longest harmonious subsequence among all its possible subsequences.
Example 1:
diff --git a/solution/0900-0999/0975.Odd Even Jump/README.md b/solution/0900-0999/0975.Odd Even Jump/README.md index 71f1151615bcd..f5e96324a9b87 100644 --- a/solution/0900-0999/0975.Odd Even Jump/README.md +++ b/solution/0900-0999/0975.Odd Even Jump/README.md @@ -20,17 +20,17 @@ tags: -给定一个整数数组 A
,你可以从某一起始索引出发,跳跃一定次数。在你跳跃的过程中,第 1、3、5... 次跳跃称为奇数跳跃,而第 2、4、6... 次跳跃称为偶数跳跃。
给定一个整数数组 arr
,你可以从某一起始索引出发,跳跃一定次数。在你跳跃的过程中,第 1、3、5... 次跳跃称为奇数跳跃,而第 2、4、6... 次跳跃称为偶数跳跃。
你可以按以下方式从索引 i
向后跳转到索引 j
(其中 i < j
):
j
,使得 A[i] <= A[j]
,A[j]
是可能的最小值。如果存在多个这样的索引 j
,你只能跳到满足要求的最小索引 j
上。j
,使得 A[i] >= A[j]
,A[j]
是可能的最大值。如果存在多个这样的索引 j
,你只能跳到满足要求的最小索引 j
上。j
,使得 arr[i] <= arr[j]
,且 arr[j]
的值尽可能小。如果存在多个这样的索引 j
,你只能跳到满足要求的最小索引 j
上。j
,使得 arr[i] >= arr[j]
,且 arr[j]
的值尽可能大。如果存在多个这样的索引 j
,你只能跳到满足要求的最小索引 j
上。i
,可能无法进行合乎要求的跳跃。)如果从某一索引开始跳跃一定次数(可能是 0 次或多次),就可以到达数组的末尾(索引 A.length - 1
),那么该索引就会被认为是好的起始索引。
如果从某一索引开始跳跃一定次数(可能是 0 次或多次),就可以到达数组的末尾(索引 arr.length - 1
),那么该索引就会被认为是好的起始索引。
返回好的起始索引的数量。
@@ -38,10 +38,11 @@ tags:示例 1:
-输入:[10,13,12,14,15] ++输入:[10,13,12,14,15] 输出:2 解释: -从起始索引 i = 0 出发,我们可以跳到 i = 2,(因为 A[2] 是 A[1],A[2],A[3],A[4] 中大于或等于 A[0] 的最小值),然后我们就无法继续跳下去了。 +从起始索引 i = 0 出发,我们可以跳到 i = 2,(因为 arr[2] 是 arr[1],arr[2],arr[3],arr[4] 中大于或等于 arr[0] 的最小值),然后我们就无法继续跳下去了。 从起始索引 i = 1 和 i = 2 出发,我们可以跳到 i = 3,然后我们就无法继续跳下去了。 从起始索引 i = 3 出发,我们可以跳到 i = 4,到达数组末尾。 从起始索引 i = 4 出发,我们已经到达数组末尾。 @@ -50,16 +51,17 @@ tags:示例 2:
-输入:[2,3,1,1,4] ++输入:[2,3,1,1,4] 输出:3 解释: 从起始索引 i=0 出发,我们依次可以跳到 i = 1,i = 2,i = 3: -在我们的第一次跳跃(奇数)中,我们先跳到 i = 1,因为 A[1] 是(A[1],A[2],A[3],A[4])中大于或等于 A[0] 的最小值。 +在我们的第一次跳跃(奇数)中,我们先跳到 i = 1,因为 arr[1] 是(arr[1],arr[2],arr[3],arr[4])中大于或等于 arr[0] 的最小值。 -在我们的第二次跳跃(偶数)中,我们从 i = 1 跳到 i = 2,因为 A[2] 是(A[2],A[3],A[4])中小于或等于 A[1] 的最大值。A[3] 也是最大的值,但 2 是一个较小的索引,所以我们只能跳到 i = 2,而不能跳到 i = 3。 +在我们的第二次跳跃(偶数)中,我们从 i = 1 跳到 i = 2,因为 arr[2] 是(arr[2],arr[3],arr[4])中小于或等于 arr[1] 的最大值。arr[3] 也是最大的值,但 2 是一个较小的索引,所以我们只能跳到 i = 2,而不能跳到 i = 3。 -在我们的第三次跳跃(奇数)中,我们从 i = 2 跳到 i = 3,因为 A[3] 是(A[3],A[4])中大于或等于 A[2] 的最小值。 +在我们的第三次跳跃(奇数)中,我们从 i = 2 跳到 i = 3,因为 arr[3] 是(arr[3],arr[4])中大于或等于 arr[2] 的最小值。 我们不能从 i = 3 跳到 i = 4,所以起始索引 i = 0 不是好的起始索引。 @@ -73,7 +75,8 @@ tags:示例 3:
-输入:[5,1,3,4,2] ++输入:[5,1,3,4,2] 输出:3 解释: 我们可以从起始索引 1,2,4 出发到达数组末尾。 @@ -84,8 +87,8 @@ tags:提示:
-
diff --git a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md index 9fd1818df76b2..fb8cf597844ea 100644 --- a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md +++ b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md @@ -25,10 +25,10 @@ tags:- -
1 <= A.length <= 20000
- +
0 <= A[i] < 100000
- +
1 <= arr.length <= 20000
0 <= arr[i] < 100000
示例 1:
输入:n = 234 -输出:15 +输出:15 解释: -各位数之积 = 2 * 3 * 4 = 24 -各位数之和 = 2 + 3 + 4 = 9 +各位数之积 = 2 * 3 * 4 = 24 +各位数之和 = 2 + 3 + 4 = 9 结果 = 24 - 9 = 15@@ -36,9 +36,9 @@ tags:输入:n = 4421 输出:21 -解释: -各位数之积 = 4 * 4 * 2 * 1 = 32 -各位数之和 = 4 + 4 + 2 + 1 = 11 +解释: +各位数之积 = 4 * 4 * 2 * 1 = 32 +各位数之和 = 4 + 4 + 2 + 1 = 11 结果 = 32 - 11 = 21diff --git a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md index a6251a0511bcf..747d67bfc0a8b 100644 --- a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md +++ b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md @@ -25,10 +25,10 @@ Given an integer numbern
, return the difference between the producInput: n = 234 -Output: 15 -Explanation: -Product of digits = 2 * 3 * 4 = 24 -Sum of digits = 2 + 3 + 4 = 9 +Output: 15 +Explanation: +Product of digits = 2 * 3 * 4 = 24 +Sum of digits = 2 + 3 + 4 = 9 Result = 24 - 9 = 15@@ -37,9 +37,9 @@ Result = 24 - 9 = 15Input: n = 4421 Output: 21 -Explanation: -Product of digits = 4 * 4 * 2 * 1 = 32 -Sum of digits = 4 + 4 + 2 + 1 = 11 +Explanation: +Product of digits = 4 * 4 * 2 * 1 = 32 +Sum of digits = 4 + 4 + 2 + 1 = 11 Result = 32 - 11 = 21diff --git a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md index ebba6f60b91fa..6a05d1729477a 100644 --- a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md +++ b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md @@ -41,7 +41,7 @@ tags:输入:nums = [555,901,482,1771] -输出:1 +输出:1 解释: 只有 1771 是位数为偶数的数字。diff --git a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README_EN.md b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README_EN.md index 2a92c8c6d1e4b..85b2b1a18b99c 100644 --- a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README_EN.md +++ b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README_EN.md @@ -27,7 +27,7 @@ tags:Input: nums = [12,345,2,6,7896] Output: 2 -Explanation: +Explanation: 12 contains 2 digits (even number of digits). 345 contains 3 digits (odd number of digits). 2 contains 1 digit (odd number of digits). @@ -40,7 +40,7 @@ Therefore only 12 and 7896 contain an even number of digits.Input: nums = [555,901,482,1771] -Output: 1 +Output: 1 Explanation: Only 1771 contains an even number of digits.diff --git a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README_EN.md b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README_EN.md index 5cd11d3f3f03c..daf591b44707a 100644 --- a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README_EN.md +++ b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README_EN.md @@ -28,7 +28,7 @@ tags:Input: arr = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] -Explanation: +Explanation: - index 0 --> the greatest element to the right of index 0 is index 1 (18). - index 1 --> the greatest element to the right of index 1 is index 4 (6). - index 2 --> the greatest element to the right of index 2 is index 4 (6). diff --git a/solution/1400-1499/1470.Shuffle the Array/README.md b/solution/1400-1499/1470.Shuffle the Array/README.md index 376f0ef21ca39..0abf200137426 100644 --- a/solution/1400-1499/1470.Shuffle the Array/README.md +++ b/solution/1400-1499/1470.Shuffle the Array/README.md @@ -27,7 +27,7 @@ tags:diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md index 72d7dfd45532f..7546d3beeda30 100644 --- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md +++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/README_EN.md @@ -32,7 +32,7 @@ tags:示例 1:
输入:nums = [2,5,1,3,4,7], n = 3 -输出:[2,3,5,4,1,7] +输出:[2,3,5,4,1,7] 解释:由于 x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 ,所以答案为 [2,3,5,4,1,7]diff --git a/solution/1400-1499/1470.Shuffle the Array/README_EN.md b/solution/1400-1499/1470.Shuffle the Array/README_EN.md index efdc95874d1c1..8a806cd3fd73d 100644 --- a/solution/1400-1499/1470.Shuffle the Array/README_EN.md +++ b/solution/1400-1499/1470.Shuffle the Array/README_EN.md @@ -30,7 +30,7 @@ tags: Input: nums = [2,5,1,3,4,7], n = 3 -Output: [2,3,5,4,1,7] +Output: [2,3,5,4,1,7] Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. diff --git a/solution/1400-1499/1473.Paint House III/README_EN.md b/solution/1400-1499/1473.Paint House III/README_EN.md index 804a5898db39a..0c13ec8adc04a 100644 --- a/solution/1400-1499/1473.Paint House III/README_EN.md +++ b/solution/1400-1499/1473.Paint House III/README_EN.md @@ -53,7 +53,7 @@ Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. Input: houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 Output: 11 Explanation: Some houses are already painted, Paint the houses of this way [2,2,1,2,2] -This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. +This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11.Input: prices = [8,4,6,2,3] Output: [4,2,4,2,3] -Explanation: +Explanation: For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4. For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2. For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4. diff --git a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/README.md b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/README.md index 366fe481c6cf9..af2911a1357a6 100644 --- a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/README.md +++ b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/README.md @@ -90,7 +90,21 @@ tags: -### 方法一 +### 方法一:二分查找 + +根据题目描述,如果一个天数 $t$ 可以满足制作 $m$ 束花,那么对任意 $t' > t$,也一定可以满足制作 $m$ 束花。因此,我们可以使用二分查找的方法找到最小的满足制作 $m$ 束花的天数。 + +我们记花园中最大的开花天数为 $mx$,接下来,我们定义二分查找的左边界 $l = 1$,右边界 $r = mx + 1$。 + +然后,我们进行二分查找,对于每一个中间值 $\textit{mid} = \frac{l + r}{2}$,我们判断是否可以制作 $m$ 束花。如果可以,我们将右边界 $r$ 更新为 $\textit{mid}$,否则,我们将左边界 $l$ 更新为 $\textit{mid} + 1$。 + +最终,当 $l = r$ 时,结束二分查找。此时如果 $l > mx$,说明无法制作 $m$ 束花,返回 $-1$,否则返回 $l$。 + +因此,问题转换为判断一个天数 $\textit{days}$ 是否可以制作 $m$ 束花。 + +我们可以使用一个函数 $\text{check}(\textit{days})$ 来判断是否可以制作 $m$ 束花。具体地,我们从左到右遍历花园中的每一朵花,如果当前花开的天数小于等于 $\textit{days}$,我们将当前花加入到当前花束中,否则,我们将当前花束清空。当当前花束中的花的数量等于 $k$ 时,我们将当前花束的数量加一,并清空当前花束。最后,我们判断当前花束的数量是否大于等于 $m$,如果是,说明可以制作 $m$ 束花,否则,说明无法制作 $m$ 束花。 + +时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别为花园中的花的数量和最大的开花天数,本题中 $M \leq 10^9$。空间复杂度 $O(1)$。 @@ -99,59 +113,50 @@ tags: ```python class Solution: def minDays(self, bloomDay: List[int], m: int, k: int) -> int: - if m * k > len(bloomDay): - return -1 - - def check(day: int) -> bool: + def check(days: int) -> int: cnt = cur = 0 - for bd in bloomDay: - cur = cur + 1 if bd <= day else 0 + for x in bloomDay: + cur = cur + 1 if x <= days else 0 if cur == k: cnt += 1 cur = 0 return cnt >= m - left, right = min(bloomDay), max(bloomDay) - while left < right: - mid = (left + right) >> 1 - if check(mid): - right = mid - else: - left = mid + 1 - return left + mx = max(bloomDay) + l = bisect_left(range(mx + 2), True, key=check) + return -1 if l > mx else l ``` #### Java ```java class Solution { + private int[] bloomDay; + private int m, k; + public int minDays(int[] bloomDay, int m, int k) { - if (m * k > bloomDay.length) { - return -1; - } - int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; - for (int bd : bloomDay) { - min = Math.min(min, bd); - max = Math.max(max, bd); - } - int left = min, right = max; - while (left < right) { - int mid = (left + right) >>> 1; - if (check(bloomDay, m, k, mid)) { - right = mid; + this.bloomDay = bloomDay; + this.m = m; + this.k = k; + final int mx = (int) 1e9; + int l = 1, r = mx + 1; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; } else { - left = mid + 1; + l = mid + 1; } } - return left; + return l > mx ? -1 : l; } - private boolean check(int[] bloomDay, int m, int k, int day) { + private boolean check(int days) { int cnt = 0, cur = 0; - for (int bd : bloomDay) { - cur = bd <= day ? cur + 1 : 0; + for (int x : bloomDay) { + cur = x <= days ? cur + 1 : 0; if (cur == k) { - cnt++; + ++cnt; cur = 0; } } @@ -166,36 +171,28 @@ class Solution { class Solution { public: int minDays(vector-& bloomDay, int m, int k) { - if (m * k > bloomDay.size()) { - return -1; - } - int mi = INT_MIN, mx = INT_MAX; - for (int& bd : bloomDay) { - mi = min(mi, bd); - mx = max(mx, bd); - } - int left = mi, right = mx; - while (left < right) { - int mid = left + right >> 1; - if (check(bloomDay, m, k, mid)) { - right = mid; - } else { - left = mid + 1; + int mx = ranges::max(bloomDay); + int l = 1, r = mx + 1; + auto check = [&](int days) { + int cnt = 0, cur = 0; + for (int x : bloomDay) { + cur = x <= days ? cur + 1 : 0; + if (cur == k) { + cnt++; + cur = 0; + } } - } - return left; - } - - bool check(vector & bloomDay, int m, int k, int day) { - int cnt = 0, cur = 0; - for (int& bd : bloomDay) { - cur = bd <= day ? cur + 1 : 0; - if (cur == k) { - ++cnt; - cur = 0; + return cnt >= m; + }; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; } } - return cnt >= m; + return l > mx ? -1 : l; } }; ``` @@ -204,40 +201,55 @@ public: ```go func minDays(bloomDay []int, m int, k int) int { - if m*k > len(bloomDay) { - return -1 - } - mi, mx := 0, 1000000000 - for _, bd := range bloomDay { - mi = min(mi, bd) - mx = max(mx, bd) - } - left, right := mi, mx - for left < right { - mid := (left + right) >> 1 - if check(bloomDay, m, k, mid) { - right = mid - } else { - left = mid + 1 + mx := slices.Max(bloomDay) + if l := sort.Search(mx+2, func(days int) bool { + cnt, cur := 0, 0 + for _, x := range bloomDay { + if x <= days { + cur++ + if cur == k { + cnt++ + cur = 0 + } + } else { + cur = 0 + } } + return cnt >= m + }); l <= mx { + return l } - return left + return -1 + } +``` -func check(bloomDay []int, m, k, day int) bool { - cnt, cur := 0, 0 - for _, bd := range bloomDay { - if bd <= day { - cur++ - } else { - cur = 0 - } - if cur == k { - cnt++ - cur = 0 - } - } - return cnt >= m +#### TypeScript + +```ts +function minDays(bloomDay: number[], m: number, k: number): number { + const mx = Math.max(...bloomDay); + let [l, r] = [1, mx + 1]; + const check = (days: number): boolean => { + let [cnt, cur] = [0, 0]; + for (const x of bloomDay) { + cur = x <= days ? cur + 1 : 0; + if (cur === k) { + cnt++; + cur = 0; + } + } + return cnt >= m; + }; + while (l < r) { + const mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > mx ? -1 : l; } ``` diff --git a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/README_EN.md b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/README_EN.md index 419d1cbfd33e4..31731f50e9996 100644 --- a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/README_EN.md +++ b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/README_EN.md @@ -78,7 +78,21 @@ It is obvious that we can make two bouquets in different ways. -### Solution 1 +### Solution 1: Binary Search + +According to the problem description, if a day $t$ can satisfy making $m$ bouquets, then for any $t' > t$, it can also satisfy making $m$ bouquets. Therefore, we can use binary search to find the minimum day that satisfies making $m$ bouquets. + +Let $mx$ be the maximum blooming day in the garden. Next, we define the left boundary of the binary search as $l = 1$ and the right boundary as $r = mx + 1$. + +Then, we perform binary search. For each middle value $\textit{mid} = \frac{l + r}{2}$, we check if it is possible to make $m$ bouquets. If it is possible, we update the right boundary $r$ to $\textit{mid}$; otherwise, we update the left boundary $l$ to $\textit{mid} + 1$. + +Finally, when $l = r$, the binary search ends. At this point, if $l > mx$, it means it is not possible to make $m$ bouquets, and we return $-1$; otherwise, we return $l$. + +Therefore, the problem is reduced to checking if a day $\textit{days}$ can make $m$ bouquets. + +We can use a function $\text{check}(\textit{days})$ to determine if it is possible to make $m$ bouquets. Specifically, we traverse each flower in the garden from left to right. If the blooming day of the current flower is less than or equal to $\textit{days}$, we add the current flower to the current bouquet; otherwise, we clear the current bouquet. When the number of flowers in the current bouquet equals $k$, we increment the bouquet count and clear the current bouquet. Finally, we check if the bouquet count is greater than or equal to $m$. If it is, it means it is possible to make $m$ bouquets; otherwise, it is not possible. + +The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the number of flowers in the garden and the maximum blooming day, respectively. In this problem, $M \leq 10^9$. The space complexity is $O(1)$. @@ -87,59 +101,50 @@ It is obvious that we can make two bouquets in different ways. ```python class Solution: def minDays(self, bloomDay: List[int], m: int, k: int) -> int: - if m * k > len(bloomDay): - return -1 - - def check(day: int) -> bool: + def check(days: int) -> int: cnt = cur = 0 - for bd in bloomDay: - cur = cur + 1 if bd <= day else 0 + for x in bloomDay: + cur = cur + 1 if x <= days else 0 if cur == k: cnt += 1 cur = 0 return cnt >= m - left, right = min(bloomDay), max(bloomDay) - while left < right: - mid = (left + right) >> 1 - if check(mid): - right = mid - else: - left = mid + 1 - return left + mx = max(bloomDay) + l = bisect_left(range(mx + 2), True, key=check) + return -1 if l > mx else l ``` #### Java ```java class Solution { + private int[] bloomDay; + private int m, k; + public int minDays(int[] bloomDay, int m, int k) { - if (m * k > bloomDay.length) { - return -1; - } - int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; - for (int bd : bloomDay) { - min = Math.min(min, bd); - max = Math.max(max, bd); - } - int left = min, right = max; - while (left < right) { - int mid = (left + right) >>> 1; - if (check(bloomDay, m, k, mid)) { - right = mid; + this.bloomDay = bloomDay; + this.m = m; + this.k = k; + final int mx = (int) 1e9; + int l = 1, r = mx + 1; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; } else { - left = mid + 1; + l = mid + 1; } } - return left; + return l > mx ? -1 : l; } - private boolean check(int[] bloomDay, int m, int k, int day) { + private boolean check(int days) { int cnt = 0, cur = 0; - for (int bd : bloomDay) { - cur = bd <= day ? cur + 1 : 0; + for (int x : bloomDay) { + cur = x <= days ? cur + 1 : 0; if (cur == k) { - cnt++; + ++cnt; cur = 0; } } @@ -154,36 +159,28 @@ class Solution { class Solution { public: int minDays(vector & bloomDay, int m, int k) { - if (m * k > bloomDay.size()) { - return -1; - } - int mi = INT_MIN, mx = INT_MAX; - for (int& bd : bloomDay) { - mi = min(mi, bd); - mx = max(mx, bd); - } - int left = mi, right = mx; - while (left < right) { - int mid = left + right >> 1; - if (check(bloomDay, m, k, mid)) { - right = mid; - } else { - left = mid + 1; + int mx = ranges::max(bloomDay); + int l = 1, r = mx + 1; + auto check = [&](int days) { + int cnt = 0, cur = 0; + for (int x : bloomDay) { + cur = x <= days ? cur + 1 : 0; + if (cur == k) { + cnt++; + cur = 0; + } } - } - return left; - } - - bool check(vector & bloomDay, int m, int k, int day) { - int cnt = 0, cur = 0; - for (int& bd : bloomDay) { - cur = bd <= day ? cur + 1 : 0; - if (cur == k) { - ++cnt; - cur = 0; + return cnt >= m; + }; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; } } - return cnt >= m; + return l > mx ? -1 : l; } }; ``` @@ -192,40 +189,55 @@ public: ```go func minDays(bloomDay []int, m int, k int) int { - if m*k > len(bloomDay) { - return -1 - } - mi, mx := 0, 1000000000 - for _, bd := range bloomDay { - mi = min(mi, bd) - mx = max(mx, bd) - } - left, right := mi, mx - for left < right { - mid := (left + right) >> 1 - if check(bloomDay, m, k, mid) { - right = mid - } else { - left = mid + 1 + mx := slices.Max(bloomDay) + if l := sort.Search(mx+2, func(days int) bool { + cnt, cur := 0, 0 + for _, x := range bloomDay { + if x <= days { + cur++ + if cur == k { + cnt++ + cur = 0 + } + } else { + cur = 0 + } } + return cnt >= m + }); l <= mx { + return l } - return left + return -1 + } +``` -func check(bloomDay []int, m, k, day int) bool { - cnt, cur := 0, 0 - for _, bd := range bloomDay { - if bd <= day { - cur++ - } else { - cur = 0 - } - if cur == k { - cnt++ - cur = 0 - } - } - return cnt >= m +#### TypeScript + +```ts +function minDays(bloomDay: number[], m: number, k: number): number { + const mx = Math.max(...bloomDay); + let [l, r] = [1, mx + 1]; + const check = (days: number): boolean => { + let [cnt, cur] = [0, 0]; + for (const x of bloomDay) { + cur = x <= days ? cur + 1 : 0; + if (cur === k) { + cnt++; + cur = 0; + } + } + return cnt >= m; + }; + while (l < r) { + const mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > mx ? -1 : l; } ``` diff --git a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.cpp b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.cpp index de3424858b0ed..fe93831e60858 100644 --- a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.cpp +++ b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.cpp @@ -1,35 +1,27 @@ class Solution { public: int minDays(vector & bloomDay, int m, int k) { - if (m * k > bloomDay.size()) { - return -1; - } - int mi = INT_MIN, mx = INT_MAX; - for (int& bd : bloomDay) { - mi = min(mi, bd); - mx = max(mx, bd); - } - int left = mi, right = mx; - while (left < right) { - int mid = left + right >> 1; - if (check(bloomDay, m, k, mid)) { - right = mid; - } else { - left = mid + 1; + int mx = ranges::max(bloomDay); + int l = 1, r = mx + 1; + auto check = [&](int days) { + int cnt = 0, cur = 0; + for (int x : bloomDay) { + cur = x <= days ? cur + 1 : 0; + if (cur == k) { + cnt++; + cur = 0; + } } - } - return left; - } - - bool check(vector & bloomDay, int m, int k, int day) { - int cnt = 0, cur = 0; - for (int& bd : bloomDay) { - cur = bd <= day ? cur + 1 : 0; - if (cur == k) { - ++cnt; - cur = 0; + return cnt >= m; + }; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; } } - return cnt >= m; + return l > mx ? -1 : l; } -}; \ No newline at end of file +}; diff --git a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.go b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.go index c9986ab44b997..83d8734ed83db 100644 --- a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.go +++ b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.go @@ -1,36 +1,22 @@ func minDays(bloomDay []int, m int, k int) int { - if m*k > len(bloomDay) { - return -1 - } - mi, mx := 0, 1000000000 - for _, bd := range bloomDay { - mi = min(mi, bd) - mx = max(mx, bd) - } - left, right := mi, mx - for left < right { - mid := (left + right) >> 1 - if check(bloomDay, m, k, mid) { - right = mid - } else { - left = mid + 1 + mx := slices.Max(bloomDay) + if l := sort.Search(mx+2, func(days int) bool { + cnt, cur := 0, 0 + for _, x := range bloomDay { + if x <= days { + cur++ + if cur == k { + cnt++ + cur = 0 + } + } else { + cur = 0 + } } + return cnt >= m + }); l <= mx { + return l } - return left -} + return -1 -func check(bloomDay []int, m, k, day int) bool { - cnt, cur := 0, 0 - for _, bd := range bloomDay { - if bd <= day { - cur++ - } else { - cur = 0 - } - if cur == k { - cnt++ - cur = 0 - } - } - return cnt >= m -} \ No newline at end of file +} diff --git a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.java b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.java index ea962af7212e6..04a16e8fe01ee 100644 --- a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.java +++ b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.java @@ -1,34 +1,33 @@ class Solution { + private int[] bloomDay; + private int m, k; + public int minDays(int[] bloomDay, int m, int k) { - if (m * k > bloomDay.length) { - return -1; - } - int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; - for (int bd : bloomDay) { - min = Math.min(min, bd); - max = Math.max(max, bd); - } - int left = min, right = max; - while (left < right) { - int mid = (left + right) >>> 1; - if (check(bloomDay, m, k, mid)) { - right = mid; + this.bloomDay = bloomDay; + this.m = m; + this.k = k; + final int mx = (int) 1e9; + int l = 1, r = mx + 1; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; } else { - left = mid + 1; + l = mid + 1; } } - return left; + return l > mx ? -1 : l; } - private boolean check(int[] bloomDay, int m, int k, int day) { + private boolean check(int days) { int cnt = 0, cur = 0; - for (int bd : bloomDay) { - cur = bd <= day ? cur + 1 : 0; + for (int x : bloomDay) { + cur = x <= days ? cur + 1 : 0; if (cur == k) { - cnt++; + ++cnt; cur = 0; } } return cnt >= m; } -} \ No newline at end of file +} diff --git a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.py b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.py index dbfd1a53635a4..5475b146aa761 100644 --- a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.py +++ b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.py @@ -1,22 +1,14 @@ class Solution: def minDays(self, bloomDay: List[int], m: int, k: int) -> int: - if m * k > len(bloomDay): - return -1 - - def check(day: int) -> bool: + def check(days: int) -> int: cnt = cur = 0 - for bd in bloomDay: - cur = cur + 1 if bd <= day else 0 + for x in bloomDay: + cur = cur + 1 if x <= days else 0 if cur == k: cnt += 1 cur = 0 return cnt >= m - left, right = min(bloomDay), max(bloomDay) - while left < right: - mid = (left + right) >> 1 - if check(mid): - right = mid - else: - left = mid + 1 - return left + mx = max(bloomDay) + l = bisect_left(range(mx + 2), True, key=check) + return -1 if l > mx else l diff --git a/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.ts b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.ts new file mode 100644 index 0000000000000..1ad67a6078e88 --- /dev/null +++ b/solution/1400-1499/1482.Minimum Number of Days to Make m Bouquets/Solution.ts @@ -0,0 +1,24 @@ +function minDays(bloomDay: number[], m: number, k: number): number { + const mx = Math.max(...bloomDay); + let [l, r] = [1, mx + 1]; + const check = (days: number): boolean => { + let [cnt, cur] = [0, 0]; + for (const x of bloomDay) { + cur = x <= days ? cur + 1 : 0; + if (cur === k) { + cnt++; + cur = 0; + } + } + return cnt >= m; + }; + while (l < r) { + const mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > mx ? -1 : l; +} diff --git a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md index f461cf8c542c6..2b8aaf021084b 100644 --- a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md +++ b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md @@ -27,11 +27,11 @@ tags: 例如,第 -1
趟列车需要1.5
小时,那你必须再等待0.5
小时,搭乘在第 2 小时发车的第2
趟列车。返回能满足你准时到达办公室所要求全部列车的 最小正整数 时速(单位:千米每小时),如果无法准时到达,则返回
+-1
。返回能满足你在时限前到达办公室所要求全部列车的 最小正整数 时速(单位:千米每小时),如果无法准时到达,则返回
-1
。生成的测试用例保证答案不超过
-107
,且hour
的 小数点后最多存在两位数字 。+
示例 1:
@@ -63,15 +63,15 @@ tags: 输出:-1 解释:不可能准时到达,因为第 3 趟列车最早是在第 2 小时发车。+
提示:
diff --git a/solution/2600-2699/2694.Event Emitter/README_EN.md b/solution/2600-2699/2694.Event Emitter/README_EN.md index e7c28c1bf15bf..6759594507da2 100644 --- a/solution/2600-2699/2694.Event Emitter/README_EN.md +++ b/solution/2600-2699/2694.Event Emitter/README_EN.md @@ -33,7 +33,7 @@ tags:
- -
n == dist.length
- -
1 <= n <= 105
- -
1 <= dist[i] <= 105
- +
1 <= hour <= 109
- +
1 <= n <= 105
- +
1 <= dist[i] <= 105
1 <= hour <= 109
hours
中,小数点后最多存在两位数字Input: actions = ["EventEmitter", "emit", "subscribe", "subscribe", "emit"], -values = [[], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent", "function cb1() { return 6; }"], ["firstEvent"]] +values = [[], ["firstEvent"], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent", "function cb1() { return 6; }"], ["firstEvent"]] Output: [[],["emitted",[]],["subscribed"],["subscribed"],["emitted",[5,6]]] Explanation: const emitter = new EventEmitter(); diff --git a/solution/3100-3199/3126.Server Utilization Time/README.md b/solution/3100-3199/3126.Server Utilization Time/README.md index 814b0577b4e33..1fe601412550e 100644 --- a/solution/3100-3199/3126.Server Utilization Time/README.md +++ b/solution/3100-3199/3126.Server Utilization Time/README.md @@ -31,7 +31,7 @@ session_status 是 ('start', 'stop') 的 ENUM (category)。 这张表的每一行包含 server_id, status_time 和 session_status。-编写一个解决方案来查找服务器 运行 的 总时间。输出应四舍五入为最接近的 整天数。
+编写一个解决方案来查找服务器 运行 的 总时间。输出应向下舍入为最接近的 整天数。
以 任意 顺序返回结果表。
diff --git a/solution/3200-3299/3279.Maximum Total Area Occupied by Pistons/README.md b/solution/3200-3299/3279.Maximum Total Area Occupied by Pistons/README.md index 6d82924e44957..b0de44bc3caf5 100644 --- a/solution/3200-3299/3279.Maximum Total Area Occupied by Pistons/README.md +++ b/solution/3200-3299/3279.Maximum Total Area Occupied by Pistons/README.md @@ -13,7 +13,7 @@ tags: -# [3279. 活塞占据的最大总面积 🔒](https://leetcode.cn/problems/maximum-total-area-occupied-by-pistons) +# [3279. 活塞占据的最大总区域 🔒](https://leetcode.cn/problems/maximum-total-area-occupied-by-pistons) [English Version](/solution/3200-3299/3279.Maximum%20Total%20Area%20Occupied%20by%20Pistons/README_EN.md) @@ -21,13 +21,13 @@ tags: -一台旧车的引擎中有一些活塞,我们想要计算活塞 下方 的 最大 面积。
+一台旧车的引擎中有一些活塞,我们想要计算活塞 下方 的 最大 区域。
给定:
@@ -38,7 +38,7 @@ tags:
- 一个整数
-height
,表示活塞 最大 可到达的高度。- 一个整数数组
+positions
,其中positions[i]
是活塞i
的当前位置,等于其 下方 的当前面积。- 一个整数数组
positions
,其中positions[i]
是活塞i
的当前位置,等于其 下方 的当前区域。- 一个字符串
directions
,其中directions[i]
是活塞i
的当前移动方向,'U'
表示向上,'D'
表示向下。如果一个活塞到达了其中一个终点,即 -positions[i] == 0
或positions[i] == height
,它的方向将会改变。返回所有活塞下方的最大可能面积。
+返回所有活塞下方的最大可能区域。
@@ -51,7 +51,7 @@ tags:
解释:
-当前活塞的位置下方面积最大。
+当前活塞的位置下方区域最大。
示例 2:
@@ -63,7 +63,7 @@ tags:解释:
-三秒后,活塞将会位于
+[3, 3, 3, 6]
,此时下方面积最大。三秒后,活塞将会位于
[3, 3, 3, 6]
,此时下方区域最大。diff --git a/solution/3200-3299/3284.Sum of Consecutive Subarrays/README.md b/solution/3200-3299/3284.Sum of Consecutive Subarrays/README.md index 102998410f492..70a4882133bec 100644 --- a/solution/3200-3299/3284.Sum of Consecutive Subarrays/README.md +++ b/solution/3200-3299/3284.Sum of Consecutive Subarrays/README.md @@ -18,7 +18,7 @@ tags: -
如果一个长度为
+n
的数组arr
符合下面条件,可以称它 连续:如果一个长度为
n
的数组arr
符合下面其中一个条件,可以称它 连续:
- 对于所有的
@@ -27,7 +27,7 @@ tags:1 <= i < n
,arr[i] - arr[i - 1] == 1
。数组的 值 是其元素的和。
-例如,
+[3, 4, 5]
是一个值为 12 的连续数组,并且[9, 8]
是另一个值为 17 的子数组。而[3, 4, 3]
和[8, 6]
都不连续。例如,
[3, 4, 5]
是一个值为 12 的连续数组,并且[9, 8]
是另一个值为 17 的连续数组。而[3, 4, 3]
和[8, 6]
都不连续。给定一个整数数组
diff --git a/solution/3200-3299/3299.Sum of Consecutive Subsequences/README.md b/solution/3200-3299/3299.Sum of Consecutive Subsequences/README.md index ef1a94f109d51..c49a65dd8e684 100644 --- a/solution/3200-3299/3299.Sum of Consecutive Subsequences/README.md +++ b/solution/3200-3299/3299.Sum of Consecutive Subsequences/README.md @@ -18,7 +18,7 @@ tags: -nums
,返回所有 连续 子数组 的 值 之和。如果一个长度为
+n
的数组arr
符合下面条件,可以称它 连续:如果一个长度为
n
的数组arr
符合下面其中一个条件,可以称它 连续:
- 对于所有的
@@ -27,7 +27,7 @@ tags:1 <= i < n
,arr[i] - arr[i - 1] == 1
。数组的 值 是其元素的和。
-例如,
+[3, 4, 5]
是一个值为 12 的连续数组,并且[9, 8]
是另一个值为 17 的子数组。而[3, 4, 3]
和[8, 6]
都不连续。例如,
[3, 4, 5]
是一个值为 12 的连续数组,并且[9, 8]
是另一个值为 17 的连续数组。而[3, 4, 3]
和[8, 6]
都不连续。给定一个整数数组
diff --git a/solution/3300-3399/3303.Find the Occurrence of First Almost Equal Substring/README.md b/solution/3300-3399/3303.Find the Occurrence of First Almost Equal Substring/README.md index 062013d915c69..65a8e2ca1843f 100644 --- a/solution/3300-3399/3303.Find the Occurrence of First Almost Equal Substring/README.md +++ b/solution/3300-3399/3303.Find the Occurrence of First Almost Equal Substring/README.md @@ -73,7 +73,7 @@ tags:nums
,返回所有 连续 非空 子序列 的 值 之和。提示:
-
diff --git a/solution/README.md b/solution/README.md index 56eec3a709d7f..860caf1e6e16f 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3289,7 +3289,7 @@ | 3276 | [选择矩阵中单元格的最大得分](/solution/3200-3299/3276.Select%20Cells%20in%20Grid%20With%20Maximum%20Score/README.md) | `位运算`,`数组`,`动态规划`,`状态压缩`,`矩阵` | 困难 | 第 413 场周赛 | | 3277 | [查询子数组最大异或值](/solution/3200-3299/3277.Maximum%20XOR%20Score%20Subarray%20Queries/README.md) | `数组`,`动态规划` | 困难 | 第 413 场周赛 | | 3278 | [寻找数据科学家职位的候选人 II](/solution/3200-3299/3278.Find%20Candidates%20for%20Data%20Scientist%20Position%20II/README.md) | `数据库` | 中等 | 🔒 | -| 3279 | [活塞占据的最大总面积](/solution/3200-3299/3279.Maximum%20Total%20Area%20Occupied%20by%20Pistons/README.md) | `数组`,`哈希表`,`字符串`,`计数`,`前缀和`,`模拟` | 困难 | 🔒 | +| 3279 | [活塞占据的最大总区域](/solution/3200-3299/3279.Maximum%20Total%20Area%20Occupied%20by%20Pistons/README.md) | `数组`,`哈希表`,`字符串`,`计数`,`前缀和`,`模拟` | 困难 | 🔒 | | 3280 | [将日期转换为二进制表示](/solution/3200-3299/3280.Convert%20Date%20to%20Binary/README.md) | `数学`,`字符串` | 简单 | 第 414 场周赛 | | 3281 | [范围内整数的最大得分](/solution/3200-3299/3281.Maximize%20Score%20of%20Numbers%20in%20Ranges/README.md) | `贪心`,`数组`,`二分查找`,`排序` | 中等 | 第 414 场周赛 | | 3282 | [到达数组末尾的最大得分](/solution/3200-3299/3282.Reach%20End%20of%20Array%20With%20Max%20Score/README.md) | `贪心`,`数组` | 中等 | 第 414 场周赛 |- +
1 <= pattern.length < s.length <= 3 * 105
1 <= pattern.length < s.length <= 105
s
和pattern
都只包含小写英文字母。