220v
젝무의 개발새발
220v
전체 방문자
오늘
어제
  • 분류 전체보기 (255)
    • AI (35)
      • ML, DL 학습 (30)
      • 논문 리뷰 (4)
      • 실습 및 프로젝트 (1)
    • Algorithm (145)
      • LeetCode (13)
      • 프로그래머스 (35)
      • 백준 (96)
      • 알고리즘, 문법 정리 (1)
    • Mobile, Application (17)
      • Flutter (10)
      • iOS, MacOS (7)
    • BackEnd (7)
      • Flask (1)
      • Node.js (5)
      • Spring, JSP..etc (1)
    • Web - FrontEnd (18)
      • JavaScript, JQuery, HTML, C.. (12)
      • React (6)
    • DataBase (1)
      • MySQL (1)
      • Firebase Firestore (0)
      • Supabase (0)
    • Git (1)
    • 기타 툴 및 오류 해결 (3)
    • 강의 (5)
      • Database (3)
      • 암호학 (2)
      • 알고리즘 (0)
    • 후기와 회고 (2)
    • 블로그 꾸미기 (1)
    • 일상과 이것저것 (20)
      • 맛집 (12)
      • 세상사는일 (4)
      • 도서리뷰 (1)
      • 이런저런 생각들 (잡글) (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • top-down
  • brute-Force
  • Dynamic Programming
  • 구현
  • disjoint set
  • union-find
  • dfs
  • 위상 정렬
  • simulation
  • 백준
  • implementation
  • Greedy
  • IMPLEMENT
  • Backtracking
  • 프로그래머스
  • bitmasking
  • BFS
  • Lis
  • two pointer
  • topological sort
  • Minimum Spanning Tree
  • 다익스트라
  • REACT
  • Priority Queue
  • dp
  • 오블완
  • binary search
  • 티스토리챌린지
  • Prefix Sum
  • Mathematics

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
220v

젝무의 개발새발

Algorithm/LeetCode

[LeetCode/릿코드] 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (220702 daily challenge)

2022. 7. 3. 02:35

접근 - 1

가로, 세로의 잘린 간격을 리스트에 담아 저장.

가로, 세로의 간격 리스트를 이중for문으로 순회하며 곱해서 최대의 넓이를 구해냄.

class Solution:
    def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
        horizontalCuts.sort()
        verticalCuts.sort()
        
        horizonGap = []
        verticalGap = []
        maxCuttedArea = 0

        # horizonGap, verticalGap 리스트에 잘린 간격의 칸 수를 순서대로 삽입.
        horizonGap.append(horizontalCuts[0])
        for num1, num2 in zip(horizontalCuts, horizontalCuts[1:]):
            horizonGap.append(num2 - num1)
        horizonGap.append(h - horizontalCuts[-1])

        verticalGap.append(verticalCuts[0])
        for num1, num2 in zip(verticalCuts, verticalCuts[1:]):
            verticalGap.append(num2 - num1)
        verticalGap.append(w - verticalCuts[-1])

        for ver in verticalGap:
            for hor in horizonGap:
                area = ver * hor
                if area > maxCuttedArea:
                    maxCuttedArea = area

        return (maxCuttedArea % 1000000007) 

시간초과 뜸.

 

접근 - 2

곰곰이 생각하다 깨달았음.

그냥 horizontalGap 과 verticalGap 의 최댓값만 서로 곱해주면 그게 최댓값이잖아?

class Solution:
    def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
        horizontalCuts.sort()
        verticalCuts.sort()
        
        maxHorizonGap = 0
        maxVerticalGap = 0
        maxCuttedArea = 0

        # 가로, 세로 잘린 칸 수의 최댓값을 서로 곱함
        maxHorizonGap = horizontalCuts[0]
        for num1, num2 in zip(horizontalCuts, horizontalCuts[1:]):
            gap = num2 - num1
            if(maxHorizonGap < gap):
                maxHorizonGap = gap
        if(maxHorizonGap < (h - horizontalCuts[-1])):
            maxHorizonGap = h - horizontalCuts[-1]

        maxVerticalGap = verticalCuts[0]
        for num1, num2 in zip(verticalCuts, verticalCuts[1:]):
            gap = num2 - num1
            if(maxVerticalGap < gap):
                maxVerticalGap = gap
        if(maxVerticalGap < (w - verticalCuts[-1])):
            maxVerticalGap = w - verticalCuts[-1]

        return (maxVerticalGap * maxHorizonGap) % 1000000007

 

복잡도

접근 1에서는

  • 시간복잡도 O(n2)
  • 공간복잡도 O(n)

 

접근 2에서는

  • 시간복잡도 O(n)

  • 공간복잡도 O(1)

    • 입력되는 리스트의 크기를 고려한다면 O(n)
    'Algorithm/LeetCode' 카테고리의 다른 글
    • [LeetCode/릿코드] 376. Wiggle Subsequence (220703 daily challenge)
    • [LeetCode/릿코드] 135. Candy (220704 daily challenge)
    • [LeetCode/릿코드] 215. Kth Largest Element in an Array (220622 daily challenge)
    • [LeetCode/릿코드] 383. Ransom Note
    220v
    220v
    DGU CSE 20 / Apple Developer Academy @ POSTECH 2nd Jr.Learner.

    티스토리툴바