FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/C++/k-empty-slots.cpp at master · Addoile/LeetCode-Solutions · GitHub
Addoile
LeetCode-Solutions
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
C++
/
k-empty-slots.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
22 lines (21 loc) · 720 Bytes
Breadcrumbs
LeetCode-Solutions
/
C++
/
k-empty-slots.cpp
Copy path
File metadata and controls
22 lines (21 loc) · 720 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
Time: O(n)
//
Space: O(n)
class
Solution
{
public:
int
kEmptySlots
(vector<
int
>& flowers,
int
k) {
vector<
int
>
days
(flowers.
size
());
for
(
int
i =
0
; i < flowers.
size
(); ++i) {
days[flowers[i] -
1
] = i;
}
auto
result = numeric_limits<
int
>::
max
();
for
(
int
i =
0
, left =
0
, right = k +
1
; right < days.
size
(); ++i) {
if
(days[i] < days[left] || days[i] <= days[right]) {
if
(i == right) {
result =
min
(result,
max
(days[left], days[right]));
}
left = i, right = k +
1
+ i;
}
}
return
(result == numeric_limits<
int
>::
max
()) ? -
1
: result +
1
;
}
};
Back
|
FazBrowse Home
|
New Git URL