최대 1 분 소요

문제 링크

내 풀이


def solution(wallpaper):
    answer = []
    sharp_position = []
    sharp_position2 = []
    lux = 100
    rdx = 0
    for i, points in enumerate(wallpaper):
        if '#' in points:
            sharp_position.append(points.find('#'))
        if '#' in points and i <= lux:
            lux = i      
        if '#' in points and i >= rdx:
            rdx = i
        for i , point in enumerate(points):
            if '#' == point:
                sharp_position2.append(i)
                
    luy = min(sharp_position)
    rdx = rdx + 1
    rdy = max(sharp_position2) + 1
    
    if lux < rdx and luy < rdy:          
        answer.append(lux)  
        answer.append(luy)
        answer.append(rdx)
        answer.append(rdy)
    
    return answer

다른 풀이


def solution(wallpaper):
    a, b = [], []
    for i in range(len(wallpaper)):
        for j in range(len(wallpaper[i])):
            if wallpaper[i][j] == "#":
                a.append(i)
                b.append(j)
    return [min(a), min(b), max(a) + 1, max(b) + 1]

풀이 해석

wallpaper[i][j] 형태를 이용해서 for 문으로 표현함

wallpaper[i][j] == ‘#’ 일 때의 i값과 j 값을 각각 list에 저장해놓은 뒤 최소 최댓값을 이용하여 답을 도출해냄

배울 점

  • 변수 두개로 한번에 list 두개 생성
  • a, b = [] , []
  • 문제에서 주어진 wallpaper[i][j] 꼴 적극 활용

댓글남기기