Skip to content

Commit 93d11f4

Browse files
committed
feat: add solutions to lc problem: No.1124
No.1124.Longest Well-Performing Interval
1 parent db9832c commit 93d11f4

File tree

7 files changed

+116
-113
lines changed

7 files changed

+116
-113
lines changed

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

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,19 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46-
前缀和 + 哈希表。前缀和 s 初始值为 0。遍历 hours 中每一项数据 h:
46+
**方法一:前缀和 + 哈希表**
4747

48-
- 若 h 大于 8,则 s 加 1,否则减 1。
49-
- 若当前 s 大于 0,说明从下标 0 到当前下标的这一段,满足「表现良好的时间段」,`ans = i + 1`
50-
- 若出现了一个新的 s,我们记录到哈希表 seen 中,`seen[s]` 表示 s 第一次出现的位置。
48+
我们可以利用前缀和的思想,维护一个变量 $s$,表示从下标 $0$ 到当前下标的这一段,「劳累的天数」与「不劳累的天数」的差值。如果 $s$ 大于 $0$,说明从下标 $0$ 到当前下标的这一段,满足「表现良好的时间段」。另外,用哈希表 $pos$ 记录每个 $s$ 第一次出现的下标。
5149

52-
我们想要 s 大于 0,因此要找到 `s - 1` 第一次出现的位置。虽然 `s - x` 同样满足条件,但是它会出现得比 `s - 1` 要晚。因此最大长度是 `i - seen[s - 1]`
50+
接下来,我们遍历数组 `hours`,对于每个下标 $i$:
51+
52+
- 如果 $hours[i] \gt 8$,我们就让 $s$ 加 $1$,否则减 $1$。
53+
- 如果 $s$ 大于 $0$,说明从下标 $0$ 到当前下标的这一段,满足「表现良好的时间段」,我们更新结果 $ans = i + 1$。否则,如果 $s - 1$ 在哈希表 $pos$ 中,记 $j = pos[s - 1]$,说明从下标 $j + 1$ 到当前下标 $i$ 的这一段,满足「表现良好的时间段」,我们更新结果 $ans = max(ans, i - j)$。
54+
- 然后,如果 $s$ 不在哈希表 $pos$ 中,我们就记录 $pos[s] = i$。
55+
56+
遍历结束后,返回答案即可。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `hours` 的长度。
5359

5460
<!-- tabs:start -->
5561

