본문 바로가기
백준 & 프로그래머스

프로그래머스.바탕화면 정리.python

by concho 2023. 8. 27.

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

 

프로그래머스

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

programmers.co.kr

wallpaper가 최대 50*50이라 모두 탐색해도 ㄱㅊ

그래도 커지는걸 대비해 4가지의 부분 탐색으로 수정 
4가지로 multiprocessing을 이용한다면 큰 큐모에서 시간단축 가능 예상

def solution(wallpaper):
    answer = []
    col = len(wallpaper)
    row = len(wallpaper[0])
    # 북쪽부터 (#)위치 찾기
    for i in range(col):
        for j in range(row):
            if wallpaper[i][j] == '#':
                answer.append(i)
                break
        if wallpaper[i][j] == '#':
                break
        
    # 서쪽부터 (#)위치 찾기
    for j in range(row):
        for i in range(col):
            if wallpaper[i][j] == '#':
                answer.append(j)
                break
        if wallpaper[i][j] == '#':
                break
    
    # 남쪽부터 (#)위치 찾기
    for i in range(col,-1,-1):
        for j in range(row,-1,-1):
            if wallpaper[i-1][j-1] == '#':
                answer.append(i)
                break
        if wallpaper[i-1][j-1] == '#':
                break
        
    # 동쪽부터 (#)위치 찾기
    for j in range(row,-1,-1):
        for i in range(col,-1,-1):
            if wallpaper[i-1][j-1] == '#':
                answer.append(j)
                break
        if wallpaper[i-1][j-1] == '#':
                break
    
    return answer

멀티 프로세싱 이용:

import multiprocessing

def solution(wallpaper):
    answer = []
    
    def sol0(wallpaper):
        col = len(wallpaper)
        row = len(wallpaper[0])
        # 북쪽부터 (#)위치 찾기
        for i in range(col):
            for j in range(row):
                if wallpaper[i][j] == '#':
                    answer.append(i)
                    break
            if wallpaper[i][j] == '#':
                    break
    def sol1(wallpaper):
        col = len(wallpaper)
        row = len(wallpaper[0])    
        # 서쪽부터 (#)위치 찾기
        for j in range(row):
            for i in range(col):
                if wallpaper[i][j] == '#':
                    answer.append(j)
                    break
            if wallpaper[i][j] == '#':
                    break
    def sol2(wallpaper):
        col = len(wallpaper)
        row = len(wallpaper[0])
        # 남쪽부터 (#)위치 찾기
        for i in range(col,-1,-1):
            for j in range(row,-1,-1):
                if wallpaper[i-1][j-1] == '#':
                    answer.append(i)
                    break
            if wallpaper[i-1][j-1] == '#':
                    break
    def sol3(wallpaper):
        col = len(wallpaper)
        row = len(wallpaper[0])    
        # 동쪽부터 (#)위치 찾기
        for j in range(row,-1,-1):
            for i in range(col,-1,-1):
                if wallpaper[i-1][j-1] == '#':
                    answer.append(j)
                    break
            if wallpaper[i-1][j-1] == '#':
                    break
    
    p0 = multiprocessing.Process(target=sol0(wallpaper))
    p1 = multiprocessing.Process(target=sol1(wallpaper))
    p2 = multiprocessing.Process(target=sol2(wallpaper))
    p3 = multiprocessing.Process(target=sol3(wallpaper))
    p0.start()
    p1.start()
    p2.start()
    p3.start()
    return answer

댓글