1869. Longer Contiguous Segments of Ones than Zeros
class Solution:
def checkZeroOnes(self, s: str) -> bool:
M, N = 0, 0
cnt = 0
for i in s:
if i == '1':
cnt += 1
else:
M = max(cnt, M)
cnt = 0
M = max(cnt, M)
cnt = 0
for j in s:
if j == '0':
cnt += 1
else:
N = max(cnt, N)
cnt = 0
N = max(cnt, N)
return M > N
for문을 두 번이나 도는 비효율적인 코드
뻔한 결말.
class Solution:
def checkZeroOnes(self, s: str) -> bool:
longestZero, longestOne = 0, 0
zeroCnt, oneCnt = 0, 0
for i in s:
if i == '0':
zeroCnt += 1
oneCnt = 0
else:
oneCnt += 1
zeroCnt = 0
longestZero = max(zeroCnt, longestZero)
longestOne = max(oneCnt, longestOne)
return longestOne > longestZero