자바/알고리즘
[프로그래머스] 바탕화면 정리
OverTheHorizon3410
2024. 1. 5. 00:18
package org.example.알고리즘.바탕화면정리;
import java.util.Arrays;
class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(Arrays.toString(solution.solution(new String[]{
".#...", "..#..", "...#."
})));
System.out.println("================================");
System.out.println(Arrays.toString(solution.solution(new String[]{
"..........", ".....#....", "......##..", "...##.....", "....#....."
})));
System.out.println("================================");
}
public int[] solution(String[] wallpaper) {
int[] answer = {};
int minX = Integer.MAX_VALUE;
int maxX = 0;
int minY = Integer.MAX_VALUE;
int maxY = 0;
for (int x = 0; x < wallpaper.length; x++) {
for (int y = 0; y < wallpaper[x].length(); y++) {
char c = wallpaper[x].charAt(y);
if (c == '#') {
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y);
}
}
}
System.out.println("minX = " + minX);
System.out.println("maxX = " + maxX);
System.out.println("minY = " + minY);
System.out.println("maxY = " + maxY);
answer = new int[]{minX, minY, maxX + 1, maxY + 1};
return answer;
}
}
- 최소 X Y 최대 X Y 값을 구하기만 하면 되는 간단한 문제이다.
- 최대 X Y 의 값에서 +1 처리를 해주어야하는데,
Uploaded by N2T