Skip to content

Commit 9b2116b

Browse files
committed
feat: add solutions to lc problem: No.1124
No.1124.Longest Well-Performing Interval
1 parent 68fcf00 commit 9b2116b

File tree

6 files changed

+224
-64
lines changed

6 files changed

+224
-64
lines changed

solution/1100-1199/1124.Longest Well-Performing Interval/README.md

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535

3636
<!-- 这里可写通用的实现逻辑 -->
3737

38+
前缀和 + 哈希表。前缀和 s 初始值为 0。遍历 hours 中每一项数据 h:
39+
40+
- 若 h 大于 8,则 s 加 1,否则减 1。
41+
- 若当前 s 大于 0,说明从下标 0 到当前下标的这一段,满足「表现良好的时间段」,`ans = i + 1`
42+
- 若出现了一个新的 s,我们记录到哈希表 seen 中,`seen[s]` 表示 s 第一次出现的位置。
43+
44+
我们想要 s 大于 0,因此要找到 `s - 1` 第一次出现的位置。虽然 `s - x` 同样满足条件,但是它会出现得比 `s - 1` 要晚。因此最大长度是 `i - seen[s - 1]`
45+
3846
<!-- tabs:start -->
3947

4048
### **Python3**
@@ -44,19 +52,18 @@
4452
```python
4553
class Solution:
4654
def longestWPI(self, hours: List[int]) -> int:
47-
pre_sum, res = 0, 0
48-
mp = {}
49-
for i in range(len(hours)):
50-
temp = 1 if hours[i] > 8 else -1
51-
pre_sum += temp
52-
if pre_sum > 0:
53-
res = i + 1
55+
ans = s = 0
56+
seen = {}
57+
for i, h in enumerate(hours):
58+
s += 1 if h > 8 else -1
59+
if s > 0:
60+
ans = i + 1
5461
else:
55-
if pre_sum not in mp:
56-
mp[pre_sum] = i
57-
if (pre_sum - 1) in mp:
58-
res = max(res, i - mp[pre_sum - 1])
59-
return res
62+
if s not in seen:
63+
seen[s] = i
64+
if s - 1 in seen:
65+
ans = max(ans, i - seen[s - 1])
66+
return ans
6067
```
6168

6269
### **Java**
@@ -66,23 +73,78 @@ class Solution:
6673
```java
6774
class Solution {
6875
public int longestWPI(int[] hours) {
69-
int res = 0;
70-
Map<Integer, Integer> map = new HashMap<>();
71-
int s = 0;
76+
int s = 0, ans = 0;
77+
Map<Integer, Integer> seen = new HashMap<>();
7278
for (int i = 0; i < hours.length; ++i) {
7379
s += hours[i] > 8 ? 1 : -1;
7480
if (s > 0) {
75-
res = i + 1;
81+
ans = i + 1;
7682
} else {
77-
int b = map.getOrDefault(s - 1, -1);
78-
if (b != -1) {
79-
res = Math.max(res, i - b);
83+
seen.putIfAbsent(s, i);
84+
if (seen.containsKey(s - 1)) {
85+
ans = Math.max(ans, i - seen.get(s - 1));
8086
}
8187
}
82-
map.putIfAbsent(s, i);
8388
}
84-
return res;
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int longestWPI(vector<int>& hours) {
100+
int s = 0, ans = 0;
101+
unordered_map<int, int> seen;
102+
for (int i = 0; i < hours.size(); ++i)
103+
{
104+
s += hours[i] > 8 ? 1 : -1;
105+
if (s > 0) ans = i + 1;
106+
else
107+
{
108+
if (!seen.count(s)) seen[s] = i;
109+
if (seen.count(s - 1)) ans = max(ans, i - seen[s - 1]);
110+
}
111+
}
112+
return ans;
85113
}
114+
};
115+
```
116+
117+
### **Go**
118+
119+
```go
120+
func longestWPI(hours []int) int {
121+
s, ans := 0, 0
122+
seen := make(map[int]int)
123+
for i, h := range hours {
124+
if h > 8 {
125+
s += 1
126+
} else {
127+
s -= 1
128+
}
129+
if s > 0 {
130+
ans = i + 1
131+
} else {
132+
if _, ok := seen[s]; !ok {
133+
seen[s] = i
134+
}
135+
if j, ok := seen[s-1]; ok {
136+
ans = max(ans, i-j)
137+
}
138+
}
139+
}
140+
return ans
141+
}
142+
143+
func max(a, b int) int {
144+
if a > b {
145+
return a
146+
}
147+
return b
86148
}
87149
```
88150

solution/1100-1199/1124.Longest Well-Performing Interval/README_EN.md

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,100 @@
3838
```python
3939
class Solution:
4040
def longestWPI(self, hours: List[int]) -> int:
41-
pre_sum, res = 0, 0
42-
mp = {}
43-
for i in range(len(hours)):
44-
temp = 1 if hours[i] > 8 else -1
45-
pre_sum += temp
46-
if pre_sum > 0:
47-
res = i + 1
41+
ans = s = 0
42+
seen = {}
43+
for i, h in enumerate(hours):
44+
s += 1 if h > 8 else -1
45+
if s > 0:
46+
ans = i + 1
4847
else:
49-
if pre_sum not in mp:
50-
mp[pre_sum] = i
51-
if (pre_sum - 1) in mp:
52-
res = max(res, i - mp[pre_sum - 1])
53-
return res
48+
if s not in seen:
49+
seen[s] = i
50+
if s - 1 in seen:
51+
ans = max(ans, i - seen[s - 1])
52+
return ans
5453
```
5554

