문제 풀기/C#

70. 바탕화면 정리

kagan-draca 2025. 2. 19. 18:58

 

기본틀 :

using System;

public class Solution {
    public int[] solution(string[] wallpaper) {
        int[] answer = new int[] {};
        return answer;
    }
}

 

시작 위치의 가로,세로

 

마지막 위치의 가로, 세로

 

변수를 만들어주고 int 범위에서

 

가장 큰 값이나 작은 값으로 값을 초기화 해줬다.

 

wallpaper를 반복문으로 순회하면서

 

가장 처음 나오는 "#"과 가장 마지막에 나오는 "#"을

 

IndexOf()와 LastIndexOf()를 이용해 구하고

 

Math.Min()이나 Math.Max()으로 시작 위치와 끝 위치 

 

가로, 세로를 변경해줬다.

 

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(string[] wallpaper) 
    {
        int startHeight = int.MaxValue, startWidth = int.MaxValue;
        int endHeight = int.MinValue, endWidth = int.MinValue;
        
        for(int i = 0; i < wallpaper.Length; i++)
        {
            string temp = wallpaper[i];
            
            if(!temp.Contains("#")) continue;
            
            int startIndex = temp.IndexOf("#");
            
            if(startIndex != -1)
            {
                startHeight = Math.Min(i, startHeight);
                startWidth = Math.Min(startIndex, startWidth);
            }
            
            int endIndex = temp.LastIndexOf("#");
            
            if(endIndex != -1)
            {
                endHeight = Math.Max(i + 1, endHeight);
                endWidth = Math.Max(endIndex + 1, endWidth);
            }
        }
        
        return new int[] {startHeight, startWidth, endHeight, endWidth};
    }
}