description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
import sys
n = int(input())
a = [int(x) for x in input().split()]
if all(x <= 0 for x in a):
print(0)
import sys
sys.exit(0)
pvs = list(reversed(sorted([x for x in set(a) if x > 0])))
obest = 0
for v in pvs:
cv = 0
poss = False
for x in a:
if x == v:
poss = True
cv += x
obest = max(obest, cv - v)
elif x < v:
cv += x
if poss and cv - v > obest:
obest = cv - v
if cv < 0:
cv = 0
poss = False
else:
cv = 0
print(obest)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
import sys
input = sys.stdin.readline
def main():
n = int(input())
a = list(map(int, input().split()))
ans = 0
for m in range(31):
v = 0
for x in a:
if x > m:
v = 0
else:
v = max(v, 0) + x
ans = max(ans, v - m)
print(ans)
main()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [([c] * b) for i in range(a)]
def list3d(a, b, c, d):
return [[([d] * c) for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
INF = 10**10
MOD = 10**9 + 7
N = INT()
A = LIST()
nums = sorted(set(A), reverse=1)
ans = -INF
for num in nums:
dp0 = [-INF] * (N + 1)
dp1 = [-INF] * (N + 1)
for i in range(N):
if A[i] < num:
dp0[i + 1] = max(dp0[i + 1], dp0[i] + A[i], A[i])
dp1[i + 1] = max(dp1[i + 1], dp1[i] + A[i], A[i])
elif A[i] == num:
dp1[i + 1] = max(dp1[i + 1], dp0[i] + A[i], A[i])
dp1[i + 1] = max(dp1[i + 1], dp1[i] + A[i], A[i])
ans = max(ans, max(dp1) - num)
print(ans)
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
from sys import stdin
input = stdin.buffer.readline
n = int(input())
(*a,) = map(int, input().split())
ans = 0
for x in range(0, 31):
if not x in a:
continue
mx = a[0] if a[0] <= x else 0
sm, mn = 0, 0
for i in range(n):
if a[i] > x:
continue
sm += a[i]
mx = max(mx, sm - mn)
mn = min(mn, sm)
ans = max(ans, mx - x)
print(ans)
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
n = int(input())
A = list(map(int, input().split()))
for a in A:
if a > 0:
break
else:
print(0)
exit()
if n == 1:
print(0)
exit()
ans = -1
INF = 10**18
for M in range(1, 31):
dp = [0] * n
if A[0] > M:
dp[0] = -INF
else:
dp[0] = A[0]
for i in range(1, n):
if A[i] > M:
a = -INF
else:
a = A[i]
dp[i] = max(dp[i - 1] + a, a)
ans = max(ans, max(dp) - M)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
import sys
input = sys.stdin.readline
def prog():
n = int(input())
cards = list(map(int, input().split()))
maxes = [0]
for m in range(1, 31):
ans = 0
value = 0
for card in cards:
if card <= m:
value = max(card, value + card)
else:
value = 0
ans = max(value, ans)
maxes.append(ans - m)
print(max(maxes))
prog()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
n = int(input())
a = list(map(int, input().split())) + [100]
ans = 0
for i in range(-30, 31):
b = []
for x in a:
if x <= i:
b.append(x)
elif b:
m = 0
t = 0
for y in b:
t += y
ans = max(ans, t - m - i)
m = min(m, t)
b = []
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
N = int(input())
As = list(map(int, input().split()))
dp = [-100] * 70
dp[0] = 0
ans = 0
for i, A in enumerate(As):
new_dp = [-100] * 70
new_dp[A] = A
for ma in range(-30, A + 1):
new_dp[A] = max(new_dp[A], dp[ma] + A)
for ma in range(A + 1, 31):
new_dp[ma] = dp[ma] + A
dp = new_dp
ans = max(ans, max(dp[i] - i for i in range(-30, 31)))
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
import sys
ninf = -100000000
[n] = [int(i) for i in sys.stdin.readline().split()]
cards = [int(i) for i in sys.stdin.readline().split()]
dp = [[(0) for i in range(n)] for j in range(61)]
for i in range(61):
if cards[0] <= i - 30:
dp[i][0] = cards[0]
else:
dp[i][0] = ninf
for j in range(1, n):
for i in range(61):
if cards[j] <= i - 30:
dp[i][j] = max(dp[i][j - 1] + cards[j], cards[j])
else:
dp[i][j] = ninf
ans = 0
for j in range(n):
for i in range(61):
ans = max(ans, dp[i][j] - (i - 30))
print(ans)
|
IMPORT ASSIGN VAR NUMBER ASSIGN LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
n = int(input())
a = list(map(int, input().split()))
max_sum = 0
for j in range(1, 31):
if j not in a:
continue
dp = [(0) for i in range(n)]
dp[0] = a[0]
valid_sums = []
prev = False
for i in range(n):
if a[i] <= j:
if prev:
dp[i] = max(0, a[i], a[i] + dp[i - 1])
if a[i] == j:
valid = True
valid_sums.append(dp[i])
elif valid:
valid_sums.append(a[i] + dp[i - 1])
else:
dp[i] = a[i]
prev = True
if a[i] == j:
valid = True
valid_sums.append(dp[i])
else:
valid = False
else:
prev = False
valid = False
max_sum = max(max_sum, max(valid_sums) - j)
print(max_sum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
def solve(n, ar):
ans = 0
for i in range(1, 31):
sum = 0
for j in range(n):
if sum < 0:
sum = 0
sum += ar[j]
if ar[j] > i:
sum = 0
if sum - i > ans:
ans = sum - i
print(ans)
n = int(input())
ar = list(map(int, input().split()))
solve(n, ar)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
x = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(32):
s, max1, s1 = 0, 0, 0
for j in a:
if j <= i:
s += j
max1 = max(max1, s - s1)
s1 = min(s1, s)
else:
s1 = 0
s = 0
res = max(res, max1 - i)
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
def calc(x, mx):
if x > mx:
return float("-inf")
else:
return x
def calcmax(arr):
max_so_far = float("-inf")
currmax = 0
for i in arr:
currmax += i
if currmax > max_so_far:
max_so_far = currmax
if currmax < 0:
currmax = 0
return max_so_far
n = int(input())
arr = [int(c) for c in input().split()]
ans = float("-inf")
for i in range(-30, 31):
a = [calc(x, i) for x in arr]
ans = max(ans, calcmax(a) - i)
print(ans)
|
FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
def do(arr):
res = 0
for i in range(30, -31, -1):
temp = 0
for x in arr:
if x > i:
temp = 0
else:
res = max(res, temp + x - i)
temp = max(0, temp + x)
return res
input()
lst = list(map(int, input().split()))
print(do(lst))
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
n = int(input())
s = list(map(int, input().split()))
ans = 0
inf = -99999999999999999
for mx in range(31):
c = b = 0
for i in s:
if i > mx:
i = inf
c += i
b = min(b, c)
ans = max(ans, c - b - mx)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
n = int(input())
l = list(map(int, input().split()))
curr = 0
best = 0
prevs = [0] * 31
for v in l:
curr += v
if v >= 0:
for i in range(0, v):
prevs[i] = curr
for i in range(v, 31):
best = max(curr - prevs[i] - i, best)
else:
for i in range(31):
prevs[i] = min(prevs[i], curr)
print(best)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
inf = int(1000000.0)
n = int(input())
lis = list(map(int, input().split()))
final = 0
for i in range(30, 0, -1):
s = 0
ans = 0
for j in range(n):
a = lis[j]
if lis[j] > i:
a = -inf
s += a
s = max(0, s)
ans = max(ans, s)
final = max(ans - i, final)
print(final)
|
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
import sys
input = sys.stdin.readline
def build(a, index, beg, end, tree):
if beg == end:
tree[index][0] = a[beg]
tree[index][1] = a[beg]
else:
mid = (beg + end) // 2
build(a, 2 * index + 1, beg, mid, tree)
build(a, 2 * index + 2, mid + 1, end, tree)
tree[index][0] = tree[2 * index + 1][0] + tree[2 * index + 2][0]
tree[index][1] = max(
tree[2 * index + 1][1], tree[2 * index + 1][0] + tree[2 * index + 2][1]
)
def query(index, beg, end, l, r):
result = [-1, -1]
if beg > r or end < l:
return result
if beg >= l and end <= r:
return tree[index]
mid = (beg + end) // 2
if l > mid:
return query(2 * index + 2, mid + 1, end, l, r)
if r <= mid:
return query(2 * index + 1, beg, mid, l, r)
left = query(2 * index + 1, beg, mid, l, r)
right = query(2 * index + 2, mid + 1, end, l, r)
result[0] = left[0] + right[0]
result[1] = max(left[1], left[0] + right[1])
return result
n = int(input())
arr = list(map(int, input().split()))
tree = [[0, 0] for j in range(3 * n)]
build(arr, 0, 0, n - 1, tree)
fw = [(0) for k in range(n)]
bw = [(0) for k in range(n)]
cur_max = bw[n - 1] = arr[n - 1]
i = n - 2
p = [n - 1]
bw[n - 1] = 0
d = dict()
q = 0
d[arr[n - 1]] = n - 1
while i >= 0:
if arr[i] > 0:
r = 1000000
for j in range(arr[i] + 1, 31):
if j in d.keys():
r = min(r, d[j])
if r > p[0]:
bw[i] = max(0, cur_max)
else:
bw[i] = max(0, query(0, 0, n - 1, i + 1, r - 1)[1])
cur_max = max(arr[i], cur_max + arr[i])
if arr[i] == cur_max:
p = [i]
q = 1
else:
p.append(i)
q += 1
d[arr[i]] = i
i += -1
tree = [[0, 0] for j in range(3 * n)]
build(arr[::-1], 0, 0, n - 1, tree)
cur_max, max_so_far = arr[0], arr[0]
p = [0]
q = 0
d = dict()
d[arr[0]] = 0
for i in range(1, n):
if arr[i] > 0:
r = -1
for j in range(arr[i] + 1, 31):
if j in d.keys():
r = max(r, d[j])
if r < p[0]:
fw[i] = max(0, cur_max)
else:
fw[i] = max(0, query(0, 0, n - 1, n - 1 - i + 1, n - 1 - r - 1)[1])
cur_max = max(arr[i], cur_max + arr[i])
if arr[i] == cur_max:
p = [i]
q = 1
else:
p.append(i)
q += 1
d[arr[i]] = i
f = 0
f = max(f, fw[n - 1], bw[0])
j = 1
while j < n - 1:
f = max(f, fw[j] + bw[j])
j += 1
print(f)
|
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
import sys
def I():
return sys.stdin.readline().rstrip()
def f(l):
m = max(l)
if m <= 0:
return 0
s = 0
ms = 0
for x in l:
s += x
ms = max(ms, s)
s = max(s, 0)
ans = max(ms - m, 0)
le = 0
ri = -1
while le < len(l):
le = ri + 1
ri = le
while ri < len(l) and l[ri] < m:
ri += 1
if le < ri - 1:
ans = max(ans, f(l[le:ri]))
return ans
I()
print(f(list(map(int, I().split()))))
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
a = []
n = 0
def tryit(mx):
gs = 0
cs = 0
cmx = -float("inf")
for i in a:
if i > mx:
cs = 0
cmx = -float("inf")
continue
cs = max(i, cs + i)
if cs == i:
cmx = i
cmx = max(cmx, i)
gs = max(gs, cs - cmx)
return gs
n = int(input())
a = list(map(int, input().split()))
cs = 0
gs = 0
mx = -float("inf")
ans = 0
for i in range(-30, 31):
ans = max(ans, tryit(i))
print(ans)
|
ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
def solve(n, d):
maxScore = 0
maxer = float("-inf")
res = 0
for i in range(31):
sub = float("-inf")
curr = float("-inf")
for j in range(len(d)):
now = d[j]
if d[j] > i:
now = float("-inf")
sub = max(sub + now, now)
curr = max(curr, sub)
res = max(curr - i, res)
return res
def main():
n = int(input())
d = input()
d = [int(i) for i in d.split()]
ans = solve(n, d)
print(ans)
main()
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$.
First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the number of cards.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β the values on the cards.
-----Output-----
Print a single integer β the final score of the game.
-----Examples-----
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
-----Note-----
In the first example Alice chooses a segment $[1;5]$ β the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.
In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.
In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
|
INF = 100000000000000000000000000000000000000000000000000
n, a, resp = int(input()), list(map(int, input().split())), 0
for ale in range(31):
corrente, melhor = 0, 0
for i in range(n):
val = -INF if a[i] > ale else a[i]
corrente += val
melhor = min(melhor, corrente)
resp = max(resp, corrente - melhor - ale)
print(resp)
|
ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Frog Gorf is traveling through Swamp kingdom. Unfortunately, after a poor jump, he fell into a well of $n$ meters depth. Now Gorf is on the bottom of the well and has a long way up.
The surface of the well's walls vary in quality: somewhere they are slippery, but somewhere have convenient ledges. In other words, if Gorf is on $x$ meters below ground level, then in one jump he can go up on any integer distance from $0$ to $a_x$ meters inclusive. (Note that Gorf can't jump down, only up).
Unfortunately, Gorf has to take a break after each jump (including jump on $0$ meters). And after jumping up to position $x$ meters below ground level, he'll slip exactly $b_x$ meters down while resting.
Calculate the minimum number of jumps Gorf needs to reach ground level.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 300000$) β the depth of the well.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le i$), where $a_i$ is the maximum height Gorf can jump from $i$ meters below ground level.
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($0 \le b_i \le n - i$), where $b_i$ is the distance Gorf will slip down if he takes a break on $i$ meters below ground level.
-----Output-----
If Gorf can't reach ground level, print $-1$. Otherwise, firstly print integer $k$ β the minimum possible number of jumps.
Then print the sequence $d_1,\,d_2,\,\ldots,\,d_k$ where $d_j$ is the depth Gorf'll reach after the $j$-th jump, but before he'll slip down during the break. Ground level is equal to $0$.
If there are multiple answers, print any of them.
-----Examples-----
Input
3
0 2 2
1 1 0
Output
2
1 0
Input
2
1 1
1 0
Output
-1
Input
10
0 1 2 3 5 5 6 7 8 5
9 8 7 1 5 4 3 2 0 0
Output
3
9 4 0
-----Note-----
In the first example, Gorf is on the bottom of the well and jump to the height $1$ meter below ground level. After that he slip down by meter and stays on height $2$ meters below ground level. Now, from here, he can reach ground level in one jump.
In the second example, Gorf can jump to one meter below ground level, but will slip down back to the bottom of the well. That's why he can't reach ground level.
In the third example, Gorf can reach ground level only from the height $5$ meters below the ground level. And Gorf can reach this height using a series of jumps $10 \Rightarrow 9 \dashrightarrow 9 \Rightarrow 4 \dashrightarrow 5$ where $\Rightarrow$ is the jump and $\dashrightarrow$ is slipping during breaks.
|
import sys
input = sys.stdin.readline
def solve():
n = int(input())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))
ans = []
lo, hi = n, n - A[n]
if hi <= 0:
print(1)
print(0)
return
while lo > 0:
nextStep = hi
idx = hi
for i in range(hi, lo + 1):
j = i + B[i]
if nextStep > j - A[j]:
nextStep = j - A[j]
idx = i
if nextStep == hi > 0:
print(-1)
return
ans.append(idx)
if nextStep <= 0:
ans.append(0)
print(len(ans))
print(*ans)
return
lo, hi = hi - 1, nextStep
return
solve()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN WHILE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR BIN_OP VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR
|
Frog Gorf is traveling through Swamp kingdom. Unfortunately, after a poor jump, he fell into a well of $n$ meters depth. Now Gorf is on the bottom of the well and has a long way up.
The surface of the well's walls vary in quality: somewhere they are slippery, but somewhere have convenient ledges. In other words, if Gorf is on $x$ meters below ground level, then in one jump he can go up on any integer distance from $0$ to $a_x$ meters inclusive. (Note that Gorf can't jump down, only up).
Unfortunately, Gorf has to take a break after each jump (including jump on $0$ meters). And after jumping up to position $x$ meters below ground level, he'll slip exactly $b_x$ meters down while resting.
Calculate the minimum number of jumps Gorf needs to reach ground level.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 300000$) β the depth of the well.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le i$), where $a_i$ is the maximum height Gorf can jump from $i$ meters below ground level.
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($0 \le b_i \le n - i$), where $b_i$ is the distance Gorf will slip down if he takes a break on $i$ meters below ground level.
-----Output-----
If Gorf can't reach ground level, print $-1$. Otherwise, firstly print integer $k$ β the minimum possible number of jumps.
Then print the sequence $d_1,\,d_2,\,\ldots,\,d_k$ where $d_j$ is the depth Gorf'll reach after the $j$-th jump, but before he'll slip down during the break. Ground level is equal to $0$.
If there are multiple answers, print any of them.
-----Examples-----
Input
3
0 2 2
1 1 0
Output
2
1 0
Input
2
1 1
1 0
Output
-1
Input
10
0 1 2 3 5 5 6 7 8 5
9 8 7 1 5 4 3 2 0 0
Output
3
9 4 0
-----Note-----
In the first example, Gorf is on the bottom of the well and jump to the height $1$ meter below ground level. After that he slip down by meter and stays on height $2$ meters below ground level. Now, from here, he can reach ground level in one jump.
In the second example, Gorf can jump to one meter below ground level, but will slip down back to the bottom of the well. That's why he can't reach ground level.
In the third example, Gorf can reach ground level only from the height $5$ meters below the ground level. And Gorf can reach this height using a series of jumps $10 \Rightarrow 9 \dashrightarrow 9 \Rightarrow 4 \dashrightarrow 5$ where $\Rightarrow$ is the jump and $\dashrightarrow$ is slipping during breaks.
|
def task5(n, a, b):
F = [None] * (n + 1)
paths = [[] for i in range(n + 1)]
F[n] = 0
visited = set()
current = [n]
high = n
while True:
new_vertexes = []
for i in current:
for j in range(i - a[i], high + 1):
k = j + b[j]
if k not in visited:
new_vertexes.append(k)
visited.add(k)
if F[k] is None:
paths[k] = i, j
F[k] = F[i] + 1
elif F[i] + 1 < F[k]:
paths[k] = i, j
F[k] = F[i] + 1
high = min(high, i - a[i])
current = new_vertexes
if not current:
break
path = []
if F[0] is not None:
current = paths[0]
while current[0] != n:
path.append(current[1])
current = paths[current[0]]
path.append(current[1])
return -1 if F[0] is None else F[0], path[::-1]
def main():
n = int(input().strip())
arr_a = [0] + list(map(int, input().strip().split()))
arr_b = [0] + list(map(int, input().strip().split()))
result = task5(n, arr_a, arr_b)
print(result[0])
if result[1]:
print(*result[1], sep=" ")
main()
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR LIST IF VAR NUMBER NONE ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER NONE NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
start = 0
sliding_window_counter = collections.Counter()
result = 0
for end in range(len(nums)):
sliding_window_counter[nums[end]] += 1
while sliding_window_counter[0] > 1:
sliding_window_counter[nums[start]] -= 1
start += 1
result = max(result, end - start)
return result
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
se = set(nums)
if 0 not in se:
return len(nums) - 1
elif 1 not in se:
return 0
count = 0
flag = 0
ls = []
for i in nums:
if i == 0:
if flag == 0:
count += 1
else:
flag = 0
ls.append(count)
count = 1
elif flag == 1:
count += 1
else:
flag = 1
ls.append(count)
count = 1
ls.append(count)
ind = 0
ans = 0
while ind < len(ls):
if ls[ind] != 0:
if ls[ind] == 1:
if ind == 0 and ind < len(ls) - 1:
ans = max(ans, ls[ind + 1])
elif ind == len(ls) - 1 and ind > 0:
ans = max(ans, ls[ind - 1])
elif ind < len(ls) - 1 and ind > 0:
ans = max(ans, ls[ind - 1] + ls[ind + 1])
elif ind == 0 and ind < len(ls) - 1:
ans = max(ans, ls[ind + 1])
elif ind == len(ls) - 1 and ind > 0:
ans = max(ans, ls[ind - 1])
elif ind < len(ls) - 1 and ind > 0:
ans = max(ans, max(ls[ind - 1], ls[ind + 1]))
ind += 2
return ans
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER IF NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
i, r = 0, 1
for j in range(len(nums)):
if nums[j] == 0:
r -= 1
if r < 0:
print("yes")
if nums[i] == 0:
r += 1
i += 1
return j - i
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
prevStart = -1
candidates = []
if 0 not in nums:
return len(nums) - 1
for i in range(len(nums)):
if nums[i] == 1:
if prevStart == -1:
prevStart = i
continue
elif prevStart != -1:
candidates.append([prevStart, i - 1])
prevStart = -1
if prevStart != -1:
candidates.append([prevStart, len(nums) - 1])
res = [0]
for i in range(len(candidates)):
if i == len(candidates) - 1:
res.append(candidates[i][1] - candidates[i][0] + 1)
elif candidates[i + 1][0] - candidates[i][1] == 2:
res.append(candidates[i + 1][1] - candidates[i][0])
else:
res.append(candidates[i][1] - candidates[i][0] + 1)
return max(res)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
start, end, maxlen, k = 0, 0, 0, 1
while end < len(nums):
if nums[end] == 0:
k = k - 1
if k < 0:
if nums[start] == 0:
k += 1
start = start + 1
end = end + 1
continue
if k >= 0 or nums[end] == 1:
maxlen = max(maxlen, end - start)
end = end + 1
return maxlen
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
deleted = False
max_, cur_cnt, last_cnt = 0, 0, 0
idx, size = 0, len(nums)
while idx < size and nums[idx] == 0:
idx += 1
for i in range(idx, size):
if nums[i] == 1:
cur_cnt += 1
max_ = max(max_, cur_cnt)
else:
if not deleted:
deleted = True
else:
cur_cnt -= last_cnt
last_cnt = cur_cnt
if size == sum(nums):
return size - 1
return max_
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
n = len(nums)
if sum(nums) == n:
return n - 1
dp = [[(0) for x in range(len(nums))] for y in range(2)]
dp[0][0] = 1 if nums[0] == 1 else 0
for i in range(1, len(nums)):
if nums[i] == 0:
dp[0][i] = 0
dp[1][i] = dp[0][i - 1]
else:
dp[0][i] = dp[0][i - 1] + 1
dp[1][i] = dp[1][i - 1] + 1
return max([i for x in dp for i in x])
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, a: List[int]) -> int:
ones = 0
old_ones = 0
maxlen = 0
n = len(a)
for i in range(n):
if a[i] == 1:
ones += 1
maxlen = max(maxlen, ones + old_ones)
else:
old_ones = ones
ones = 0
return maxlen if ones < n else n - 1
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR BIN_OP VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
if 0 in nums and 1 in nums:
longest = 0
i = nums.index(0)
while True:
if 0 in nums[:i][::-1]:
left_side = nums[:i][::-1].index(0)
else:
left_side = len(nums[:i][::-1])
if 0 in nums[i + 1 :]:
right_side = nums[i + 1 :].index(0)
else:
right_side = len(nums[i + 1 :])
longest = max(left_side + right_side, longest)
try:
i = nums[i + 1 :].index(0) + i + 1
except:
break
return longest
elif sum(nums) == 0:
return 0
elif sum(nums) == len(nums):
return len(nums) - 1
|
CLASS_DEF FUNC_DEF VAR VAR IF NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE NUMBER IF NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
if len(nums) < 1:
return 0
res = 0
logs = []
for x in nums:
if x == 0:
logs.append(res)
res = 0
else:
res += 1
logs.append(res)
if len(logs) == 1:
return max(0, logs[0] - 1)
return max([(logs[i] + logs[i + 1]) for i in range(len(logs) - 1)])
|
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
s = []
i = 0
while i < len(nums):
if nums[i] == 0:
s.append(0)
i += 1
else:
j = i
cnt = 0
while j < len(nums) and nums[j] == 1:
cnt += 1
j += 1
s.append(cnt)
i = j
print(s)
if s[0] == len(nums):
return len(nums) - 1
maxYet = max(s[0], s[len(s) - 1])
for i in range(1, len(s) - 1):
if s[i] == 0:
maxYet = max(maxYet, s[i - 1] + s[i + 1])
else:
maxYet = max(maxYet, s[i])
return maxYet
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
right = [(0) for _ in range(len(nums))]
left = [(0) for _ in range(len(nums))]
for i in range(1, len(nums)):
if nums[i - 1] == 1:
left[i] = 1 + left[i - 1]
res = float("-inf")
for i in range(len(nums) - 2, -1, -1):
if nums[i + 1] == 1:
right[i] = 1 + right[i + 1]
res = max(res, left[i] + right[i])
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
mc = 0
onep = False
for i in range(len(nums)):
if nums[i] == 0:
c = 0
j = i - 1
while j >= 0 and nums[j] != 0:
j -= 1
c += 1
j = i + 1
while j < len(nums) and nums[j] != 0:
j += 1
c += 1
if c > mc:
mc = c
else:
onep = True
if onep and mc == 0:
return len(nums) - 1
elif not onep and mc == 0:
return 0
else:
return mc
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
groups = [[k, len(list(g))] for k, g in itertools.groupby(nums)]
if len(groups) == 1:
return groups[0][1] - 1 if groups[0][0] else 0
ans = 0
for i in range(len(groups)):
k, klen = groups[i]
if k:
ans = max(ans, klen)
elif i not in [0, len(groups) - 1] and klen == 1:
ans = max(ans, groups[i - 1][1] + groups[i + 1][1])
return ans
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
seq = [0]
prev = 0
res = 0
for n in nums:
if n:
seq[-1] += 1
res = max(prev + seq[-1], res)
else:
prev = seq[-1]
seq.append(0)
if len(seq) == 1:
res -= 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
zero = -1
k = 1
maxi = 0
count = 0
i = 0
while i != len(nums):
if nums[i] == 0:
if k == 0:
i = zero + 1
zero = i
maxi = max(count, maxi)
count = 0
k = 1
else:
k -= 1
zero = i
i += 1
else:
count += 1
i += 1
maxi = max(count, maxi)
if maxi == len(nums):
return maxi - 1
else:
return maxi
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
l = 0
r = 0
earliest_one = float("inf")
earliest_zero = float("inf")
zeros = 1
max_len = 0
while r < len(nums):
zeros -= nums[r] == 0
if zeros < 0:
zeros += nums[l] == 0
l += 1
r += 1
return r - l - 1
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
z = [0]
l = []
a = 0
c = 0
for i in range(len(nums)):
if a == 1 and nums[i] == 0:
z.append(c)
c = i - (l[-1] + 1)
a = 1
l.append(i)
elif nums[i] == 0:
a = a + 1
l.append(i)
elif nums[i] == 1:
c = c + 1
z.append(c)
if nums.count(1) == len(nums):
return len(nums) - 1
return max(z)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
new_array = list()
current_segment = 0
for num in nums:
if num == 0:
if current_segment > 0:
new_array.append(current_segment)
current_segment = 0
new_array.append(0)
else:
current_segment += 1
if current_segment > 0:
new_array.append(current_segment)
max_length = 0
for idx in range(len(new_array)):
max_length = max(max_length, new_array[idx])
if idx > 0 and idx < len(new_array) - 1 and new_array[idx] == 0:
if new_array[idx - 1] > 0 and new_array[idx + 1] > 0:
max_length = max(
max_length, new_array[idx - 1] + new_array[idx + 1]
)
if max_length == len(nums):
return max_length - 1
else:
return max_length
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
i, j = 0, 0
ans, cur = 0, 0
while j < len(nums):
cur += nums[j]
while i < j and cur < j - i:
cur -= nums[i]
i += 1
ans = max(ans, cur)
j += 1
return min(ans, len(nums) - 1)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
c = 0
ind = 0
for i in range(len(nums)):
if nums[i] == 0:
j = i + 1
k = 0
while j < len(nums) and nums[j] == 1:
k += 1
j += 1
j = i - 1
while j >= 0 and nums[j] == 1:
k += 1
j -= 1
if k > c:
c = k
ind = i
if ind == 0 and nums[ind] == 1:
ind = len(nums) - 1
c = len(nums) - 1
return c
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
max_length = 0
count = 0
for num in nums:
if num == 1:
count += 1
max_length = max(max_length, count)
else:
count = 0
if max_length == len(nums):
return len(nums) - 1
elif max_length == 0:
return 0
num_left = 0
num_right = 0
for i, num in enumerate(nums):
if num == 1:
num_right += 1
else:
max_length = max(max_length, num_left + num_right)
if i + 1 < len(nums) and nums[i + 1] == 1:
num_left = num_right
num_right = 0
else:
num_left = 0
num_right = 0
max_length = max(max_length, num_left + num_right)
return max_length
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, array: List[int]) -> int:
i = 0
lenght = 0
stack = deque()
zero = False
while i < len(array):
if array[i] == 1:
stack.append(array[i])
i += 1
elif zero == False:
stack.append(array[i])
zero = True
i += 1
else:
temp = len(stack) - 1
if temp > lenght:
lenght = temp
while stack[0] != 0:
stack.popleft()
stack.popleft()
zero = False
if len(stack) > lenght:
lenght = len(stack) - 1
return lenght
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
left = 0
right = 0
zeroes = 1
res = 0
while right < len(nums):
if nums[right] == 0:
zeroes -= 1
while zeroes < 0:
if nums[left] == 0:
zeroes += 1
left += 1
res = max(res, right - left)
right += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
if 1 not in nums:
return 0
if 0 not in nums:
return len(nums) - 1
new = []
i = 0
n = len(nums)
while i < n:
if nums[i] == 1:
cnt = 0
while i < n and nums[i] == 1:
cnt += 1
i += 1
new.append(cnt)
else:
new.append(0)
i += 1
mx = max(new)
for i in range(1, len(new) - 1):
if new[i] == 0:
mx = max(mx, new[i - 1] + new[i + 1])
return mx
|
CLASS_DEF FUNC_DEF VAR VAR IF NUMBER VAR RETURN NUMBER IF NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
left = []
total = 0
for n in nums:
if n == 0:
total = 0
else:
total += n
left.append(total)
right = []
total = 0
for i in range(len(nums) - 1, -1, -1):
if nums[i] == 0:
total = 0
else:
total += nums[i]
right.append(total)
right = right[::-1]
curMax = 0
for i in range(len(nums) - 2):
curMax = max(left[i] + right[i + 2], curMax)
return curMax
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
n = len(nums)
leftones = [0] * n
for i in range(n):
if nums[i] == 0:
leftones[i] = 0
else:
leftones[i] = 1 if i == 0 else leftones[i - 1] + 1
rightones = [0] * n
for i in range(n - 1, -1, -1):
if nums[i] == 0:
rightones[i] = 0
else:
rightones[i] = 1 if i == n - 1 else rightones[i + 1] + 1
res = 0
for i in range(1, n - 1):
res = max(res, leftones[i - 1] + rightones[i + 1])
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
prev, _next, n = (
[(0) for _ in range(len(nums))],
[(0) for _ in range(len(nums))],
len(nums),
)
count = 0
for i in range(n):
num = nums[i]
if i == 0:
if num:
count = 1
elif num:
prev[i] = count
count += 1
else:
prev[i] = count
count = 0
count = 0
for i in range(n - 1, -1, -1):
num = nums[i]
if i == n - 1:
if num:
count = 1
elif num:
_next[i] = count
count += 1
else:
_next[i] = count
count = 0
_max = 0
for i in range(n):
num = nums[i]
_max = max(_max, prev[i] + _next[i])
return _max
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
prev = 0
prev_zero = 0
result = float("-inf")
boolean = False
count = 0
for i in range(len(nums)):
if nums[i] == 0:
boolean = True
count += 1
if count == 2:
count -= 1
prev = prev_zero + 1
prev_zero = i
result = max(result, i - prev)
return result if boolean == True else len(nums) - 1
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums):
n = len(nums)
last_zero = None
last_one = None
cnt = 0
res = 0
for i in range(n):
if nums[i] == 1:
if last_one is None:
last_one = i
cnt += 1
elif last_zero is None:
last_zero = i
else:
last_one = last_zero + 1
last_zero = i
cnt = i - last_one
res = max(res, cnt)
if res == n:
return n - 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
noofones = 0
m = 0
lastoneslen = 0
for i in nums:
if i == 1:
noofones += 1
else:
m = max(m, noofones + lastoneslen)
lastoneslen = noofones
noofones = 0
m = max(m, noofones + lastoneslen)
if m == len(nums):
m -= 1
return m
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
n = len(nums)
if n == 0:
return 0
joined = [(0) for i in range(n)]
left = 0
right = 0
for i in range(n):
joined[i] += left
joined[n - 1 - i] += right
if nums[i] == 1:
left += 1
else:
left = 0
if nums[n - 1 - i] == 1:
right += 1
else:
right = 0
return max(joined)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
if not 0 in nums:
return len(nums) - 1
ans = 0
tot = 0
prev = 0
for n in nums:
if n == 1:
tot += 1
else:
ans = max(tot + prev, ans)
prev = tot
tot = 0
return max(prev + tot, ans)
|
CLASS_DEF FUNC_DEF VAR VAR IF NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
start = end = zeros = ones = maxx = 0
while end < len(nums):
if nums[end] == 1:
ones += 1
elif nums[end] == 0:
zeros += 1
while zeros > 1:
if nums[start] == 0:
zeros -= 1
start += 1
maxx = max(maxx, end - start)
end += 1
return maxx
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
arr = []
count = 0
for i in range(len(nums)):
if nums[i] == 1:
count += 1
elif nums[i] == 0:
arr.append(count)
count = 0
arr.append(count)
if len(arr) == 1:
return arr[0] - 1
maxi = 0
for j in range(1, len(arr)):
maxi = max(maxi, arr[j - 1] + arr[j])
return maxi
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
prev, curr, best = 0, 0, 0
n = len(nums)
for i, x in enumerate(nums):
if x == 0:
best = max(best, prev + curr)
if i == n - 1:
pass
elif nums[i + 1] == 1:
prev = curr
curr = 0
else:
prev = 0
curr = 0
else:
curr += 1
best = max(best, prev + curr)
if best == n:
best -= 1
return best
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
res = []
count = 0
for i, val in enumerate(nums):
if val == 0:
if count > 0:
res.append(count)
count = 0
res.append(0)
elif val == 1:
count += 1
if count > 0:
res.append(count)
lenRes = len(res)
ma = 0
if lenRes > 2:
for i in range(1, lenRes - 1):
ma = max(
max(ma, res[i - 1] + res[i + 1]),
max(res[i - 1], res[i]),
max(res[i + 1], res[i]),
)
return ma
elif lenRes == 1 and res[0] > 0:
return res[0] - 1
elif lenRes == 1 and res[0] == 0:
return ma
elif lenRes == 2:
return max(res[0], res[1])
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR IF VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
longest = 0
left = 0
x = 1
for right in range(len(nums)):
if not nums[right]:
x -= 1
while x < 0:
if not nums[left]:
x += 1
left += 1
longest = max(longest, right - left + 1)
return longest - 1
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
if len(nums) < 2:
return 0
len1_prev = 0
len1 = 0
i = 0
res = 0
zc = 0
while i < len(nums):
if nums[i] == 1:
len1 += 1
else:
res = max(res, len1 + len1_prev)
if zc == 0:
len1_prev = len1
len1 = 0
zc = 1
elif zc == 1:
if len1 > 0:
len1_prev = len1
len1 = 0
zc = 1
else:
len1 = 0
len1_prev = 0
zc = 0
i += 1
res = max(res, len1 + len1_prev)
res = min(len(nums) - 1, res)
return res
|
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
w_start = 0
d = {}
max_len = 0
case = 0
for i in range(0, len(nums)):
c = str(nums[i])
if c not in d:
d[c] = 0
d[c] += 1
if str(0) in d:
while d[str(0)] > 1:
t = str(nums[w_start])
d[t] -= 1
w_start += 1
case = 1
max_len = max(max_len, i - w_start + 1 - case)
case = 0
return max_len - 1
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR NUMBER VAR WHILE VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, A):
if not A:
return 0
n = len(A)
right = 0
cnt = 0
res = 0
for left in range(n):
while right <= n - 1 and (cnt == 0 or A[right] == 1):
cnt += A[right] == 0
right += 1
res = max(res, right - left)
cnt -= A[left] == 0
return res - 1
|
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, xs: List[int]) -> int:
n = len(xs)
if n < 2:
return 0
dp = collections.deque()
best = 0
for i, x in enumerate(xs):
if x:
dp.append(i)
while dp and i - dp[0] - len(dp) + 1 > 1:
dp.popleft()
nholes = i - dp[0] - len(dp) + 1 if dp else 1
this_len = len(dp) - (not dp[0] and not nholes) if dp else 0
best = max(best, this_len)
return best
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR WHILE VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
i = 0
j = 0
sum = 0
ans = 0
for j, val in enumerate(nums):
sum += val
while i < j and sum < j - i:
sum -= nums[i]
i += 1
ans = max(ans, j - i)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
zc, oc = 0, 0
for i, v in enumerate(nums):
if v:
oc += 1
else:
zc += 1
if oc == len(nums):
return oc - 1
elif zc == len(nums):
return 0
elif zc == 1:
return oc
elif oc == 1:
return 1
else:
l = r = 0
for i, v in enumerate(nums):
if v == 1:
l += 1
else:
break
st = i + 1
po = i
maxo, maxi = -1, -1
while st < len(nums):
if nums[st] == 1:
r += 1
st += 1
continue
else:
v = l + r
if maxo < v:
maxo = v
maxi = po
po = st
l = r
r = 0
st += 1
maxo = max(maxo, l + r)
return maxo
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
def get_longest(nums):
arr = [(1 - x) for x in nums]
l, r = 0, 0
count = 0
max_length = 0
while l < len(arr):
if r == len(arr):
step = "l"
elif l == r:
step = "r"
elif count + arr[r] > 1:
step = "l"
else:
step = "r"
if step == "l":
count -= arr[l]
l += 1
else:
count += arr[r]
r += 1
max_length = max(max_length, r - l)
return max(0, max_length - 1)
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
return get_longest(nums)
|
FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR VAR ASSIGN VAR STRING IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
n = len(nums)
prev = 0
cnt = 0
pmax = 0
for i in range(0, n):
if nums[i] == 1:
cnt += 1
else:
prev = cnt
cnt = 0
maxm = prev + cnt
if maxm > pmax:
pmax = maxm
if cnt == n:
return n - 1
else:
return maxm if maxm > pmax else pmax
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
if 0 not in nums:
return len(nums) - 1
arr = [0]
count, flag = 0, 0
for idx, num in enumerate(nums):
if num == 0:
if flag == 1:
arr.append(count)
count = 0
arr.append(0)
flag = 0
elif num == 1:
count += 1
flag = 1
if idx == len(nums) - 1:
arr.append(count)
arr.append(0)
maxSum = 0
for i in range(1, len(arr) - 1):
if arr[i] == 0:
maxSum = max(maxSum, arr[i - 1] + arr[i + 1])
return maxSum
|
CLASS_DEF FUNC_DEF VAR VAR IF NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
i = 0
ints = []
while i < len(nums):
start = i
end = i
while i < len(nums) and nums[i] == 1:
i += 1
end += 1
if start != end:
ints.append((start, end))
i += 1
if len(ints) == 1:
diff = ints[0][1] - ints[0][0]
if diff == len(nums):
return diff - 1
else:
return diff
elif not ints:
return 0
max_ones = 0
for seq in range(len(ints) - 1):
seq1 = ints[seq]
seq2 = ints[seq + 1]
length = None
if seq2[0] - seq1[1] == 1:
length = seq1[1] - seq1[0] + (seq2[1] - seq2[0])
else:
length = max(seq1[1] - seq1[0], seq2[1] - seq2[0])
if length > max_ones:
max_ones = length
return max_ones
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NONE IF BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
n = len(nums)
l, r = [0] * n, [0] * n
for i in range(1, n):
l[i] = l[i - 1] + 1 if nums[i - 1] == 1 else 0
for i in range(n - 2, -1, -1):
r[i] = r[i + 1] + 1 if nums[i + 1] == 1 else 0
ans = max(r[0], l[n - 1])
for i in range(1, n - 1):
ans = max(ans, l[i] + r[i])
return ans
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
res = zeros = 0
zero_i = None
i = 0
for j, v in enumerate(nums):
if v == 0:
zeros += 1
if zeros == 2:
i = zero_i + 1
zeros = 1
zero_i = j
res = max(res, j - i)
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
if all(nums):
return len(nums) - 1
m = pre = l = pl = 0
for i, n in enumerate(nums):
if n == pre == 1:
l += 1
elif n == pre == 0:
pl = 0
elif n == 1 != pre:
l = 1
else:
m = max(m, pl + l)
pl = l
pre = n
return max(m, pl + l) if pre == 1 else m
|
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
max_len = 0
zero_seen = False
for i in range(len(nums)):
length = 0
if nums[i] == 0:
zero_seen = True
l, r = i - 1, i + 1
while l >= 0 and nums[l] == 1:
length += 1
l -= 1
while r < len(nums) and nums[r] == 1:
length += 1
r += 1
max_len = max(max_len, length)
return max_len if zero_seen else len(nums) - 1
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
prev, res, curr = 0, 0, 0
for i in range(len(nums)):
if nums[i] == 1:
curr += 1
else:
if i < len(nums) - 1 and nums[i + 1] != 0:
prev = curr
else:
prev = 0
curr = 0
res = max(res, curr + prev)
if res == len(nums):
res -= 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
if not nums:
return 0
if all(nums):
return len(nums) - 1
n = len(nums)
dp = [([0] * 2) for _ in range(n)]
dp[0][0] = 1 if nums[0] == 1 else 0
dp[0][1] = 0
res = 0
for i in range(1, n):
if nums[i] == 1:
dp[i][0] = dp[i - 1][0] + 1
dp[i][1] = dp[i - 1][1] + 1
else:
dp[i][0] = 0
dp[i][1] = dp[i - 1][0]
res = max(res, dp[i][1], dp[i][0])
return res
|
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
low, high = 0, 0
cn = 0
ans = 0
while low < len(nums) and high < len(nums):
if nums[high] == 0:
if cn < 1:
cn += 1
else:
while nums[low] != 0:
low += 1
low += 1
high += 1
ans = max(ans, high - low)
return max(ans, high - low) - 1
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
var = 0
count = 0
max1 = 0
pos = 0
flag = 0
for i in nums:
if i == 1:
flag = 1
if var == 1:
pos = pos + 1
count = count + 1
max1 = max(count, max1)
elif i == 0 and var == 1:
count = pos
pos = 0
elif flag == 1:
if var == 0:
var = var + 1
else:
count = pos
if max1 == len(nums):
return max1 - 1
return max1
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1'sΒ in the resulting array.
Return 0 if there is no such subarray.
Β
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Β
Constraints:
1 <= nums.length <= 10^5
nums[i]Β is eitherΒ 0Β orΒ 1.
|
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
if sum(nums) >= len(nums) - 1:
return len(nums) - 1
l = 0
while nums[l] == 0:
l += 1
if l == len(nums):
return 0
r = l + 1
sum_total = nums[l]
res = 0
for r in range(r, len(nums)):
sum_total += nums[r]
if sum_total == r - l - 1:
res = max(res, sum_total)
while sum_total < r - l:
sum_total -= nums[l]
l += 1
return max(res, sum_total)
|
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, S, P):
char_count_p = {}
for c in P:
char_count_p[c] = char_count_p.get(c, 0) + 1
char_count_s = {}
left, right, matched, required = 0, 0, 0, len(char_count_p)
min_window_len, min_window_start = float("inf"), None
while right < len(S):
if S[right] in char_count_p:
char_count_s[S[right]] = char_count_s.get(S[right], 0) + 1
if char_count_s[S[right]] == char_count_p[S[right]]:
matched += 1
while matched == required:
if right - left + 1 < min_window_len:
min_window_len = right - left + 1
min_window_start = left
if S[left] in char_count_p:
char_count_s[S[left]] -= 1
if char_count_s[S[left]] < char_count_p[S[left]]:
matched -= 1
left += 1
right += 1
return (
S[min_window_start : min_window_start + min_window_len]
if min_window_start is not None
else "-1"
)
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING NONE WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR NONE VAR VAR BIN_OP VAR VAR STRING
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
count = 0
mp = [0] * 256
for i in range(len(p)):
if mp[ord(p[i])] == 0:
count += 1
mp[ord(p[i])] += 1
res = float("inf")
start = 0
i, j = 0, 0
while j < len(s):
mp[ord(s[j])] -= 1
if mp[ord(s[j])] == 0:
count -= 1
if count == 0:
while count == 0:
if j - i + 1 < res:
res = j - i + 1
start = i
mp[ord(s[i])] += 1
if mp[ord(s[i])] > 0:
count += 1
i += 1
j += 1
if res == float("inf"):
return "-1"
else:
return s[start : start + res]
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN STRING RETURN VAR VAR BIN_OP VAR VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
n = len(s)
D = {}
var = "-1"
for k in p:
if k in D:
D[k] += 1
else:
D[k] = 1
i, j = 0, 0
count = len(D)
mini = 9999
while j < n:
if s[j] in D:
D[s[j]] -= 1
if D[s[j]] == 0:
count -= 1
while count == 0:
if j - i + 1 < mini:
mini = j - i + 1
var = s[i : j + 1]
if s[i] in D:
D[s[i]] += 1
if D[s[i]] > 0:
count += 1
i += 1
j += 1
return var
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
if p == "":
return -1
countT, window = {}, {}
for c in p:
countT[c] = 1 + countT.get(c, 0)
l = 0
have, need = 0, len(countT)
res, resLen = [-1, -1], float("inf")
for r in range(len(s)):
c = s[r]
window[c] = 1 + window.get(c, 0)
if c in countT and window[c] == countT[c]:
have += 1
while have == need:
if r - l + 1 < resLen:
res = [l, r]
resLen = r - l + 1
window[s[l]] -= 1
if s[l] in countT and window[s[l]] < countT[s[l]]:
have -= 1
l += 1
l, r = res
return s[l : r + 1] if resLen != float("inf") else -1
|
CLASS_DEF FUNC_DEF IF VAR STRING RETURN NUMBER ASSIGN VAR VAR DICT DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR VAR BIN_OP VAR NUMBER NUMBER
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
no_of_chars = 256
len1 = len(s)
len2 = len(p)
if len1 < len2:
return -1
hash_p = [0] * no_of_chars
hash_str = [0] * no_of_chars
for i in range(0, len2):
hash_p[ord(p[i])] += 1
start, start_index, min_len = 0, -1, float("inf")
count = 0
for j in range(0, len1):
hash_str[ord(s[j])] += 1
if hash_str[ord(s[j])] <= hash_p[ord(s[j])]:
count += 1
if count == len2:
while (
hash_str[ord(s[start])] > hash_p[ord(s[start])]
or hash_p[ord(s[start])] == 0
):
if hash_str[ord(s[start])] > hash_p[ord(s[start])]:
hash_str[ord(s[start])] -= 1
start += 1
len_window = j - start + 1
if min_len > len_window:
min_len = len_window
start_index = start
if start_index == -1:
return -1
return s[start_index : start_index + min_len]
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR BIN_OP VAR VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
start = 0
end = 0
freq = [0] * 26
n = len(s)
size = 0
st = set(p)
m = len(st)
freq_target = [0] * 26
for i in p:
freq_target[ord(i) - ord("a")] += 1
min_size = 9999999
ans = [-1, -1]
curr = 0
while end < n and start <= end:
if s[end] in st:
index = ord(s[end]) - ord("a")
freq[index] += 1
if freq[index] == freq_target[index]:
size += 1
curr += 1
while size == m:
if curr < min_size:
min_size = curr
ans = [start, end]
if s[start] in st:
index = ord(s[start]) - ord("a")
freq[index] -= 1
if freq[index] < freq_target[index]:
size -= 1
start += 1
curr -= 1
end += 1
if ans == [-1, -1]:
return -1
else:
i, j = ans
return s[i : j + 1]
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR LIST NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
if len(s) < len(p):
return -1
ans = s + "1"
i = 0
j = -1
m = {}
for x in p:
if x in m:
m[x] += 1
else:
m[x] = 1
count = 0
while j < len(s):
if count != len(p):
j += 1
if j == len(s):
break
if s[j] in m:
m[s[j]] -= 1
if m[s[j]] >= 0:
count += 1
else:
if len(ans) > j - i + 1:
ans = s[i : j + 1]
if s[i] in m:
m[s[i]] += 1
if m[s[i]] > 0:
count -= 1
i += 1
if ans == s + "1":
return -1
return ans
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR STRING RETURN NUMBER RETURN VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
Ans = len(s)
Dict = {}
for i in p:
if i in Dict:
Dict[i] = Dict[i] + 1
else:
Dict[i] = 1
count = len(Dict)
i = 0
start = 0
j = 0
while j < len(s):
if s[j] in Dict:
Dict[s[j]] = Dict[s[j]] - 1
else:
Dict[s[j]] = -1
if Dict[s[j]] == 0:
count = count - 1
if count == 0:
while count == 0:
if Ans > j - i + 1:
Ans = j - i + 1
start = i
if s[i] in Dict:
Dict[s[i]] = Dict[s[i]] + 1
if Dict[s[i]] > 0:
count = count + 1
i = i + 1
j = j + 1
if Ans == len(s):
return "-1"
else:
return s[start : start + Ans]
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN STRING RETURN VAR VAR BIN_OP VAR VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, t):
n = len(s)
r = {}
res = s
found = False
for c in list(t):
if c in r:
r[c] += 1
else:
r[c] = 1
curr = ""
for j in range(n):
c = s[j]
curr += c
if c in r:
r[c] -= 1
while len(curr):
all_there = True
for k in r:
if r[k] > 0:
all_there = False
break
if all_there:
if len(res) > len(curr):
res = curr
found = True
else:
break
if curr[0] in r:
r[curr[0]] += 1
curr = curr[1:]
if not found:
return -1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR RETURN NUMBER RETURN VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
d = {}
for i in p:
if i not in d:
d[i] = 1
else:
d[i] += 1
count = len(d)
res = len(s)
i, j, st = 0, 0, 0
while i < len(s):
if s[i] in d:
d[s[i]] -= 1
else:
d[s[i]] = -1
if d[s[i]] == 0:
count -= 1
if count == 0:
while count == 0:
if res > i - j + 1:
res = i - j + 1
st = j
if s[j] in d:
d[s[j]] += 1
if d[s[j]] > 0:
count += 1
j += 1
i += 1
if res == len(s):
return -1
else:
return s[st : st + res]
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR BIN_OP VAR VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
frequencyP = {}
for ch in p:
frequencyP[ch] = frequencyP.get(ch, 0) + 1
start, end = 0, 0
count = len(p)
minLen = float("inf")
minStart = 0
while end < len(s):
if s[end] in frequencyP:
frequencyP[s[end]] -= 1
if frequencyP[s[end]] >= 0:
count -= 1
while count == 0:
if end - start + 1 < minLen:
minLen = end - start + 1
minStart = start
if s[start] in frequencyP:
frequencyP[s[start]] += 1
if frequencyP[s[start]] > 0:
count += 1
start += 1
end += 1
if minLen == float("inf"):
return -1
return s[minStart : minStart + minLen]
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR BIN_OP VAR VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
if len(s) < len(p):
return -1
d1 = {}
d2 = {}
for i in p:
if i in d1:
d1[i] += 1
else:
d1[i] = 1
count = 0
start = 0
l = 9999999999999
ans = ""
for i in range(len(s)):
if s[i] in d2:
d2[s[i]] += 1
else:
d2[s[i]] = 1
if s[i] in p and d2[s[i]] == d1[s[i]]:
count += 1
if count == len(d1):
if l > len(s[start : i + 1]):
ans = s[start : i + 1]
l = len(s[start : i + 1])
while start < i:
if d2[s[start]] != 0:
d2[s[start]] -= 1
if s[start] in d1 and d2[s[start]] == d1[s[start]]:
start += 1
if l > len(s[start : i + 1]):
ans = s[start : i + 1]
l = len(s[start : i + 1])
elif s[start] in d1 and d2[s[start]] < d1[s[start]]:
if l > len(s[start : i + 1]):
ans = s[start : i + 1]
l = len(s[start : i + 1])
start += 1
count -= 1
break
else:
start += 1
if ans == "":
return -1
return ans
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR STRING RETURN NUMBER RETURN VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
need = {}
have = {}
for char in p:
need[char] = need.get(char, 0) + 1
haveNum = 0
needNum = len(need)
n = len(s)
l = r = 0
minLen = float("inf")
res = [-1, -1]
while r < n:
char = s[r]
have[char] = have.get(char, 0) + 1
if char in p and have[char] == need[char]:
haveNum += 1
while haveNum == needNum:
if minLen > r - l + 1:
minLen = r - l + 1
res = [l, r + 1]
have[s[l]] -= 1
if s[l] in p and have[s[l]] < need[s[l]]:
haveNum -= 1
l += 1
r += 1
[l, r] = res
return s[l:r] if [l, r] != [-1, -1] else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN LIST VAR VAR VAR RETURN LIST VAR VAR LIST NUMBER NUMBER VAR VAR VAR NUMBER
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, t):
if t == "":
return ""
countT, window = {}, {}
have = 0
for c in t:
countT[c] = 1 + countT.get(c, 0)
need = len(countT)
res = [-1, -1]
resLen = float("infinity")
l = 0
for r in range(len(s)):
c = s[r]
window[c] = 1 + window.get(c, 0)
if c in countT and window[c] == countT[c]:
have += 1
while have == need:
if r - l - 1 < resLen:
resLen = r - l - 1
res = [l, r]
window[s[l]] -= 1
if s[l] in countT and window[s[l]] < countT[s[l]]:
have -= 1
l += 1
l, r = res
if resLen != float("infinity"):
return s[l : r + 1]
return -1
|
CLASS_DEF FUNC_DEF IF VAR STRING RETURN STRING ASSIGN VAR VAR DICT DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN VAR VAR BIN_OP VAR NUMBER RETURN NUMBER
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
n = len(s)
m = len(p)
if m > n:
return "-1"
sFreq = [0] * 26
pFreq = [0] * 26
for i in range(m):
pFreq[ord(p[i]) - ord("a")] += 1
start = 0
end = 0
count = 0
minLength = float("inf")
startIndex = -1
while end < n:
sFreq[ord(s[end]) - ord("a")] += 1
if sFreq[ord(s[end]) - ord("a")] <= pFreq[ord(s[end]) - ord("a")]:
count += 1
if count == m:
while (
sFreq[ord(s[start]) - ord("a")] > pFreq[ord(s[start]) - ord("a")]
or pFreq[ord(s[start]) - ord("a")] == 0
):
sFreq[ord(s[start]) - ord("a")] -= 1
start += 1
windowLength = end - start + 1
if windowLength < minLength:
minLength = windowLength
startIndex = start
end += 1
if startIndex == -1:
return "-1"
else:
return s[startIndex : startIndex + minLength]
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER RETURN STRING RETURN VAR VAR BIN_OP VAR VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
n = len(s)
if n < len(p):
return -1
m = [0] * 256
start = 0
ans = n + 1
cnt = 0
for i in p:
m[ord(i)] += 1
if m[ord(i)] == 1:
cnt += 1
i = 0
j = 0
while j < n:
m[ord(s[j])] -= 1
if m[ord(s[j])] == 0:
cnt -= 1
while cnt == 0:
if ans > j - i + 1:
ans = j - i + 1
start = i
m[ord(s[i])] += 1
if m[ord(s[i])] > 0:
cnt += 1
i += 1
j += 1
if ans > n:
return -1
return s[start : start + ans]
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR BIN_OP VAR VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, c):
t_map = {}
for char in c:
if char in t_map:
t_map[char] += 1
else:
t_map[char] = 1
left = 0
right = 0
min_len = float("inf")
min_start = 0
char_needed = len(t_map)
window_map = {}
while right < len(s):
char = s[right]
if char in t_map:
if char in window_map:
window_map[char] += 1
else:
window_map[char] = 1
if window_map[char] == t_map[char]:
char_needed -= 1
while char_needed == 0:
if right - left + 1 < min_len:
min_len = right - left + 1
min_start = left
char = s[left]
if char in t_map:
window_map[char] -= 1
if window_map[char] < t_map[char]:
char_needed += 1
left += 1
right += 1
if min_len == float("inf"):
return -1
else:
return s[min_start : min_start + min_len]
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR BIN_OP VAR VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
dict1 = {}
for i in range(len(p)):
dict1[p[i]] = 1 + dict1.get(p[i], 0)
ans = ""
mc = 0
dmc = len(p)
i = -1
j = -1
dict2 = {}
while True:
flag1 = 0
flag2 = 0
while i < len(s) - 1 and mc < dmc:
i += 1
dict2[s[i]] = 1 + dict2.get(s[i], 0)
if dict1.get(s[i], 0) >= dict2.get(s[i], 0):
mc += 1
flag1 = 1
while j < i and mc == dmc:
pst = s[j + 1 : i + 1]
if len(ans) == 0 or len(pst) < len(ans):
ans = pst
j += 1
if dict2.get(s[j], 0) == 1:
dict2.pop(s[j])
else:
dict2[s[j]] -= 1
if dict2.get(s[j], 0) < dict1.get(s[j], 0):
mc -= 1
flag2 = 1
if flag1 == 0 and flag2 == 0:
break
if len(ans) == 0:
return -1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR
|
Given two strings S and P. Find the smallest window in the string S consisting of all the characters(including duplicates) of the string P. Return "-1" in case there is no such window present. In case there are multiple such windows of same length, return the one with the least starting index.
Note : All characters are in Lowercase alphabets.
Example 1:
Input:
S = "timetopractice"
P = "toc"
Output:
toprac
Explanation: "toprac" is the smallest
substring in which "toc" can be found.
Example 2:
Input:
S = "zoomlazapzo"
P = "oza"
Output:
apzo
Explanation: "apzo" is the smallest
substring in which "oza" can be found.
Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as input paramters and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(n) n = len(p) O
Constraints:
1 β€ |S|, |P| β€ 10^{5}
|
class Solution:
def smallestWindow(self, s, p):
if p == "":
return ""
reslen = float("inf")
need = {}
for i in p:
need[i] = 1 + need.get(i, 0)
needCount = len(need)
window = {}
haveCount = 0
res = [-1, -1]
l = 0
for r in range(len(s)):
window[s[r]] = 1 + window.get(s[r], 0)
if s[r] in need and window[s[r]] == need[s[r]]:
haveCount += 1
while haveCount == needCount:
if r - l + 1 < reslen:
res = [l, r]
reslen = r - l + 1
window[s[l]] -= 1
if s[l] in need and window[s[l]] < need[s[l]]:
haveCount -= 1
l += 1
l, r = res
return s[l : r + 1] if reslen != float("inf") else -1
|
CLASS_DEF FUNC_DEF IF VAR STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR VAR BIN_OP VAR NUMBER NUMBER
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.