[프로그래머스(Programmers)][Java,Python] (Lv1) 바탕화면 정리

728x90

 

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

1. Java

import java.util.*;
class Solution {
    public int[] solution(String[] wallpaper) {
        int[] answer = new int[4];
        List<int[]> point_list = new ArrayList<>();
        for ( int i = 0; i < wallpaper.length; ++i ) {
            for ( int j = 0; j < wallpaper[i].length(); ++j ) {
                if ( wallpaper[i].charAt(j) == '#') {
                    point_list.add(new int[]{i,j});
                }
            }
        }
        Collections.sort(point_list, ((a1, a2) -> (a1[0] - a2[0])));  // x 좌표 기준 오름차순 정렬
        answer[0] = point_list.get(0)[0];                             // 시작점 x 좌표 최소값
        answer[2] = point_list.get(point_list.size()-1)[0]+1;           // 끝점 x 좌표 최대값
        Collections.sort(point_list, ((a1, a2) -> (a1[1] - a2[1])));  // y 좌표 기준 오름차순 정렬
        answer[1] = point_list.get(0)[1];                             // 시작점 y 좌표 최소값
        answer[3] = point_list.get(point_list.size()-1)[1]+1;           // 끝점 y 좌표 최대값
        return answer;
    }
}

 

2. Python

def solution(wallpaper):
    answer = []
    point_list = []
    for i in range(len(wallpaper)) :
        for j in range(len(wallpaper[i])) :
            if wallpaper[i][j] == '#' :
                point_list.append((i,j))
    point_list.sort(key=lambda x: x[0])
    answer.append(point_list[0][0])
    answer.append(point_list[len(point_list)-1][0]+1)
    point_list.sort(key=lambda x: x[1])
    answer.insert(1,point_list[0][1])
    answer.append(point_list[len(point_list)-1][1]+1)
    return answer

 

반응형