5655
### **Java**
5756

5857
```java
5958
class Solution {
6059
public int longestWPI(int[] hours) {
61-
int res = 0;
62-
Map<Integer, Integer> map = new HashMap<>();
63-
int s = 0;
60+
int s = 0, ans = 0;
61+
Map<Integer, Integer> seen = new HashMap<>();
6462
for (int i = 0; i < hours.length; ++i) {
6563
s += hours[i] > 8 ? 1 : -1;
6664
if (s > 0) {
67-
res = i + 1;
65+
ans = i + 1;
6866
} else {
69-
int b = map.getOrDefault(s - 1, -1);
70-
if (b != -1) {
71-
res = Math.max(res, i - b);
67+
seen.putIfAbsent(s, i);
68+
if (seen.containsKey(s - 1)) {
69+
ans = Math.max(ans, i - seen.get(s - 1));
7270
}
7371
}
74-
map.putIfAbsent(s, i);
7572
}
76-
return res;
73+
return ans;
7774
}
7875
}
7976
```
8077

78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
int longestWPI(vector<int>& hours) {
84+
int s = 0, ans = 0;
85+
unordered_map<int, int> seen;
86+
for (int i = 0; i < hours.size(); ++i)
87+
{
88+
s += hours[i] > 8 ? 1 : -1;
89+
if (s > 0) ans = i + 1;
90+
else
91+
{
92+
if (!seen.count(s)) seen[s] = i;
93+
if (seen.count(s - 1)) ans = max(ans, i - seen[s - 1]);
94+
}
95+
}
96+
return ans;
97+
}
98+
};
99+
```
100+
101+
### **Go**
102+
103+
```go
104+
func longestWPI(hours []int) int {
105+
s, ans := 0, 0
106+
seen := make(map[int]int)
107+
for i, h := range hours {
108+
if h > 8 {
109+
s += 1
110+
} else {
111+
s -= 1
112+
}
113+
if s > 0 {
114+
ans = i + 1
115+
} else {
116+
if _, ok := seen[s]; !ok {
117+
seen[s] = i
118+
}
119+
if j, ok := seen[s-1]; ok {
120+
ans = max(ans, i-j)
121+
}
122+
}
123+
}
124+
return ans
125+
}
126+
127+
func max(a, b int) int {
128+
if a > b {
129+
return a
130+
}
131+
return b
132+
}
133+
```
134+
81135
### **...**
82136

83137
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int longestWPI(vector<int>& hours) {
4+
int s = 0, ans = 0;
5+
unordered_map<int, int> seen;
6+
for (int i = 0; i < hours.size(); ++i)
7+
{
8+
s += hours[i] > 8 ? 1 : -1;
9+
if (s > 0) ans = i + 1;
10+
else
11+
{
12+
if (!seen.count(s)) seen[s] = i;
13+
if (seen.count(s - 1)) ans = max(ans, i - seen[s - 1]);
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func longestWPI(hours []int) int {
2+
s, ans := 0, 0
3+
seen := make(map[int]int)
4+
for i, h := range hours {
5+
if h > 8 {
6+
s += 1
7+
} else {
8+
s -= 1
9+
}
10+
if s > 0 {
11+
ans = i + 1
12+
} else {
13+
if _, ok := seen[s]; !ok {
14+
seen[s] = i
15+
}
16+
if j, ok := seen[s-1]; ok {
17+
ans = max(ans, i-j)
18+
}
19+
}
20+
}
21+
return ans
22+
}
23+
24+
func max(a, b int) int {
25+
if a > b {
26+
return a
27+
}
28+
return b
29+
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
class Solution {
22
public int longestWPI(int[] hours) {
3-
int res = 0;
4-
Map<Integer, Integer> map = new HashMap<>();
5-
int s = 0;
3+
int s = 0, ans = 0;
4+
Map<Integer, Integer> seen = new HashMap<>();
65
for (int i = 0; i < hours.length; ++i) {
76
s += hours[i] > 8 ? 1 : -1;
87
if (s > 0) {
9-
res = i + 1;
8+
ans = i + 1;
109
} else {
11-
int b = map.getOrDefault(s - 1, -1);
12-
if (b != -1) {
13-
res = Math.max(res, i - b);
10+
seen.putIfAbsent(s, i);
11+
if (seen.containsKey(s - 1)) {
12+
ans = Math.max(ans, i - seen.get(s - 1));
1413
}
1514
}
16-
map.putIfAbsent(s, i);
1715
}
18-
return res;
16+
return ans;
1917
}
20-
}
18+
}
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
class Solution:
22
def longestWPI(self, hours: List[int]) -> int:
3-
pre_sum, res = 0, 0
4-
mp = {}
5-
for i in range(len(hours)):
6-
temp = 1 if hours[i] > 8 else -1
7-
pre_sum += temp
8-
if pre_sum > 0:
9-
res = i + 1
3+
ans = s = 0
4+
seen = {}
5+
for i, h in enumerate(hours):
6+
s += 1 if h > 8 else -1
7+
if s > 0:
8+
ans = i + 1
109
else:
11-
if pre_sum not in mp:
12-
mp[pre_sum] = i
13-
if (pre_sum - 1) in mp:
14-
res = max(res, i - mp[pre_sum - 1])
15-
return res
10+
if s not in seen:
11+
seen[s] = i
12+
if s - 1 in seen:
13+
ans = max(ans, i - seen[s - 1])
14+
return ans

0 commit comments

Comments
 (0)