@@ -61,16 +67,15 @@
6167
class Solution:
6268
def longestWPI(self, hours: List[int]) -> int:
6369
ans = s = 0
64-
seen = {}
65-
for i, h in enumerate(hours):
66-
s += 1 if h > 8 else -1
70+
pos = {}
71+
for i, x in enumerate(hours):
72+
s += 1 if x > 8 else -1
6773
if s > 0:
6874
ans = i + 1
69-
else:
70-
if s not in seen:
71-
seen[s] = i
72-
if s - 1 in seen:
73-
ans = max(ans, i - seen[s - 1])
75+
elif s - 1 in pos:
76+
ans = max(ans, i - pos[s - 1])
77+
if s not in pos:
78+
pos[s] = i
7479
return ans
7580
```
7681

@@ -81,18 +86,16 @@ class Solution:
8186
```java
8287
class Solution {
8388
public int longestWPI(int[] hours) {
84-
int s = 0, ans = 0;
85-
Map<Integer, Integer> seen = new HashMap<>();
89+
int ans = 0, s = 0;
90+
Map<Integer, Integer> pos = new HashMap<>();
8691
for (int i = 0; i < hours.length; ++i) {
8792
s += hours[i] > 8 ? 1 : -1;
8893
if (s > 0) {
8994
ans = i + 1;
90-
} else {
91-
seen.putIfAbsent(s, i);
92-
if (seen.containsKey(s - 1)) {
93-
ans = Math.max(ans, i - seen.get(s - 1));
94-
}
95+
} else if (pos.containsKey(s - 1)) {
96+
ans = Math.max(ans, i - pos.get(s - 1));
9597
}
98+
pos.putIfAbsent(s, i);
9699
}
97100
return ans;
98101
}
@@ -105,15 +108,17 @@ class Solution {
105108
class Solution {
106109
public:
107110
int longestWPI(vector<int>& hours) {
108-
int s = 0, ans = 0;
109-
unordered_map<int, int> seen;
111+
int ans = 0, s = 0;
112+
unordered_map<int, int> pos;
110113
for (int i = 0; i < hours.size(); ++i) {
111114
s += hours[i] > 8 ? 1 : -1;
112-
if (s > 0)
115+
if (s > 0) {
113116
ans = i + 1;
114-
else {
115-
if (!seen.count(s)) seen[s] = i;
116-
if (seen.count(s - 1)) ans = max(ans, i - seen[s - 1]);
117+
} else if (pos.count(s - 1)) {
118+
ans = max(ans, i - pos[s - 1]);
119+
}
120+
if (!pos.count(s)) {
121+
pos[s] = i;
117122
}
118123
}
119124
return ans;
@@ -124,27 +129,25 @@ public:
124129
### **Go**
125130
126131
```go
127-
func longestWPI(hours []int) int {
128-
s, ans := 0, 0
129-
seen := make(map[int]int)
130-
for i, h := range hours {
131-
if h > 8 {
132-
s += 1
132+
func longestWPI(hours []int) (ans int) {
133+
s := 0
134+
pos := map[int]int{}
135+
for i, x := range hours {
136+
if x > 8 {
137+
s++
133138
} else {
134-
s -= 1
139+
s--
135140
}
136141
if s > 0 {
137142
ans = i + 1
138-
} else {
139-
if _, ok := seen[s]; !ok {
140-
seen[s] = i
141-
}
142-
if j, ok := seen[s-1]; ok {
143-
ans = max(ans, i-j)
144-
}
143+
} else if j, ok := pos[s-1]; ok {
144+
ans = max(ans, i-j)
145+
}
146+
if _, ok := pos[s]; !ok {
147+
pos[s] = i
145148
}
146149
}
147-
return ans
150+
return
148151
}
149152
150153
func max(a, b int) int {

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

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,15 @@
4646
class Solution:
4747
def longestWPI(self, hours: List[int]) -> int:
4848
ans = s = 0
49-
seen = {}
50-
for i, h in enumerate(hours):
51-
s += 1 if h > 8 else -1
49+
pos = {}
50+
for i, x in enumerate(hours):
51+
s += 1 if x > 8 else -1
5252
if s > 0:
5353
ans = i + 1
54-
else:
55-
if s not in seen:
56-
seen[s] = i
57-
if s - 1 in seen:
58-
ans = max(ans, i - seen[s - 1])
54+
elif s - 1 in pos:
55+
ans = max(ans, i - pos[s - 1])
56+
if s not in pos:
57+
pos[s] = i
5958
return ans
6059
```
6160

@@ -64,18 +63,16 @@ class Solution:
6463
```java
6564
class Solution {
6665
public int longestWPI(int[] hours) {
67-
int s = 0, ans = 0;
68-
Map<Integer, Integer> seen = new HashMap<>();
66+
int ans = 0, s = 0;
67+
Map<Integer, Integer> pos = new HashMap<>();
6968
for (int i = 0; i < hours.length; ++i) {
7069
s += hours[i] > 8 ? 1 : -1;
7170
if (s > 0) {
7271
ans = i + 1;
73-
} else {
74-
seen.putIfAbsent(s, i);
75-
if (seen.containsKey(s - 1)) {
76-
ans = Math.max(ans, i - seen.get(s - 1));
77-
}
72+
} else if (pos.containsKey(s - 1)) {
73+
ans = Math.max(ans, i - pos.get(s - 1));
7874
}
75+
pos.putIfAbsent(s, i);
7976
}
8077
return ans;
8178
}
@@ -88,15 +85,17 @@ class Solution {
8885
class Solution {
8986
public:
9087
int longestWPI(vector<int>& hours) {
91-
int s = 0, ans = 0;
92-
unordered_map<int, int> seen;
88+
int ans = 0, s = 0;
89+
unordered_map<int, int> pos;
9390
for (int i = 0; i < hours.size(); ++i) {
9491
s += hours[i] > 8 ? 1 : -1;
95-
if (s > 0)
92+
if (s > 0) {
9693
ans = i + 1;
97-
else {
98-
if (!seen.count(s)) seen[s] = i;
99-
if (seen.count(s - 1)) ans = max(ans, i - seen[s - 1]);
94+
} else if (pos.count(s - 1)) {
95+
ans = max(ans, i - pos[s - 1]);
96+
}
97+
if (!pos.count(s)) {
98+
pos[s] = i;
10099
}
101100
}
102101
return ans;
@@ -107,27 +106,25 @@ public:
107106
### **Go**
108107
109108
```go
110-
func longestWPI(hours []int) int {
111-
s, ans := 0, 0
112-
seen := make(map[int]int)
113-
for i, h := range hours {
114-
if h > 8 {
115-
s += 1
109+
func longestWPI(hours []int) (ans int) {
110+
s := 0
111+
pos := map[int]int{}
112+
for i, x := range hours {
113+
if x > 8 {
114+
s++
116115
} else {
117-
s -= 1
116+
s--
118117
}
119118
if s > 0 {
120119
ans = i + 1
121-
} else {
122-
if _, ok := seen[s]; !ok {
123-
seen[s] = i
124-
}
125-
if j, ok := seen[s-1]; ok {
126-
ans = max(ans, i-j)
127-
}
120+
} else if j, ok := pos[s-1]; ok {
121+
ans = max(ans, i-j)
122+
}
123+
if _, ok := pos[s]; !ok {
124+
pos[s] = i
128125
}
129126
}
130-
return ans
127+
return
131128
}
132129
133130
func max(a, b int) int {

solution/1100-1199/1124.Longest Well-Performing Interval/Solution.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
class Solution {
22
public:
33
int longestWPI(vector<int>& hours) {
4-
int s = 0, ans = 0;
5-
unordered_map<int, int> seen;
4+
int ans = 0, s = 0;
5+
unordered_map<int, int> pos;
66
for (int i = 0; i < hours.size(); ++i) {
77
s += hours[i] > 8 ? 1 : -1;
8-
if (s > 0)
8+
if (s > 0) {
99
ans = i + 1;
10-
else {
11-
if (!seen.count(s)) seen[s] = i;
12-
if (seen.count(s - 1)) ans = max(ans, i - seen[s - 1]);
10+
} else if (pos.count(s - 1)) {
11+
ans = max(ans, i - pos[s - 1]);
12+
}
13+
if (!pos.count(s)) {
14+
pos[s] = i;
1315
}
1416
}
1517
return ans;

solution/1100-1199/1124.Longest Well-Performing Interval/Solution.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
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
1+
func longestWPI(hours []int) (ans int) {
2+
s := 0
3+
pos := map[int]int{}
4+
for i, x := range hours {
5+
if x > 8 {
6+
s++
77
} else {
8-
s -= 1
8+
s--
99
}
1010
if s > 0 {
1111
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-
}
12+
} else if j, ok := pos[s-1]; ok {
13+
ans = max(ans, i-j)
14+
}
15+
if _, ok := pos[s]; !ok {
16+
pos[s] = i
1917
}
2018
}
21-
return ans
19+
return
2220
}
2321

2422
func max(a, b int) int {

solution/1100-1199/1124.Longest Well-Performing Interval/Solution.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
class Solution {
22
public int longestWPI(int[] hours) {
3-
int s = 0, ans = 0;
4-
Map<Integer, Integer> seen = new HashMap<>();
3+
int ans = 0, s = 0;
4+
Map<Integer, Integer> pos = new HashMap<>();
55
for (int i = 0; i < hours.length; ++i) {
66
s += hours[i] > 8 ? 1 : -1;
77
if (s > 0) {
88
ans = i + 1;
9-
} else {
10-
seen.putIfAbsent(s, i);
11-
if (seen.containsKey(s - 1)) {
12-
ans = Math.max(ans, i - seen.get(s - 1));
13-
}
9+
} else if (pos.containsKey(s - 1)) {
10+
ans = Math.max(ans, i - pos.get(s - 1));
1411
}
12+
pos.putIfAbsent(s, i);
1513
}
1614
return ans;
1715
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
class Solution:
22
def longestWPI(self, hours: List[int]) -> int:
33
ans = s = 0
4-
seen = {}
5-
for i, h in enumerate(hours):
6-
s += 1 if h > 8 else -1
4+
pos = {}
5+
for i, x in enumerate(hours):
6+
s += 1 if x > 8 else -1
77
if s > 0:
88
ans = i + 1
9-
else:
10-
if s not in seen:
11-
seen[s] = i
12-
if s - 1 in seen:
13-
ans = max(ans, i - seen[s - 1])
9+
elif s - 1 in pos:
10+
ans = max(ans, i - pos[s - 1])
11+
if s not in pos:
12+
pos[s] = i
1413
return ans

solution/2500-2599/2562.Find the Array Concatenation Value/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ nums 只有一个元素,所以我们选中 13 并将其加到串联值上,
7575

7676
<!-- 这里可写通用的实现逻辑 -->
7777

78+
**方法一:模拟**
79+
80+
从数组两端开始,每次取出一个元素,将其与另一个元素拼接,然后将拼接后的结果加到答案中。重复这个过程,直到数组为空。
81+
82+
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n \times \log M)$。其中 $n$ 和 $M$ 分别是数组的长度和数组中的最大值。
83+
7884
<!-- tabs:start -->
7985

8086
### **Python3**

0 commit comments

Comments
 (0)