Skip to content

Commit ff55cb6

Browse files
committed
一刷915
1 parent c99d7de commit ff55cb6

File tree

6 files changed

+98
-43
lines changed

6 files changed

+98
-43
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6430,14 +6430,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
64306430
//|{doc_base_url}/0914-x-of-a-kind-in-a-deck-of-cards.adoc[题解]
64316431
//|Easy
64326432
//|
6433-
//
6434-
//|{counter:codes}
6435-
//|{leetcode_base_url}/partition-array-into-disjoint-intervals/[915. Partition Array into Disjoint Intervals^]
6436-
//|{source_base_url}/_0915_PartitionArrayIntoDisjointIntervals.java[Java]
6437-
//|{doc_base_url}/0915-partition-array-into-disjoint-intervals.adoc[题解]
6438-
//|Medium
6439-
//|
6440-
//
6433+
6434+
|{counter:codes}
6435+
|{leetcode_base_url}/partition-array-into-disjoint-intervals/[915. Partition Array into Disjoint Intervals^]
6436+
|{source_base_url}/_0915_PartitionArrayIntoDisjointIntervals.java[Java]
6437+
|{doc_base_url}/0915-partition-array-into-disjoint-intervals.adoc[题解]
6438+
|Medium
6439+
|
6440+
64416441
//|{counter:codes}
64426442
//|{leetcode_base_url}/word-subsets/[916. Word Subsets^]
64436443
//|{source_base_url}/_0916_WordSubsets.java[Java]
Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,74 @@
11
[#0915-partition-array-into-disjoint-intervals]
2-
= 915. Partition Array into Disjoint Intervals
2+
= 915. 分割数组
33

4-
{leetcode}/problems/partition-array-into-disjoint-intervals/[LeetCode - Partition Array into Disjoint Intervals^]
4+
https://leetcode.cn/problems/partition-array-into-disjoint-intervals/[LeetCode - 915. 分割数组 ^]
55

6-
Given an array `A`, partition it into two (contiguous) subarrays `left` and `right` so that:
6+
给定一个数组 `nums` ,将其划分为两个连续子数组 `left` `right`, 使得:
77

8+
* `left` 中的每个元素都小于或等于 `right` 中的每个元素。
9+
* `left``right` 都是非空的。
10+
* `left` 的长度要尽可能小。
811
9-
* Every element in `left` is less than or equal to every element in `right`.
10-
* `left` and `right` are non-empty.
11-
* `left` has the smallest possible size.
12+
_在完成这样的分组后返回 `left` 的 *长度 *_
1213

14+
用例可以保证存在这样的划分方法。
1315

14-
Return the *length* of `left` after such a partitioning. It is guaranteed that such a partitioning exists.
16+
*示例 1:*
1517

16-
18+
....
19+
输入:nums = [5,0,3,8,6]
20+
输出:3
21+
解释:left = [5,0,3],right = [8,6]
22+
....
1723

18-
*Example 1:*
24+
*示例 2:*
1925

20-
[subs="verbatim,quotes,macros"]
21-
----
22-
*Input:* [5,0,3,8,6]
23-
*Output:* 3
24-
*Explanation:* left = [5,0,3], right = [8,6]
25-
----
26-
27-
28-
*Example 2:*
29-
30-
[subs="verbatim,quotes,macros"]
31-
----
32-
*Input:* [1,1,1,0,6,12]
33-
*Output:* 4
34-
*Explanation:* left = [1,1,1,0], right = [6,12]
35-
----
26+
....
27+
输入:nums = [1,1,1,0,6,12]
28+
输出:4
29+
解释:left = [1,1,1,0],right = [6,12]
30+
....
3631

37-
3832

33+
*提示:*
3934

40-
*Note:*
35+
* `2 \<= nums.length \<= 10^5^`
36+
* `0 \<= nums[i] \<= 10^6^`
37+
* 可以保证至少有一种方法能够按题目所描述的那样对 `nums` 进行划分。
4138
4239
43-
* `2 <= A.length <= 30000`
44-
* `0 <= A[i] <= 10^6`
45-
* It is guaranteed there is at least one way to partition `A` as described.
46-
47-
48-
49-
40+
== 思路分析
5041

42+
两次遍历,先从右向左查找最小值,然后从左向右查找最大值,当最大值小于下一个的最小值,则就到了分界线。
5143

44+
下面这个题解更简单,只需要遍历不到一遍:
5245

46+
image::images/0915-10.png[{image_attr}]
5347

5448
[[src-0915]]
49+
[tabs]
50+
====
51+
一刷::
52+
+
53+
--
5554
[{java_src_attr}]
5655
----
5756
include::{sourcedir}/_0915_PartitionArrayIntoDisjointIntervals.java[tag=answer]
5857
----
58+
--
59+
60+
// 二刷::
61+
// +
62+
// --
63+
// [{java_src_attr}]
64+
// ----
65+
// include::{sourcedir}/_0915_PartitionArrayIntoDisjointIntervals_2.java[tag=answer]
66+
// ----
67+
// --
68+
====
69+
70+
71+
== 参考资料
5972

73+
. https://leetcode.cn/problems/partition-array-into-disjoint-intervals/solutions/1913934/fen-ge-shu-zu-by-leetcode-solution-t4pm/[915. 分割数组 - 官方题解^]
74+
. https://leetcode.cn/problems/partition-array-into-disjoint-intervals/solutions/1920768/zhua-wa-mou-si-tu-jie-leetcode-by-muse-7-omut/[915. 分割数组 - 【爪哇缪斯】图解LeetCode^] -- 不到一遍的遍历,牛逼!

docs/images/0915-10.png

167 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,7 @@ include::0912-sort-an-array.adoc[leveloffset=+1]
19131913

19141914
// include::0914-x-of-a-kind-in-a-deck-of-cards.adoc[leveloffset=+1]
19151915

1916-
// include::0915-partition-array-into-disjoint-intervals.adoc[leveloffset=+1]
1916+
include::0915-partition-array-into-disjoint-intervals.adoc[leveloffset=+1]
19171917

19181918
// include::0916-word-subsets.adoc[leveloffset=+1]
19191919

logbook/202503.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,12 @@ endif::[]
833833
|{doc_base_url}/1022-sum-of-root-to-leaf-binary-numbers.adoc[题解]
834834
|✅ 深度优先遍历。
835835

836+
|{counter:codes2503}
837+
|{leetcode_base_url}/partition-array-into-disjoint-intervals/[915. 分割数组^]
838+
|{doc_base_url}/0915-partition-array-into-disjoint-intervals.adoc[题解]
839+
|✅ 两遍遍历。有题解可以不到一遍遍历即可解决问题。
840+
841+
836842
|===
837843

838844
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0915_PartitionArrayIntoDisjointIntervals {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-26 11:39:29
9+
*/
10+
public int partitionDisjoint(int[] nums) {
11+
int length = nums.length;
12+
int[] ext = new int[length];
13+
ext[length - 1] = nums[length - 1];
14+
int min = nums[length - 1];
15+
for (int i = length - 2; i >= 0; i--) {
16+
min = Math.min(min, nums[i]);
17+
ext[i] = min;
18+
}
19+
int max = nums[0];
20+
for (int i = 0; i < length - 1; i++) {
21+
max = Math.max(max, nums[i]);
22+
if (max <= ext[i + 1]) {
23+
return i + 1;
24+
}
25+
}
26+
return length - 1;
27+
}
28+
29+
// end::answer[]
30+
public static void main(String[] args) {
31+
new _0915_PartitionArrayIntoDisjointIntervals()
32+
.partitionDisjoint(new int[]{1, 1, 1, 0, 6, 12});
33+
}
34+
}

0 commit comments

Comments
 (0)