[Mock] Amazon 7

shsh·2021년 4월 2일
0

Mock

목록 보기
21/93

994. Rotting Oranges

You are given an m x n grid where each cell can have one of three values:

  • 0 representing an empty cell,
  • 1 representing a fresh orange, or
  • 2 representing a rotten orange.

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

My Answer 1: Accepted (Runtime: 60 ms - 24.87% / Memory Usage: 14.2 MB - 68.10%)

class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        orange = 0  # 전체 오렌지 개수
        self.fresh = 0   # fresh 한 오렌지 개수
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j] != 0:
                    orange += 1
                if grid[i][j] == 1:
                    self.fresh += 1
                    
                    
        # 사방을 썩게 만들기
        # grid = 3 => 지금은 신선하지만 1분 뒤엔 썩을 애들
        def make(grid, i, j):
            if i - 1 >= 0 and grid[i-1][j] == 1:
                grid[i-1][j] = 3
                self.fresh -= 1
            if i + 1 < len(grid) and grid[i+1][j] == 1:
                grid[i+1][j] = 3
                self.fresh -= 1
            if j - 1 >= 0 and grid[i][j-1] == 1:
                grid[i][j-1] = 3
                self.fresh -= 1
            if j + 1 < len(grid[i]) and grid[i][j+1] == 1:
                grid[i][j+1] = 3
                self.fresh -= 1
                
        
        cnt = 0
        prev = 0
        while self.fresh:
            prev = self.fresh
            
            for i in range(len(grid)):
                for j in range(len(grid[i])):
                    if grid[i][j] == 2:
                        make(grid, i, j)
                        
            cnt += 1    # 1분 경과
            
            # 찐으로 썩음
            for i in range(len(grid)):
                for j in range(len(grid[i])):
                    if grid[i][j] == 3:
                        grid[i][j] = 2
            
            # 1분이 지나도 이전과 똑같으면 무한루프가 되니까 -1
            if self.fresh == prev:
                return -1
            
            if self.fresh == 0:
                return cnt
        
        return cnt
  1. 전체 오렌지 개수와 fresh 한 오렌지 개수 세주기

  2. fresh 한 오렌지가 남아있을 동안 반복문 돌려서 썩은 부분 주위를 썩게 만들기 => make
    => 이 때, 지금은 신선하지만 1분 뒤엔 썩을 애들로 구분하기 위해서 3 을 넣어줌

  3. 1 분 경과하고 찐으로 썩게 만들기 => 3 -> 2 값 변경

  4. fresh 한 오렌지 개수가 0 이 되면 cnt return
    이전의 개수 값 (prev) 과 동일하면 썩을 가망이 없으므로 -1 return


59. Spiral Matrix II

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

My Answer 1: Accepted (Runtime: 24 ms - 96.76% / Memory Usage: 14.4 MB - 18.18%)

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        # 이놈.. 언젠가 만났던 전적이 있는거 같은디...
        
        # n*n 크기의 행렬 생성
        matrix = []
        for _ in range(n):
            matrix.append([0]*n)
        
        r = 0       # row 시작
        c = 0       # col 시작
        rlen = n-1  # row 끝
        clen = n-1  # col 끝
        num = 1     # 행렬을 채워주는 숫자 (1 ~ n*n)
        
        while num < n*n + 1:
            # 쭉 다 돌고 가운데 숫자만 남았을 때 => 가운데도 마저 채워주고 break
            if c == clen+1 and r == rlen+1:
                matrix[r][c] = num
                break
            
            # 오른쪽 방향 채워주기 => matrix[r][c] ~ matrix[r][clen]
            for i in range(c, clen+1):
                matrix[r][i] = num
                num += 1
            r += 1      # 가장 위쪽 row 가 다 채워졌으므로 r + 1
            
            # 아래 방향 채워주기 => matrix[r][clen] ~ matrix[rlen][clen]
            for i in range(r, rlen+1):
                matrix[i][clen] = num
                num += 1
            clen -= 1   # 가장 오른쪽 col 이 다 채워졌으므로 clen - 1
            
            # 왼쪽 방향 채워주기 => matrix[rlen][clen] ~ matrix[rlen][c]
            for i in range(clen, c-1, -1):
                matrix[rlen][i] = num
                num += 1
            rlen -= 1   # 가장 아래쪽 row 가 다 채워졌으므로 rlen - 1
            
            # 위 방향 채워주기 => matrix[rlen][c] ~ matrix[r][c]
            for i in range(rlen, r-1, -1):
                matrix[i][c] = num
                num += 1
            c += 1      # 가장 왼쪽 col 이 다 채워졌으므로 c + 1
        
        return matrix

차근차근 해봤읍니다

profile
Hello, World!

0개의 댓글

Powered by GraphCDN, the GraphQL CDN