Make Unreal REAL.
article thumbnail
Level 0. 캐릭터의 좌표

 

 

#include <iostream>
#include <vector>
#include <map>

using namespace std;

vector<int> solution(vector<string> keyinput, vector<int> board)
{
    map<string, vector<int>> mp = {{"left", {-1, 0}}, {"right", {1, 0}},
                                    {"up", {0, 1}}, {"down", {0, -1}}};
    vector<int> answer = {0, 0};
    vector<int> limit = {board[0] / 2, board[1] / 2};

    for (string key : keyinput)
    {
        answer[0] = max(-limit[0], min(answer[0] + mp[key][0], limit[0]));
        answer[1] = max(-limit[1], min(answer[1] + mp[key][1], limit[1]));
    }

    return answer;
}

 

limit 벡터를 만들지 않고 for 문 내에 board[0] / 2 같은 형식으로 그냥 넣어도 컴파일러 단에서 최적화해 줄 가능성이 크긴 하다.

'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글

Level 0. 평행  (1) 2023.01.24
Level 0. 최빈값 구하기  (0) 2023.01.23
Level 0. 다음에 올 숫자  (0) 2023.01.22
Level 0. 연속된 수의 합  (0) 2023.01.21
Level 0. 종이 자르기  (0) 2023.01.20
profile

Make Unreal REAL.

@diesuki4

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

검색 태그