Skip to content

Commit 9effade

Browse files
halfrostdezhiy
authored andcommitted
Update 0352 solution
1 parent 44541dc commit 9effade

File tree

7 files changed

+1603
-1468
lines changed

7 files changed

+1603
-1468
lines changed

README.md

Lines changed: 1261 additions & 1246 deletions
Large diffs are not rendered by default.

leetcode/0352.Data-Stream-as-Disjoint-Intervals/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [352. Data Stream as Disjoint Intervals](https://leetcode-cn.com/problems/data-stream-as-disjoint-intervals/)
1+
# [352. Data Stream as Disjoint Intervals](https://leetcode.com/problems/data-stream-as-disjoint-intervals/)
22

33

44
## 题目

leetcode/0875.Koko-Eating-Bananas/875. Koko Eating Bananas_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ func Test_Problem875(t *testing.T) {
4141
para875{[]int{30, 11, 23, 4, 20}, 6},
4242
ans875{23},
4343
},
44+
45+
{
46+
para875{[]int{4, 9, 11, 17}, 8},
47+
ans875{6},
48+
},
4449
}
4550

4651
fmt.Printf("------------------------Leetcode Problem 875------------------------\n")

website/content/ChapterFour/0300~0399/0350.Intersection-of-Two-Arrays-II.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ func intersect(nums1 []int, nums2 []int) []int {
7575
----------------------------------------------
7676
<div style="display: flex;justify-content: space-between;align-items: center;">
7777
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0349.Intersection-of-Two-Arrays/">⬅️上一页</a></p>
78-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0354.Russian-Doll-Envelopes/">下一页➡️</a></p>
78+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0352.Data-Stream-as-Disjoint-Intervals/">下一页➡️</a></p>
7979
</div>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# [352. Data Stream as Disjoint Intervals](https://leetcode.com/problems/data-stream-as-disjoint-intervals/)
2+
3+
4+
## 题目
5+
6+
Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals.
7+
8+
Implement the SummaryRanges class:
9+
10+
- SummaryRanges() Initializes the object with an empty stream.
11+
- void addNum(int val) Adds the integer val to the stream.
12+
- int[][] getIntervals() Returns a summary of the integers in the stream currently as a list of disjoint intervals [starti, endi].
13+
14+
**Example 1:**
15+
16+
Input
17+
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
18+
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
19+
Output
20+
[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]
21+
22+
Explanation
23+
SummaryRanges summaryRanges = new SummaryRanges();
24+
summaryRanges.addNum(1); // arr = [1]
25+
summaryRanges.getIntervals(); // return [[1, 1]]
26+
summaryRanges.addNum(3); // arr = [1, 3]
27+
summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
28+
summaryRanges.addNum(7); // arr = [1, 3, 7]
29+
summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
30+
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
31+
summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
32+
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
33+
summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
34+
35+
**Constraints**
36+
37+
- 0 <= val <= 10000
38+
- At most 3 * 10000 calls will be made to addNum and getIntervals.
39+
40+
## 题目大意
41+
42+
给你一个由非负整数a1, a2, ..., an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表。
43+
44+
实现 SummaryRanges 类:
45+
46+
- SummaryRanges() 使用一个空数据流初始化对象。
47+
- void addNum(int val) 向数据流中加入整数 val 。
48+
- int[][] getIntervals() 以不相交区间[starti, endi] 的列表形式返回对数据流中整数的总结
49+
50+
## 解题思路
51+
52+
- 使用字典过滤掉重复的数字
53+
- 把过滤后的数字放到nums中,并进行排序
54+
- 使用nums构建不重复的区间
55+
56+
## 代码
57+
58+
```go
59+
package leetcode
60+
61+
import "sort"
62+
63+
type SummaryRanges struct {
64+
nums []int
65+
mp map[int]int
66+
}
67+
68+
func Constructor() SummaryRanges {
69+
return SummaryRanges{
70+
nums: []int{},
71+
mp : map[int]int{},
72+
}
73+
}
74+
75+
func (this *SummaryRanges) AddNum(val int) {
76+
if _, ok := this.mp[val]; !ok {
77+
this.mp[val] = 1
78+
this.nums = append(this.nums, val)
79+
}
80+
sort.Ints(this.nums)
81+
}
82+
83+
func (this *SummaryRanges) GetIntervals() [][]int {
84+
n := len(this.nums)
85+
var ans [][]int
86+
if n == 0 {
87+
return ans
88+
}
89+
if n == 1 {
90+
ans = append(ans, []int{this.nums[0], this.nums[0]})
91+
return ans
92+
}
93+
start, end := this.nums[0], this.nums[0]
94+
ans = append(ans, []int{start, end})
95+
index := 0
96+
for i := 1; i < n; i++ {
97+
if this.nums[i] == end + 1 {
98+
end = this.nums[i]
99+
ans[index][1] = end
100+
} else {
101+
start, end = this.nums[i], this.nums[i]
102+
ans = append(ans, []int{start, end})
103+
index++
104+
}
105+
}
106+
return ans
107+
}
108+
```
109+
110+
111+
----------------------------------------------
112+
<div style="display: flex;justify-content: space-between;align-items: center;">
113+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0350.Intersection-of-Two-Arrays-II/">⬅️上一页</a></p>
114+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0354.Russian-Doll-Envelopes/">下一页➡️</a></p>
115+
</div>

website/content/ChapterFour/0300~0399/0354.Russian-Doll-Envelopes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ func maxEnvelopes(envelopes [][]int) int {
8484

8585
----------------------------------------------
8686
<div style="display: flex;justify-content: space-between;align-items: center;">
87-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0350.Intersection-of-Two-Arrays-II/">⬅️上一页</a></p>
87+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0352.Data-Stream-as-Disjoint-Intervals/">⬅️上一页</a></p>
8888
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0357.Count-Numbers-with-Unique-Digits/">下一页➡️</a></p>
8989
</div>

0 commit comments

Comments
 (0)