[Programmers] 이모티콘 할인행사 (JAVA)

2024. 5. 18. 17:12·코테/Algorithm
728x90
 

프로그래머스

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

programmers.co.kr

 

풀이

 

중복순열을 사용해서 문제를 해결하면 될 것 같았다.

혹시 시간초과가 발생할 까봐 계산해봤더니 모든 배열이 짧아서 가능할 것 같았다.

 

rate라는 할인율을 담은 배열을 만들어 tmp라는 중복순열을 만들어줬다.

이후 중복순열이 하나 완성되었을 때, 유저와 이모티콘 배열을 돌면서 금액을 계산해줬다.

모든 user를 전부 돌고난 뒤에는 PriorityQueue에 넣어 이모티콘 플러스를 가장 많이 신청한 경우를 출력해줬다.

 

전체코드
import java.io.*;
import java.util.*;

class Solution {
    static int rate[] = {10, 20, 30, 40};
    static boolean visited[] = new boolean[4];
    static List<int[]> list = new ArrayList<>();
    static int emoticons_cnt;
    static int tmp[];
    static PriorityQueue<int[]> q = new PriorityQueue<>(new Comparator<>(){
        public int compare(int[] o1, int[] o2){
            if(o1[0] > o2[0]){
                return -1;
            }else if(o1[0] < o2[0]){
                return 1;
            }else{
                if(o1[1] > o2[1]){
                    return -1;
                }else if(o1[1] < o2[1]){
                    return 1;
                }else{
                    return 0;
                }
            }
        }
    });
    public int[] solution(int[][] users, int[] emoticons) {
        emoticons_cnt = emoticons.length;
        tmp = new int[emoticons_cnt];
        Rate(0, users, emoticons);
        
        int[] answer = q.poll();
        return answer;
    }
    static void Rate(int c, int[][] users, int[] emoticons){
        if(c == emoticons_cnt){
            int plus = 0;
            int sum = 0;
            for(int i=0; i<users.length; i++){
                int each = 0;
                for(int j=0; j<emoticons.length; j++){
                    if(tmp[j] >= users[i][0]){
                        each += (emoticons[j]*(100-tmp[j])/100);
                    }else{
                        continue;
                    }
                }
                if(each >= users[i][1]){
                    plus++;
                }else{
                    sum += each;
                    each = 0;
                }
            }
            int case1[] = {plus, sum};
            q.offer(case1);
            return;
        }
        for(int i=0; i<4; i++){
            tmp[c] = rate[i];
            Rate(c+1, users, emoticons);
        }
    }
}
728x90

'코테 > Algorithm' 카테고리의 다른 글

[Programmers] 구명보트 (JAVA)  (0) 2024.05.22
[Programmers] 다음 큰 숫자 (JAVA)  (0) 2024.05.21
[BOJ] 2485: 가로수 (JAVA)  (0) 2024.05.17
[BOJ] 2578: 빙고 (JAVA)  (0) 2024.05.14
[Programmers] 방문 길이 (JAVA)  (0) 2024.05.12
'코테/Algorithm' 카테고리의 다른 글
  • [Programmers] 구명보트 (JAVA)
  • [Programmers] 다음 큰 숫자 (JAVA)
  • [BOJ] 2485: 가로수 (JAVA)
  • [BOJ] 2578: 빙고 (JAVA)
DROPDEW
DROPDEW
💻 Developer | 기록하지 않으면 존재하지 않는다
  • DROPDEW
    제 2장 1막
    DROPDEW
  • 전체
    오늘
    어제
    • categories (404) N
      • App/Android (1)
      • BE (38) N
        • HTTP 웹 기본 지식 (8)
        • 스프링 입문 - 코드로 배우는 스프링 부트, 웹 .. (12)
        • 스프링부트와 JPA 활용 (5) N
        • 스프링부트 시큐리티 & JWT (0)
        • PHP (6)
      • FE·Client (23)
        • HTML (1)
        • React (19)
        • Unity (1)
      • Data (12)
        • AI (4)
        • Bigdata (6)
        • Database (1)
        • 빅데이터분석기사 (0)
      • Infra (0)
      • CS (7)
        • CS 면접 준비 (3)
      • 취준 (13)
        • 자격증·인턴·교육 (4)
        • 인적성·NCS (6)
        • 코테·필기·면접 후기 (3)
      • 코테 (269) N
        • Algorithm (221) N
        • SQL (35)
        • 정리 (13)
      • 인사이트 (27)
        • 금융경제뉴스 (7)
        • 금융용어·지식 (2)
        • 북마크 (7)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    시뮬레이션
    그래프탐색
    정렬
    브루트포스 알고리즘
    그래프이론
    오블완
    그리디알고리즘
    수학
    매개변수탐색
    자료구조
    문자열
    너비우선탐색
    이분탐색
    티스토리챌린지
    투포인터
    최단경로
    다이나믹프로그래밍
    백준
    누적합
    구현
  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
DROPDEW
[Programmers] 이모티콘 할인행사 (JAVA)
상단으로

티스토리툴바