#include <string>
#include <vector>
#include <map>

using namespace std;

vector<int> solution(int n, vector<string> words) {
    vector<int> answer;
    map<string, int> temp;
    int num = 1, turn = 1;
    char last = words[0][0];

    for (int i = 0; i < words.size(); i++)
    {
        string word = words[i];
        
        if (last != word[0])
        {
            answer.push_back(num);
            answer.push_back(turn);
            break;
        }

        if (temp[word] == 0)
            temp[word] = num;
        else
        {
            answer.push_back(num);
            answer.push_back(turn);
            break;
        }

        num++;
        last = word[word.size() - 1];

        if (num == n + 1)
        {
            num = 1;
            turn++;
        }
    }

    if (answer.empty())
    {
        answer.push_back(0);
        answer.push_back(0);
    }

    return answer;
}

크게 어려울것 없는 구현 문제였다.

 

흔한 좌표 회전 문제이다.

아래, 왼쪽, 왼쪽위 대각선 3개의 경우를 나눠서 풀면 된다.

#include <string>
#include <vector>

using namespace std;

vector<int> solution(int n) {
    vector<int> answer;
    vector<vector<int>> map(n, vector<int>(n, 0));

    int row = -1, col = 0, num = 1;

    for(int i = 0; i < n; i++)
        for (int j = i; j < n; j++)
        {
            if (i % 3 == 0)
                row++;
            else if (i % 3 == 1)
                col++;
            else
            {
                row--;
                col--;
            }

            map[row][col] = num++;
        }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (map[i][j] != 0)
                answer.push_back(map[i][j]);

    return answer;
}

비트연산은 자주 풀었기 때문에 구현에 어려움은 없었다.

홀수 / 짝수 나눠서 계산 하면된다.

#include <vector>

using namespace std;

vector<long long> solution(vector<long long> numbers)
{
    vector<long long> answer;

    for (int i = 0; i < numbers.size(); i++)
    {
        long long temp = 1;

        while ((numbers[i] & temp) > 0)
            temp = temp << 1;
        
        answer.push_back(numbers[i] + temp - (temp >> 1));
    }

    return answer;
}

어려운것 없이 쉬운 완전탐색 문제이다.

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<vector<int>> d;
bool visited[8] = { false };
int out = 0;

void DFS(int cur, int cnt)
{
    for(int i = 0; i < d.size(); i++)
        if (!visited[i] && cur >= d[i][0])
        {
            visited[i] = true;
            DFS(cur - d[i][1], cnt + 1);
            visited[i] = false;
        }

    out = max(out, cnt);
}

int solution(int k, vector<vector<int>> dungeons) {
    int answer = 0;

    d = dungeons;

    DFS(k, 0);

    answer = out;

    return answer;
}

어디선가 풀어봤던 문제라서 쉽게 풀었다.

#define MAX 1000000007

#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = 0;
    vector<int> DP(n + 1, 0);

    DP[1] = 1;
    DP[2] = 2;

    for (int i = 3; i <= n; i++)
        DP[i] = (DP[i - 1] + DP[i - 2]) % MAX;

    answer = DP[n];

    return answer;
}

+ Recent posts