본문 바로가기
알고리즘/프로그래머스

[프로그래머스] 이모티콘 할인행사 c++

by 오오오니 2023. 11. 25.

https://school.programmers.co.kr/learn/courses/30/lessons/150368

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

풀이

이모티콘이 최대 7개 할인율 가지 수가 4개이기 때문에 전부구해서 계산해주었다.

일단 이모티콘마다 각각 어떤 할인율을 가질지는 dfs함수를 통해 구해주었다.
할인율이 40이라면 60으로 구해주었다. (원가의 60%라는 뜻)

이모티콘이 3개라면 

60, 60 , 60 / 60,60,70/ 60,60,80/ 60,60,90 ...  90,90,90 이 나올수 있다. (중복조합)

각 이모티콘마다 구한 할인율을 가지고 calculate함수에서 유저마다 이모티콘 플러스를 구독할지 말지를 계산해주었다.

 

#include <string>
#include <vector>


using namespace std;

int num[] = {60,70,80,90};
vector<int> n;
int emoticons_size;
vector<vector<int>> users_c;
vector<int> emoticons_c;
int join_max,money_max;
void calculate(){

    int join=0,money=0;
    for(auto u : users_c){
        int sum=0;   
        for(int j=0;j<emoticons_size;j++){
            if(sum >= u[1])
            {   
                break;;
            }
            if(u[0] <=100-n[j] )
            {  
                sum+=(n[j]*emoticons_c[j]/100);
               
            }
            
          
        }
        
         if(sum>= u[1])
        {
                join++;
                sum=0;
                
               
        }else{
             money+=sum;
         }
        
    }

    if(join>=join_max){
        if( join!=join_max)
            money_max=0;
        
        money_max = max(money_max,money);
        join_max=join;

    }


    
}
void dfs (int index){
    if(index==emoticons_size){
        calculate();
        
        return;
    }
    for(int i=0;i<4;i++){
        n[index] = num[i];
        dfs(index+1);
        
    }
}
vector<int> solution(vector<vector<int>> users, vector<int> emoticons) {
    vector<int> answer;
    
    users_c.assign(users.begin(),users.end());
    emoticons_c.assign(emoticons.begin(),emoticons.end());
    emoticons_size=emoticons.size();
    
    n.resize(emoticons_size);
    dfs(0);
    answer.push_back(join_max);
    answer.push_back(money_max);
    return answer;
}

다른 사람의 풀이

 answer = max(answer, {total_user, total_price});

가입자가수 우선이고 그다음에 돈의 총량을 비교하는것을 어떻게 처리하지 고민했는데 
벡터를 max에 넣어서 비교할 수 있었다. 첫번째가 우선이고 두번째가 그 다음 우선순위로 처리된다.