Skip to content

Commit 1cce78a

Browse files
authored
feat: add solutions to lc problem: No.2251 (doocs#1334)
No.2251.Number of Flowers in Full Bloom
1 parent 35a803b commit 1cce78a

File tree

6 files changed

+600
-65
lines changed

6 files changed

+600
-65
lines changed

solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md

Lines changed: 251 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51-
离散差分+离散查询
51+
**方法一:排序 + 二分查找**
5252

53-
- 按开始时间对鲜花进行排序
54-
- 按结束时间对鲜花进行排序
55-
- 在每个人的新花数组中执行二分搜索
56-
- 对按开始时间排序的花进行分析,`bisect_right` 给出给定人员考虑的最后一朵花
57-
- 对按结束时间排序的花执行 `flowers` 操作`bisect_left`,给出针对给定人员考虑的第一朵花
58-
- 两者相减,就得到当时盛开的花朵
53+
我们将花按照开始时间和结束时间分别排序,然后对于每个人,我们可以使用二分查找来找到他们到达时在花期内花的数目。就是说,找出在每个人到达时,已经开花的花的数目,减去在每个人到达时,已经凋谢的花的数目,即可得到答案。
54+
55+
时间复杂度 $O((m + n) \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $flowers$ 和 $persons$ 的长度。
56+
57+
**方法二:差分 + 排序 + 离线查询**
58+
59+
我们可以利用差分来维护每个时间点的花的数目。接下来,我们将 $persons$ 按照到达时间从小到大排序,在每个人到达时,我们对差分数组进行前缀和运算,就可以得到答案。
60+
61+
时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $flowers$ 和 $persons$ 的长度。
5962

6063
<!-- tabs:start -->
6164

@@ -72,38 +75,262 @@ class Solution:
7275
return [bisect_right(start, p) - bisect_left(end, p) for p in persons]
7376
```
7477

78+
```python
79+
class Solution:
80+
def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]:
81+
d = defaultdict(int)
82+
for st, ed in flowers:
83+
d[st] += 1
84+
d[ed + 1] -= 1
85+
ts = sorted(d)
86+
s = i = 0
87+
m = len(people)
88+
ans = [0] * m
89+
for t, j in sorted(zip(people, range(m))):
90+
while i < len(ts) and ts[i] <= t:
91+
s += d[ts[i]]
92+
i += 1
93+
ans[j] = s
94+
return ans
95+
```
96+
7597
### **Java**
7698

7799
<!-- 这里可写当前语言的特殊实现逻辑 -->
78100

79101
```java
102+
class Solution {
103+
public int[] fullBloomFlowers(int[][] flowers, int[] people) {
104+
int n = flowers.length;
105+
int[] start = new int[n];
106+
int[] end = new int[n];
107+
for (int i = 0; i < n; ++i) {
108+
start[i] = flowers[i][0];
109+
end[i] = flowers[i][1];
110+
}
111+
Arrays.sort(start);
112+
Arrays.sort(end);
113+
int m = people.length;
114+
int[] ans = new int[m];
115+
for (int i = 0; i < m; ++i) {
116+
ans[i] = search(start, people[i] + 1) - search(end, people[i]);
117+
}
118+
return ans;
119+
}
120+
121+
private int search(int[] nums, int x) {
122+
int l = 0, r = nums.length;
123+
while (l < r) {
124+
int mid = (l + r) >> 1;
125+
if (nums[mid] >= x) {
126+
r = mid;
127+
} else {
128+
l = mid + 1;
129+
}
130+
}
131+
return l;
132+
}
133+
}
134+
```
135+
136+
```java
137+
class Solution {
138+
public int[] fullBloomFlowers(int[][] flowers, int[] people) {
139+
TreeMap<Integer, Integer> d = new TreeMap<>();
140+
for (int[] f : flowers) {
141+
d.merge(f[0], 1, Integer::sum);
142+
d.merge(f[1] + 1, -1, Integer::sum);
143+
}
144+
int s = 0;
145+
int m = people.length;
146+
Integer[] idx = new Integer[m];
147+
for (int i = 0; i < m; i++) {
148+
idx[i] = i;
149+
}
150+
Arrays.sort(idx, Comparator.comparingInt(i -> people[i]));
151+
int[] ans = new int[m];
152+
for (int i : idx) {
153+
int t = people[i];
154+
while (!d.isEmpty() && d.firstKey() <= t) {
155+
s += d.pollFirstEntry().getValue();
156+
}
157+
ans[i] = s;
158+
}
159+
return ans;
160+
}
161+
}
162+
```
80163

164+
### **C++**
165+
166+
```cpp
167+
class Solution {
168+
public:
169+
vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& people) {
170+
int n = flowers.size();
171+
vector<int> start;
172+
vector<int> end;
173+
for (auto& f : flowers) {
174+
start.push_back(f[0]);
175+
end.push_back(f[1]);
176+
}
177+
sort(start.begin(), start.end());
178+
sort(end.begin(), end.end());
179+
vector<int> ans;
180+
for (auto& p : people) {
181+
auto r = upper_bound(start.begin(), start.end(), p) - start.begin();
182+
auto l = lower_bound(end.begin(), end.end(), p) - end.begin();
183+
ans.push_back(r - l);
184+
}
185+
return ans;
186+
}
187+
};
188+
```
189+
190+
```cpp
191+
class Solution {
192+
public:
193+
vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& people) {
194+
map<int, int> d;
195+
for (auto& f : flowers) {
196+
d[f[0]]++;
197+
d[f[1] + 1]--;
198+
}
199+
int m = people.size();
200+
vector<int> idx(m);
201+
iota(idx.begin(), idx.end(), 0);
202+
sort(idx.begin(), idx.end(), [&](int i, int j) {
203+
return people[i] < people[j];
204+
});
205+
vector<int> ans(m);
206+
int s = 0;
207+
for (int i : idx) {
208+
int t = people[i];
209+
while (!d.empty() && d.begin()->first <= t) {
210+
s += d.begin()->second;
211+
d.erase(d.begin());
212+
}
213+
ans[i] = s;
214+
}
215+
return ans;
216+
}
217+
};
218+
```
219+
220+
### **Go**
221+
222+
```go
223+
func fullBloomFlowers(flowers [][]int, people []int) (ans []int) {
224+
n := len(flowers)
225+
start := make([]int, n)
226+
end := make([]int, n)
227+
for i, f := range flowers {
228+
start[i] = f[0]
229+
end[i] = f[1]
230+
}
231+
sort.Ints(start)
232+
sort.Ints(end)
233+
for _, p := range people {
234+
r := sort.SearchInts(start, p+1)
235+
l := sort.SearchInts(end, p)
236+
ans = append(ans, r-l)
237+
}
238+
return
239+
}
240+
```
241+
242+
```go
243+
func fullBloomFlowers(flowers [][]int, people []int) []int {
244+
d := map[int]int{}
245+
for _, f := range flowers {
246+
d[f[0]]++
247+
d[f[1]+1]--
248+
}
249+
ts := []int{}
250+
for t := range d {
251+
ts = append(ts, t)
252+
}
253+
sort.Ints(ts)
254+
m := len(people)
255+
idx := make([]int, m)
256+
for i := range idx {
257+
idx[i] = i
258+
}
259+
sort.Slice(idx, func(i, j int) bool { return people[idx[i]] < people[idx[j]] })
260+
ans := make([]int, m)
261+
s, i := 0, 0
262+
for _, j := range idx {
263+
t := people[j]
264+
for i < len(ts) && ts[i] <= t {
265+
s += d[ts[i]]
266+
i++
267+
}
268+
ans[j] = s
269+
}
270+
return ans
271+
}
81272
```
82273

83274
### **TypeScript**
84275

85276
```ts
86-
function fullBloomFlowers(flowers: number[][], persons: number[]): number[] {
87-
// 离散差分
88-
let hashMap = new Map();
89-
for (let [start, end] of flowers) {
90-
end++;
91-
hashMap.set(start, (hashMap.get(start) || 0) + 1);
92-
hashMap.set(end, (hashMap.get(end) || 0) - 1);
277+
function fullBloomFlowers(flowers: number[][], people: number[]): number[] {
278+
const n = flowers.length;
279+
const start = new Array(n).fill(0);
280+
const end = new Array(n).fill(0);
281+
for (let i = 0; i < n; ++i) {
282+
start[i] = flowers[i][0];
283+
end[i] = flowers[i][1];
284+
}
285+
start.sort((a, b) => a - b);
286+
end.sort((a, b) => a - b);
287+
const ans: number[] = [];
288+
for (const p of people) {
289+
const r = search(start, p + 1);
290+
const l = search(end, p);
291+
ans.push(r - l);
93292
}
94-
for (let p of persons) {
95-
if (!hashMap.has(p)) {
96-
hashMap.set(p, 0);
293+
return ans;
294+
}
295+
296+
function search(nums: number[], x: number): number {
297+
let l = 0;
298+
let r = nums.length;
299+
while (l < r) {
300+
const mid = (l + r) >> 1;
301+
if (nums[mid] >= x) {
302+
r = mid;
303+
} else {
304+
l = mid + 1;
97305
}
98306
}
99-
let keys = Array.from(hashMap.keys()).sort((a, b) => a - b);
100-
let pre = 0;
101-
for (let k of keys) {
102-
pre += hashMap.get(k);
103-
hashMap.set(k, pre);
307+
return l;
308+
}
309+
```
310+
311+
```ts
312+
function fullBloomFlowers(flowers: number[][], people: number[]): number[] {
313+
const d: Map<number, number> = new Map();
314+
for (const [st, ed] of flowers) {
315+
d.set(st, (d.get(st) || 0) + 1);
316+
d.set(ed + 1, (d.get(ed + 1) || 0) - 1);
317+
}
318+
const ts = [...d.keys()].sort((a, b) => a - b);
319+
let s = 0;
320+
let i = 0;
321+
const m = people.length;
322+
const idx: number[] = [...Array(m)]
323+
.map((_, i) => i)
324+
.sort((a, b) => people[a] - people[b]);
325+
const ans = Array(m).fill(0);
326+
for (const j of idx) {
327+
const t = people[j];
328+
while (i < ts.length && ts[i] <= t) {
329+
s += d.get(ts[i])!;
330+
++i;
331+
}
332+
ans[j] = s;
104333
}
105-
// 离散查询
106-
let ans = persons.map(v => hashMap.get(v));
107334
return ans;
108335
}
109336
```

0 commit comments

Comments
 (0)