bit가 눈 앞에서 왔다갔다

Py) 프로그래머스 77485 행렬 테두리 회전하기 본문

Algorithm/Prob

Py) 프로그래머스 77485 행렬 테두리 회전하기

헬린인형 2022. 7. 14. 01:09

이거 왜 골랐냐 진짜

https://school.programmers.co.kr/learn/courses/30/lessons/77485

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1차: 3시간?

2차: 40분

 

0. 입출력

 

1. 김예고리즘

1-1 로직

1) map 생성
2) deq에 rotation 해줘야하는 애들 모아서 append 시켜놓고 left에서 pop한 것을 append
3) 순서가 바뀐 deq을 원래 map에 대입
4) 반복

1-2. 알고리즘  -> 다시 할 예정

'''
rows x columns, 회전들의 목록 queries 주어짐
시계방향으로 회전, 정수 4개로 표현
회전들을 배열에 적용, 위치가 바뀐 숫자 중 작은 숫자 순서대로 배열에 -> return
----
회전하는 숫자들을 어떻게 잡아줘야하지?
배열 4개 생성
2 2 5 4
x1~y1 / y1~y2 / x2~y2 / x1~x2
2~2/ 2~4/ 2~4/ 2~5
'''
from collections import deque
def solution(rows, columns, queries):
    answer = []
    map = []
    deq = deque()

    cnt = 1
    for i in range(rows):
        map.append([])
        for j in range(columns):
            map[i].append(cnt)
            cnt += 1

    min_v=[]
    for q in queries:
        x1, y1, x2, y2 = q

        for k in range(y2-y1):
            deq.append(map[x1-1][y1-1+k])

        for a in range(x2-x1):
            deq.append(map[x1-1+a][y2-1])

        for k in range(y2-y1):
            deq.append(map[x2-1][y2-1-k])

        for a in range(x2-x1):
            deq.append(map[x2-1-a][y1-1])

        print(deq)


        min_v.append(min(deq))
        tmp = deq.pop()
        deq.appendleft(tmp)
        print(deq)

        c = 0
        for k in range(y2-y1):
            map[x1-1][y1-1+k] = deq[k]
            c+=1

        for a in range(x2-x1):
            map[x1-1+a][y2-1] = deq[c+a]
            c+=1

        for k in range(y2-y1):
            map[x2-1][y2-1-k] = deq[c+k]
            c+=1

        for a in range(x2-x1-1):
            map[x2-1-a][y1-1] = deq[c+a]
            c+=1

        print(map)
        print(min_v)

        break


    return answer

print(solution(6,6,[[2,2,5,4],[3,3,6,6],[5,1,6,3]]))

그동안 내가 푼 문제 중 최악인거 같다.

 

2. Solution

2-1. 사용 자료구조 및 알고리즘: x

2-2. 로직

1) 행렬의 각 꼭짓점을 temp1~3에 각각 저장해 min 값을 추리고
2) 행렬의 각 면을 범위를 지정해 시계방향으로 한칸씩 당겨준 뒤 min값을 추리고
3) 이동한 temp의 위치에 맞게 올바른 답을 대입해줌

2-3. 코드   --> 수정예정

def solution(rows, columns, queries):
    answer = []
    # 1:00

    graph = [list(range(1+(i*columns), 1+(i*columns)+columns)) for i in range(rows)]
    # list니까 []로 하는게 맞음. 괄호 생각 잘하자

    for query in queries:
        x1, y1, x2, y2 = query
        temp1 = graph[x1-1][y2-1]
        temp2 = graph[x2-1][y2-1]
        temp3 = graph[x2-1][y1-1]
        ans = min(temp1, temp2, temp3)

        for i in range(x1-1, x2-1):
            graph[i+1][y2-1] = graph[i][y2-1]
            ans = min(graph[i+1][y2-1], ans)
        for i in range(y2-1, y1-1, -1):
            graph[x2-1][i-1] = graph[x2-1][i]
            ans = min(graph[x2-1][i-1], ans)

        for i in range(x2-1, x1-1, -1):
            graph[i][y2-1] = graph[i-1][y2-1]
            ans = min(graph[i][y2-1], ans)
        for i in range(y2-1, y1-1, -1):
            graph[x1 - 1][i] = graph[x1 - 1][i - 1]
            ans = min(graph[x1 - 1][i], ans)

        graph[x1][y2-1]=temp1
        graph[x2-1][y2-2]=temp2
        graph[x2-2][y1-1]=temp3
        answer.append(ans)

    return answer

print(solution(6,6,[[2,2,5,4],[3,3,6,6],[5,1,6,3]]))

 

반응형
Comments