#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
#include <string>
#include <vector>
using namespace std;
 
vector<int> copy_numbers;
int copy_target;
 
int result[20];
int answer;
 
void DFS(int cnt)
{
    if(cnt == copy_numbers.size())
    {
        int num = 0;
        
        for(int j = 0; j < cnt; j++)
        {
            num += result[j];
        }
        
        if(num == copy_target)
        {
            answer++;
        }
        
        return;
    }
 
    for(int i = 0; i < 2; i++)
    {
        if(i == 0)
        {
            result[cnt] = copy_numbers[cnt];
            DFS(cnt+1);
        }
        else if(i == 1)
        {
            result[cnt] = -copy_numbers[cnt];
            DFS(cnt+1);
        }
    }
}
 
int solution(vector<int> numbers, int target) 
{
    copy_numbers = numbers;
    copy_target = target;
    
    DFS(0);
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <math.h>
using namespace std;
 
vector<int> solution(int brown, int red) 
{
    vector<int> answer;
    
    // 제곱근까지 약수를 구해서 레드카펫의 가로, 세로 길이를 구함
    for(int redCol = 1; redCol <= sqrt(red); redCol++)
    {
        if(red % redCol == 0)
        {
            int redRow = red / redCol;
            
            if(redRow*2 + redCol*2 + 4 == brown)
            {
                answer.push_back(redRow+2);
                answer.push_back(redCol+2);
                
                return answer;
            }
        }
    }
}
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
int solution(vector<int> people, int limit) 
{
    int answer = 0;
    
    sort(people.begin(), people.end());
 
    int i = 0;
    for(int j = people.size()-1; j >= 0; j--)
    {
        // 더 이상 태울 사람이 없는 경우 종료
        if(i > j)
        {
            break;
        }
        
        // 첫번째와 마지막 사람의 무게 합
        if(people[i] + people[j] <= limit)
        {
            i++;
            answer++;
        }
        else
        {
            answer++;
        }
    }
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
int num[3];
int result[3];
int visited[10];
int answer;
 
vector<vector<int>> copy_baseball;
 
int strike()
{
    int strikeCnt = 0;
    
    for(int i = 0; i < 3; i++)
    {            
        if(result[i] == num[i])
        { 
            strikeCnt++;
        }
    }
    
    return strikeCnt;
}
 
int ball()
{
    int ballCnt = 0;
    
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if(i == j)
            {
                continue;
            }
 
            if(result[i] == num[j])
            {
                ballCnt++;
            }
        }
    }
    
    return ballCnt;
}
 
void DFS(int cnt)
{
    if(cnt == 3)
    {   
        for(int i = 0; i < copy_baseball.size(); i++)
        {
            // 자리수별로 num에 저장
            int temp = copy_baseball[i][0];
            for(int j = 2; j >= 0; j--)
            {
                num[j] = temp % 10;
                temp /= 10;
            }
            
            // 스트라이크
            if(strike() != copy_baseball[i][1])
            {
                return;
            }
            
            // 볼
            if(ball() != copy_baseball[i][2])
            {
                return;
            }    
        }
        
        answer++;   
        
        return;
    }
    
    for(int i = 1; i <= 9; i++)    
    {
        if(visited[i] == 0)
        {
            visited[i] = 1;
            result[cnt] = i;
            DFS(cnt+1);   
            visited[i] = 0;
        }
    }
}
 
int solution(vector<vector<int>> baseball) 
{
    copy_baseball = baseball;
    
    DFS(0);
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <queue>
using namespace std;
 
int solution(vector<int> scoville, int K) 
{
    int answer = 0;
    
    priority_queue<intvector<int>, greater<int>> pq;
    
    for(int i = 0; i < scoville.size(); i++)
    {
        pq.push(scoville[i]);
    }
    
    while(1)
    {   
        int first = pq.top();
        
        // 가장 작은 수(맨 앞)이 K보다 크면, 전부 K보다 큼
        if(first >= K)
        {
            return answer;
        }
        // K이상으로 만들 수 없는 경우
        else if(scoville.size()-1 == answer)
        {
            return -1;
        }
        
        pq.pop();
        int second = pq.top();
        pq.pop();   
        
        int mix = first + (second * 2);
        pq.push(mix);
        
        answer++;
    }
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<string> temp;
 
// 3, 30 있을때 330 vs 303 비교
bool cmp(string a, string b)
{
    if(a+> b+a)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
string solution(vector<int> numbers) 
{
    string answer = "";
    vector<string> temp;
    
    // string 배열에 numbers 삽입
    for(int i = 0; i < numbers.size(); i++)
    {
        temp.push_back(to_string(numbers[i]));
    }
 
    // 정렬
    sort(temp.begin(), temp.end(), cmp);
    
    for(int i = 0; i < temp.size(); i++)
    {
        answer += temp[i];
    }
    
    // 0000과 같은 경우 -> 0
    if(answer[0== '0')
    {
        return "0";
    }
    else
    {
        return answer;   
    }
}
cs

+ Recent posts