File tree 3 files changed +85
-4
lines changed
solution/1100-1199/1124.Longest Well-Performing Interval
3 files changed +85
-4
lines changed Original file line number Diff line number Diff line change 43
43
<!-- 这里可写当前语言的特殊实现逻辑 -->
44
44
45
45
``` python
46
-
46
+ class Solution :
47
+ def longestWPI (self , hours : List[int ]) -> int :
48
+ pre_sum, res = 0 , 0
49
+ mp = {}
50
+ for i in range (len (hours)):
51
+ temp = 1 if hours[i] > 8 else - 1
52
+ pre_sum += temp
53
+ if pre_sum > 0 :
54
+ res = i + 1
55
+ else :
56
+ if pre_sum not in mp:
57
+ mp[pre_sum] = i
58
+ if (pre_sum - 1 ) in mp:
59
+ res = max (res, i - mp[pre_sum - 1 ])
60
+ return res
47
61
```
48
62
49
63
### ** Java**
50
64
51
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
52
66
53
67
``` java
54
-
68
+ class Solution {
69
+ public int longestWPI (int [] hours ) {
70
+ int res = 0 ;
71
+ Map<Integer , Integer > map = new HashMap<> ();
72
+ int s = 0 ;
73
+ for (int i = 0 ; i < hours. length; ++ i) {
74
+ s += hours[i] > 8 ? 1 : - 1 ;
75
+ if (s > 0 ) {
76
+ res = i + 1 ;
77
+ } else {
78
+ int b = map. getOrDefault(s - 1 , - 1 );
79
+ if (b != - 1 ) {
80
+ res = Math . max(res, i - b);
81
+ }
82
+ }
83
+ map. putIfAbsent(s, i);
84
+ }
85
+ return res;
86
+ }
87
+ }
55
88
```
56
89
57
90
### ** ...**
Original file line number Diff line number Diff line change 37
37
### ** Python3**
38
38
39
39
``` python
40
-
40
+ class Solution :
41
+ def longestWPI (self , hours : List[int ]) -> int :
42
+ pre_sum, res = 0 , 0
43
+ mp = {}
44
+ for i in range (len (hours)):
45
+ temp = 1 if hours[i] > 8 else - 1
46
+ pre_sum += temp
47
+ if pre_sum > 0 :
48
+ res = i + 1
49
+ else :
50
+ if pre_sum not in mp:
51
+ mp[pre_sum] = i
52
+ if (pre_sum - 1 ) in mp:
53
+ res = max (res, i - mp[pre_sum - 1 ])
54
+ return res
41
55
```
42
56
43
57
### ** Java**
44
58
45
59
``` java
46
-
60
+ class Solution {
61
+ public int longestWPI (int [] hours ) {
62
+ int res = 0 ;
63
+ Map<Integer , Integer > map = new HashMap<> ();
64
+ int s = 0 ;
65
+ for (int i = 0 ; i < hours. length; ++ i) {
66
+ s += hours[i] > 8 ? 1 : - 1 ;
67
+ if (s > 0 ) {
68
+ res = i + 1 ;
69
+ } else {
70
+ int b = map. getOrDefault(s - 1 , - 1 );
71
+ if (b != - 1 ) {
72
+ res = Math . max(res, i - b);
73
+ }
74
+ }
75
+ map. putIfAbsent(s, i);
76
+ }
77
+ return res;
78
+ }
79
+ }
47
80
```
48
81
49
82
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ 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
10
+ 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
You can’t perform that action at this time.
0 commit comments