본문으로 바로가기

카카오프렌즈 컬러링북 ] : < LV2 / 4129명 >

더보기
#include <vector>
#include <map>
#include <algorithm> // min, max

using namespace std;

// global variables
static int index;
static map<int, int> areas;

void CheckArea(int x, int y, int m, int n, int color, vector<vector<int>>& pic)
{
    int val = pic[y][x];
    if(val == 0 || val != color)
        return;
    //
    ++areas[index];
    // set checked
    pic[y][x] = 0;
    // Recursive
    CheckArea(x, min(max(0, y-1), m-1), m, n, color, pic); // U
    CheckArea(x, min(max(0, y+1), m-1), m, n, color, pic); // D
    CheckArea(min(max(0, x-1), n-1), y, m, n, color, pic); // L
    CheckArea(min(max(0, x+1), n-1), y, m, n, color, pic); // R
}

vector<int> solution(int m, int n, vector<vector<int>> picture)
{
    // initialize global variables
    index = 0;
    areas.clear();
    //
    int number_of_area = 0;
    int max_size_of_one_area = 0;
    //
    int color = 0;
    for(int y = 0; y < m; ++y)
    {
        for(int x = 0; x < n; ++x)
        {
            color = picture[y][x];
            if(color != 0)
            {
                ++index;
                CheckArea(x, y, m, n, color, picture);
            }
        }
    }
    //
    number_of_area = areas.size();
    //
    auto iter = areas.begin(), end = areas.end();
    for(; iter != end; ++iter)
    {
        if(iter->second > max_size_of_one_area)
            max_size_of_one_area = iter->second;
    }
    //
    vector<int> answer(2);
    answer[0] = number_of_area;
    answer[1] = max_size_of_one_area;
    return answer;
}

# recursive

# min(max(0, value), arrSize-1)


[ 수식 최대화 ] : < LV2 / 4181명 >

더보기
#include <string>
#include <vector>
#include <cmath> // abs
#include <algorithm> // next_permutation

using namespace std;

vector<string> CutString(string s)
{
    vector<string> res;
    //
    int i = 0;
    while(true)
    {
        // end
        if(i == s.size())
        {
            res.push_back(s);
            break;
        }
        //
        if('0' <= s[i] && s[i] <= '9')
        {
            ++i;
            continue;
        }
        else
        {
            // add number
            res.push_back(s.substr(0, i));
            s = s.substr(i, s.size());
            // add operator
            res.push_back(s.substr(0, 1));
            s = s.substr(1, s.size());
            //
            i = 0;
        }
    }
    //
    return res;
}

long long Operation(string a, string b, string op)
{
    long long result = 0;
    //
    long long la = stoll(a);
    long long lb = stoll(b);
    //
    switch(op[0])
    {
        case '+':
            result = la + lb;
            break;
        case '-':
            result = la - lb;
            break;
        case '*':
            result = la * lb;
            break;
    }
    //
    return result;
}

long long solution(string expression)
{
    long long answer = 0;
    //
    string s = expression;
    vector<long long> res{};
    vector<string> expCut{};
    vector<string> v{};
    vector<string> ops = {"+", "-", "*"};
    vector<int> order = {0, 1, 2};
    //
    expCut = CutString(s);
    //
    do
    {
        v.clear();
        v = expCut;
        long long val = 0;
        //
        for(int j = 0; j < ops.size(); ++j)
        {
            auto pos = v.begin();
            for(; pos != v.end(); ++pos)
            {
                if(*pos == ops[order[j]])
                {
                    val = Operation(*(pos-1), *(pos+1), *pos);
                    --pos;
                    v.erase(pos, pos + 3);
                    v.insert(pos, to_string(val));
                }
            }
        }
        //
        val = abs(stoll(v[0]));
        res.push_back(val);

    } while(next_permutation(order.begin(), order.end()));
    //
    long long max = 0;
    for(int i = 0; i < res.size(); ++i)
        if(res[i] > max)
            max = res[i];
    //
    answer = max;
    return answer;
}

# string to long long : stoll(string)

# next_permutation


튜플 ] : < LV2 / 6401명 >

더보기
#include <string>
#include <vector>
#include <map>
#include <algorithm> // remove

#define pii pair<int,int>

using namespace std;

bool cmp(const pii& a, const pii& b)
{
    if(a.second == b.second)
        return a.first > b.first;
    //
    return a.second > b.second;
}

vector<int> solution(string s)
{
    vector<int> answer;
    // erase '{', '}'
    s.erase(remove(s.begin(), s.end(), '{'), s.end());
    s.erase(remove(s.begin(), s.end(), '}'), s.end());
    // parse data split by ',' & add to map<int, int>
    map<int, int> m;
    int pos = 0;
    string temp;
    while(pos != string::npos)
    {
        pos = s.find(',');
        temp = s.substr(0, pos);
        s = s.substr(pos+1, s.size());
        ++m[stoi(temp)];
    }
    // sort map by value, descending order
    // typedef pii : pair<int, int>
    vector<pii> vec(m.begin(), m.end());
    sort(vec.begin(), vec.end(), cmp);
    /* ver. lamda
    sort(vec.begin(), vec.end(),
         [](pii a, pii b) { return a.second > b.second; });
    */
    for(auto v : vec)
        answer.push_back(v.first);
    //
    return answer;
}

# remove char in string : s.erase(remove(s.begin(), s.end(), ch), s.end()) // algorithm

# parse string : string to int, stoi(str)

# sort map by value(define, cmp, lamda) : vector<pair<T, T>>