deda
Deda의 데이터 디자인
deda
전체 방문자
오늘
어제
  • 분류 전체보기 (121)
    • Python (27)
      • Python 기초 (17)
      • Python 데이터분석 (10)
    • SQL (9)
    • Coding test (54)
      • Python 기초문제 (45)
      • LeetCode (9)
    • BigData (2)
    • ZeroBase (3)
    • UX (0)
    • Business Review (1)
    • 통계 & 수학 (17)
      • 통계학 (14)
      • 수학 (3)
    • 스터디 (6)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 소인수분해
  • 제로베이스
  • pandas
  • 함수
  • 부트캠프
  • 팩토리얼
  • 계산기
  • 등차수열
  • 데이터사이언티스트
  • 미니콘다
  • 코딩
  • 마이데이터
  • SQL
  • 데이터분석가
  • 빅데이터
  • BMI
  • matplotlib
  • 프로그래밍
  • 기초수학
  • 데이터엔지니어
  • 계차수열
  • 최소공배수
  • 데이터분석
  • 모듈
  • 통계
  • 파이썬
  • 릿코드
  • 등비수열
  • 군수열
  • 네카라쿠배

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
deda

Deda의 데이터 디자인

[LeetCode] Max Consecutive Ones II
Coding test/LeetCode

[LeetCode] Max Consecutive Ones II

2022. 5. 20. 16:40

🔍  문제

Given a binary array nums, return the maximum number of consecutive 1's in the array if you can flip at most one 0.

nput: nums = [1,0,1,1,0]
Output: 4
Explanation: Flip the first zero will get the maximum number of consecutive 1s.
After flipping, the maximum number of consecutive 1s is 4.

 

🖥  실행

1) Answer

class Solution(object):
    def findMaxConsecutiveOnes(self, nums):
        # previous and current length of consecutive 1 
        pre, curr, maxlen = -1, 0, 0
        for n in nums:
            if n == 0:
                pre, curr = curr, 0
            else:
                curr += 1
            maxlen = max(maxlen, pre + 1 + curr )
        
        return maxlen

 

'Coding test > LeetCode' 카테고리의 다른 글

[LeetCode] Find All Numbers Disappeared in an Array  (0) 2022.05.23
[LeetCode] Third Maximum Number  (0) 2022.05.20
[LeetCode] Height Checker  (0) 2022.05.20
[LeetCode] Move Zeroes  (0) 2022.05.20
[LeetCode] Replace Elements with Greatest Element on Right Side  (0) 2022.05.20
    'Coding test/LeetCode' 카테고리의 다른 글
    • [LeetCode] Find All Numbers Disappeared in an Array
    • [LeetCode] Third Maximum Number
    • [LeetCode] Height Checker
    • [LeetCode] Move Zeroes
    deda
    deda
    데이터 분석 / 파이썬 / UX / 정량리서치

    티스토리툴바