SeangG
def _(): _()
SeangG
전체 방문자
오늘
어제
  • 분류 전체보기 (37)
    • Programming Language (0)
      • Python (0)
      • Web (0)
    • Algorithm (34)
    • Art (0)
      • 3D Modeling (0)
      • Pixel (0)
      • Picture (0)
    • Game (0)
    • Project (3)
      • Problems (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Fast Refresh
  • Leecode
  • BFS
  • Python
  • 매핑 테이블
  • leetcode
  • 문자열
  • Daily Question
  • graph
  • react.js
  • next.js
  • WSL
  • 로마 숫자
  • string
  • spring boot
  • LeedCode
  • Tree
  • github oauth
  • Queue
  • dfs

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
SeangG

def _(): _()

Algorithm

[LeetCode] 1289. Minimum Falling Path Sum II (Daily Question)

2024. 4. 26. 09:55

문제


Given an n x n integer matrix grid, return the minimum sum of a falling path with non-zero shifts.

A falling path with non-zero shifts is a choice of exactly one element from each row of grid such that no two elements chosen in adjacent rows are in the same column.

 

2차원 배열 grid가 주어졌을 때, 맨 위(0행)에서 내려가면서 더했을 때 만들 수 있는 최솟값을 구하는 문제이다.

단, 내려갈 때는 같은 열로 내려갈 수 없다. 

 

Example 1:
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: 13
Explanation: The possible falling paths are: [1,5,9], [1,5,7], [1,6,7], [1,6,8], [2,4,8], [2,4,9], [2,6,7], [2,6,8], [3,4,8], [3,4,9], [3,5,7], [3,5,9] The falling path with the smallest sum is [1,5,7], so the answer is 13.

Example 2:
Input: grid = [[7]]
Output: 7

 

풀이


아이디어

- 현재 행의 제일 작은 값 및 열 번호, 두 번째로 작은 값을 구한다.

- 다음 행의 모든 값에 제일 작은 값을 다 더해주는데, 같은 열 번호는 두 번째로 작은 값을 더해준다.

- 반복

 

 

코드

class Solution:
    def minFallingPathSum(self, grid: List[List[int]]) -> int:
        min1 = (100, -1)
        min2 = (100, -1)

        for i in range(len(grid)):
            min3 = (100*200, -1)
            min4 = (100*200, -1)
            
            for j in range(len(grid[0])):
                # i가 0일때는 최소값을 더해줄 필요X
                if i!= 0:
                    if min1[1] != j:
                        grid[i][j] += min1[0]
                    else:
                        grid[i][j] += min2[0]

                # 행의 최소값
                if min3[0] > grid[i][j]:
                    min4 = min3
                    min3 = (grid[i][j], j)
                # 행의 두 번째로 작은 값
                elif min4[0] > grid[i][j]:
                    min4 = (grid[i][j], j)

            min1 = min3
            min2 = min4

        return min1[0]

 


 

후기


아이디어만 떠올린다면 풀만한 문제이다.

예외사항도 딱히 없어서 Hard문제 치고는 쉬웠다.

 

'Algorithm' 카테고리의 다른 글

[LeetCode] 2997. Minimum Number of Operations to Make Array XOR Equal to K (Daily Question)  (0) 2024.04.29
[LeetCode] 514. Freedom Trail (Daily Question)  (0) 2024.04.27
[LeetCode] 2370. Longest Ideal Subsequence (Daily Question)  (0) 2024.04.25
[LeetCode] 1137. N-th Tribonacci Number (Daily Question)  (0) 2024.04.24
[LeetCode] 310. Minimum Height Trees (Daily Question)  (0) 2024.04.23
    'Algorithm' 카테고리의 다른 글
    • [LeetCode] 2997. Minimum Number of Operations to Make Array XOR Equal to K (Daily Question)
    • [LeetCode] 514. Freedom Trail (Daily Question)
    • [LeetCode] 2370. Longest Ideal Subsequence (Daily Question)
    • [LeetCode] 1137. N-th Tribonacci Number (Daily Question)
    SeangG
    SeangG

    티스토리툴바