#include <vector>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;
 
int visited[100][100];
int row, col;
 
int dx[4= {-1100}; // 상하좌우
int dy[4= {00-11}; // 상하좌우
 
int safe(int x, int y)
{
    if(x >= 0 && y >= 0 && x < row && y < col)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
int BFS(int x, int y, int color, vector<vector<int>> picture)
{
    queue<pair<intint>> q;
    q.push({x, y});
    visited[x][y] = 1;
    int cnt = 1;
    
    while(!q.empty())
    {
        x = q.front().first;
        y = q.front().second;
        q.pop();
        
        for(int i = 0; i < 4; i++)
        {
            int xpos = x+dx[i];
            int ypos = y+dy[i];
            
            if(safe(xpos, ypos) == 1 && picture[xpos][ypos] == color && visited[xpos][ypos] == 0)
            {
                q.push({xpos, ypos});
                visited[xpos][ypos] = 1;
                cnt++;
            }
        }    
    }
    
    return cnt;
}
 
// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요. 
vector<int> solution(int m, int n, vector<vector<int>> picture) 
{
    int number_of_area = 0;
    int max_size_of_one_area = 0;
    
    memset(visited, 0sizeof(visited));
    row = m;
    col = n;
    
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            if(picture[i][j] != 0 && visited[i][j] == 0)
            {
                int temp = BFS(i, j, picture[i][j], picture);
                max_size_of_one_area = max(temp, max_size_of_one_area);
                number_of_area++;
            }
        }
    }
    
    vector<int> answer;
    answer.push_back(number_of_area);
    answer.push_back(max_size_of_one_area);
    
    return answer;
}
cs
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
int solution(vector<vector<int>> land)
{
    int sum[100000][4= {land[0][0], land[0][1], land[0][2], land[0][3]};
    
    for(int i = 1; i < land.size(); i++)
    {
        sum[i][0= max({sum[i-1][1]+land[i][0], sum[i-1][2]+land[i][0], sum[i-1][3]+land[i][0]});
        sum[i][1= max({sum[i-1][0]+land[i][1], sum[i-1][2]+land[i][1], sum[i-1][3]+land[i][1]});
        sum[i][2= max({sum[i-1][0]+land[i][2], sum[i-1][1]+land[i][2], sum[i-1][3]+land[i][2]});
        sum[i][3= max({sum[i-1][0]+land[i][3], sum[i-1][1]+land[i][3], sum[i-1][2]+land[i][3]});
    }
    
    int Max = max({sum[land.size()-1][0], sum[land.size()-1][1], sum[land.size()-1][2], sum[land.size()-1][3]});
    
    return Max;
}
cs
#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
int solution(int n)
{
    // 자기 자신
    int answer = 1;
    
    for(int i = 1; i <= n/2; i++)
    {
        int sum = i;
        
        for(int j = i+1; j <= n/2+1; j++)
        {
            sum += j;
            
            // 합이 n인 경우 
            if(sum == n)
            {
                answer++;
                break;
            }
            // 합이 n보다 큰 경우
            else if(sum > n)
            {
                break;
            }
        }
    }
    
    return answer;
}
cs
#include <string>
#include <vector>
using namespace std;
 
int count(int n)
{
    // 1의 개수
    int cnt = 0;
    
    for(int i = 0; n > 0; i++)
    {
        if(n % 2 == 1)
        {
            cnt++;
        }
        n /= 2;
    }
    
    return cnt;
}
 
int solution(int n) 
{
    int cmp = count(n);
    
    // n을 1만큼 증가시키면서 확인
    while(1)
    {
        n++;
        
        // 1의 개수가 같으면 종료
        if(count(n) == cmp)
        {
            break;
        }
    }
    
    return n;
}
cs
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
bool solution(vector<string> phone_book) 
{
    // 사전순으로 정렬
    sort(phone_book.begin(), phone_book.end());
    
    // 정렬 후, 앞 뒤끼리만 비교하면 됨
    for(int i = 0; i < phone_book.size()-1; i++)
    {
        if(phone_book[i].size() > phone_book[i+1].size())
        {
            if(phone_book[i].substr(0, phone_book[i+1].size()) == phone_book[i+1])
            {
                return false;
            }
        }
        else if(phone_book[i].size() == phone_book[i+1].size())
        {
            continue;
        }
        else
        {
            if(phone_book[i+1].substr(0, phone_book[i].size()) == phone_book[i])
            {
                return false;
            }
        }
    }
    
    return true;
}
cs
#include <string>
#include <vector>
#include <queue>
using namespace std;
 
int solution(int stock, vector<int> dates, vector<int> supplies, int k) 
{    
    priority_queue<intvector<int>, less<int>> pq;
    
    int day = 0;
    int idx = 0;
    int ans = 0;
    
    while(1)
    {
        day++;
        stock--;
        
        if(day == k)
        {
            break;
        }
        
        // 공급하는 날이 됬으면, 공급량 저장
        if(dates[idx] == day)
        {
            pq.push(supplies[idx]);
            idx++;
        }
        
        // 재고가 0일때 밀가루 공급
        if(stock == 0)
        {
            stock += pq.top();
            pq.pop();
            ans++;
        }
    }
    
    return ans;
}
cs
#include <string>
#include <vector>
#include <stack>
using namespace std;
 
int solution(string arrangement) 
{
    stack<char> st;
    int answer = 0;
    
    for(int i = 0; i < arrangement.size(); i++)
    {
        if(arrangement[i] == '(')
        {
            st.push('(');
        }
        else
        {
            st.pop();
            
            if(arrangement[i-1== '(')
            {
                answer += st.size();
            }
            else
            {
                answer++;
            }
        }
    }
    
    return answer;
}
cs
#include <string>
#include <iostream>
#include <stack>
using namespace std;
 
bool solution(string s)
{
    stack<char> st;
    
    for(int i = 0; i < s.size(); i++)
    { 
        if(s[i] == '(')
        {
            st.push(s[i]);
        }
        else
        {
            if(st.empty())
            {
                return false;
            }
            else
            {
                st.pop();   
            }
        }  
    }
    
    if(st.empty())
    {
        return true;
    }
    else
    {
        return false;
    }
}
cs

+ Recent posts