본문 바로가기

프로그래머스

과일 장수

728x90

처음에 문제이해를 잘못해서 3시간 걸렸다...

문제를 꼼꼼히 읽는 연습을 해야겠다. 최대한 상자를 팔아 넘겨야하는데 최고큰 상자 하나만 팔아넘겨버렸다;;

 

import java.util.*;
class Solution {
    public int solution(int k, int m, int[] score) {
        int answer = 0;
        Integer[] scoreInteger = Arrays.stream(score).boxed().toArray(Integer[]::new);
        
        Arrays.sort(scoreInteger,Collections.reverseOrder());
        Deque <Integer> deq = new LinkedList<>();
        int cnt=0;
        int min =9999999;
        int applecnt=0;
        for(int i=0;i<scoreInteger.length;i++)
        {
                if(cnt<m)
                {
                    if(min>scoreInteger[i])
                    {
                        min = scoreInteger[i];
                    }
                    cnt++;
                }
                if(cnt == m)
                {
                   answer += min * m;
                    cnt =0;
                }
                // System.out.println(min + " " + m+ " " + cnt );
            
        }
        // System.out.println(min + " " + m+ " " + applecnt );
        return answer;
    }
}

 

프로그래머스 베스트 코드

import java.util.*;

class Solution {
    public int solution(int k, int m, int[] score) {
        int answer = 0;

        Arrays.sort(score);

        for(int i = score.length; i >= m; i -= m){
            answer += score[i - m] * m;
        }

        return answer;
    }
}
728x90

'프로그래머스' 카테고리의 다른 글

키패드 누르기  (0) 2023.01.16
삼총사  (0) 2023.01.15
푸드파이터대회  (0) 2023.01.14
3번 뒤집기  (0) 2023.01.12
최대공약수와 최소공배수  (0) 2023.01.12