본문으로 바로가기

[풀이 - Happy Ladybugs] : <Easy / 83.86%>

더보기
string happyLadybugs(string b)
{
    string result = "YES";
    //
    map<char, int> m;
    for(int i = 0; i < b.size(); ++i)
        m[b[i]]++;
    // case 1 : has no '_', check unHappy bug
    if(m.count('_') == 0)
    {
        for(int i = 1; i < b.size()-1; ++i)
            if(b[i] != b[i-1] && b[i] != b[i+1])
                return "NO";
    }
    // case 2 : check only one bug
    auto iter = m.begin(), end = m.end();
    for(; iter != end; ++iter)
    {
        char c = iter->first;
        int i = iter->second;
        //
        if(c == '_')
            continue;
        //
        if(i == 1)
            return "NO";
    }
    //
    return result;
}

[풀이 - Strange Counter] : <Easy / 80.38%>

더보기
long strangeCounter(long t)
{
    const long prior = 3;
    long result = 0;
    // find cycle
    long cycle = 0;
    long timeEnd = prior;
    while(true)
    {
        if(t <= timeEnd)
            break;
        else
        {
            ++cycle;
            timeEnd = timeEnd + (prior * pow((long)2, cycle));
        }
    }
    // calc value from timeEnd To t (begin 1)
    result = timeEnd - t + 1;
    return result;
}

 

 


[풀이 - The Grid Search] : <Medium / 74.79%>

더보기
string gridSearch(vector<string> G, vector<string> P)
{
    int gRow = G.size(); int pRow = P.size();
    int gCol = G[0].size(); int pCol = P[0].size();
    //
    for(int y = 0; y < gRow - pRow + 1; ++y)
    {
        for(int x = 0; x < gCol - pCol + 1; ++x)
        {
            bool isPattern = true;
            for(int i = 0; i < pRow; ++i)
            {
                string gStr = G[y+i].substr(x, pCol);
                //
                if(P[i] != gStr)
                {
                    isPattern = false;
                    break;
                }
            }
            if(isPattern)
                return "YES";
        }
    }
    return "NO";
}

 


[풀이 - 3D Surface Area] : <Medium / 92.05%>

더보기
int surfaceArea(vector<vector<int>> A)
{
    int result = 0;
    //
    int H = A.size();
    int W = A[0].size();
    // Set 3D Vector
    for(int y = 0; y < H; ++y)
    {
        for(int x = 0; x < W; ++x)
        {
            int height = A[y][x];
            int bar = 4 * height + 2;
            //
            cout << "bar(before) : " << bar;
            if(x < W-1)
            { // check reight
                int temp = A[y][x+1];
                if(height >= temp)
                    bar -= temp; // right Bar Height
                else
                    bar -= height;
            }
            if(x > 0)
            { // check left
                int temp = A[y][x-1];
                if(height >= temp)
                    bar -= temp; // right Bar Height
                else
                    bar -= height;
            }
            if(y < H-1)
            { // check up
                int temp = A[y+1][x];
                if(height >= temp)
                    bar -= temp; // upper Bar Height
                else
                    bar -= height;
            }
            if(y > 0)
            { // check down
                int temp = A[y-1][x];
                if(height >= temp)
                    bar -= temp; // upper Bar Height
                else
                    bar -= height;
            }
            cout << ", bar(after) : " << bar << endl;;
            result += bar;
        }
    }
    return result;
}