#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
#include <string>
#include <vector>
#include <map>
using namespace std;
 
int solution(vector<vector<string>> clothes) 
{
    map<stringint> m;  // 옷 종류 개수 저장할 맵
    vector<string> name; // 옷 종류 이름 저장할 벡터
    
    for(int i = 0; i < clothes.size(); i++)
    {   
        // 옷 종류 이름 저장한적 없으면
        if(m[clothes[i][1]] == 0)
        {
            name.push_back(clothes[i][1]);
        }
        
        m[clothes[i][1]]++;
    }
    
    // (옷 1번 개수+안입었을때) * (옷 2번 개수+안입었을때) * (옷 3번 개수+안입었을때) .... -1(아무것도 안입었을때)
    int result = 1;
    for(int i = 0; i < name.size(); i++)
    {
        result *= (m[name[i]]+1);
    }
    result -= 1;
    
    return result;
}
cs

1. 

#include <string>
#include <vector>
#include <queue>
using namespace std;
 
int solution(int bridge_length, int weight, vector<int> truck_weights) 
{
    queue<int> q;
    int time = 0;
    int sum = 0;
    int truckIdx = 0;
    
    while(1)
    {    
        time++;
        
        // 큐 사이즈 = 다리길이 -> 트럭 도착
        if(q.size() == bridge_length)
        {
            sum -= q.front();
            q.pop();
        }
        
        // 트럭의 무게가 다리의 무게보다 작으면, 트럭을 삽입
        if(sum + truck_weights[truckIdx] <= weight)
        {
            // 마지막 트럭이 삽입되면 종료
            if(truckIdx == truck_weights.size()-1)
            {
                // 마지막 트럭 도착시간 추가
                time += bridge_length;
                break;
            }
            
            q.push(truck_weights[truckIdx]);
            sum += truck_weights[truckIdx];
            truckIdx++;
        }
        // 트럭의 무게가 다리의 무게보다 크면, 0을 삽입해서 트럭을 도착점으로 민다
        else
        {
            q.push(0);
        }    
    }
    
    return time;
}
cs

 

2.

#include <string>
#include <vector>
#include <queue>
using namespace std;
 
int solution(int bridge_length, int weight, vector<int> truck_weights) 
{
    queue<int> q;
    queue<int> t;
    int time = 0;
    int sum = 0;
    int truckIdx = 0;
    
    while(1)
    {    
        time++;
        
        // 현재시간 - 트럭 삽입 시간 = 다리길이  -> 트럭 도착
        if(time - t.front() == bridge_length)
        {
            sum -= q.front();
            q.pop();
            t.pop();
        }
        
        // 트럭의 무게가 다리의 무게보다 작으면, 트럭을 삽입
        if(sum + truck_weights[truckIdx] <= weight)
        {
            // 마지막 트럭이 삽입되면 종료
            if(truckIdx == truck_weights.size()-1)
            {
                // 마지막 트럭 도착시간 추가
                time += bridge_length;
                break;
            }
            
            q.push(truck_weights[truckIdx]);
            t.push(time); // 트럭이 삽입되는 시간 삽입
            sum += truck_weights[truckIdx];
            truckIdx++;
        }
    }
    
    return time;
}
cs

+ Recent posts