Make Unreal REAL.
article thumbnail
Level 1. 바탕화면 정리

 

 

첫 매칭 위치를 반환하는 string.find()와 마지막 매칭 위치를 반환하는 string.rfind()를 이용해 해결했다.

 

#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>

using namespace std;

vector<int> solution(vector<string> wallpaper)
{
    int lux = INT_MAX, luy = INT_MAX;
    int rdx = INT_MIN, rdy = INT_MIN;
    int width = wallpaper.front().size(), height = wallpaper.size();
    int x, pos;

    for (x = 0; x < height; ++x)
        if (wallpaper[x].find('#') != string::npos)
            break;

    lux = x;

    for (x = 0; x < height; ++x)
        if ((pos = wallpaper[x].find('#')) != string::npos)
            luy = min(luy, pos);

    for (x = height - 1; 0 <= x; --x)
        if (wallpaper[x].rfind('#') != string::npos)
            break;

    rdx = x + 1;

    for (x = height - 1; 0 <= x; --x)
        if ((pos = wallpaper[x].rfind('#')) != string::npos)
            rdy = max(rdy, pos + 1);

    return vector<int>{lux, luy, rdx, rdy};
}

 

물론, 다른 사람의 풀이처럼 이중 for문 하나로 끝낼 수도 있지만 나는 모든 원소를 순회하고 싶지 않아서 각각을 나눠 계산했다.

 

#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>

using namespace std;

vector<int> solution(vector<string> wallpaper)
{
    int lux = INT_MAX, luy = INT_MAX;
    int rdx = INT_MIN, rdy = INT_MIN;
    int width = wallpaper.front().size(), height = wallpaper.size();

    for (int x = 0; x < height; ++x)
    {
        for (int y = 0; y < width; ++y)
        {
            if (wallpaper[x][y] == '#')
            {
                lux = min(lux, x);
                luy = min(luy, y);
                rdx = max(rdx, x + 1);
                rdy = max(rdy, y + 1);
            }
        }
    }

    return vector<int>{lux, luy, rdx, rdy};
}
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그