본문 바로가기
코딩 문제 풀이/프로그래머스

[프로그래머스] Lv3 문제풀이4(feat. JAVA)

by 코딩하는 랄로 2023. 10. 10.
728x90

기지국 설치

import java.util.*;

class Solution {
    public int solution(int n, int[] stations, int w) {
        int answer = 0;

        int end = 0;
        ArrayList<Integer> area = new ArrayList<>();

        for(int i = 0; i < stations.length; i++) {
            int start = stations[i] - w - 1;
            if(start > end) {
                area.add(start - end);
            }
            end = stations[i] + w;
        }
        if(n - end > 0) {
            area.add(n - end);
        }

        int scope = w * 2 + 1;
        for(int a : area) {
            answer += a / scope;
            if(a % scope != 0)
                answer++;
        }

        return answer;
    }
}

 

 

 

베스트 앨범

import java.util.*;
import java.util.stream.Collectors;
class Solution {
    public static  class Music {
        int order;
        int plays;

        Music(int order, int plays) {
            this.order = order;
            this.plays = plays;
        }
    }
    public int[] solution(String[] genres, int[] plays) {
        int[] answer = {};

        HashMap<String, ArrayList<Music>> orderInGenre = new HashMap<>();
        HashMap<String, Integer> countPlays = new HashMap<>();

        for(int i = 0; i < genres.length; i++) {
            if(countPlays.containsKey(genres[i])){
                countPlays.put(genres[i], countPlays.get(genres[i]) + plays[i]);
                orderInGenre.get(genres[i]).add(new Music(i, plays[i]));
            } else {
                countPlays.put(genres[i], plays[i]);
                orderInGenre.put(genres[i], new ArrayList<>());
                orderInGenre.get(genres[i]).add(new Music(i, plays[i]));
            }
        }

        var entries = countPlays.entrySet().stream().sorted(Map.Entry.comparingByValue(((o1, o2) -> o2-o1))).collect(Collectors.toList());

        ArrayList<Integer> result = new ArrayList<>();
        for(var entry : entries) {
            orderInGenre.get(entry.getKey()).sort((o1, o2) -> o2.plays - o1.plays);
            result.add(orderInGenre.get(entry.getKey()).get(0).order);
            if(orderInGenre.get(entry.getKey()).size() > 1) {
                result.add(orderInGenre.get(entry.getKey()).get(1).order);
            }
        }
        answer = result.stream().mapToInt(Integer::intValue).toArray();

        return answer;
    }
}

 

 

 

스티커모으기(2)

import java.util.stream.IntStream;

class Solution {
    public int solution(int sticker[]) {
        int n = sticker.length;
        if(n == 1) return sticker[0];
        else if(n == 2) return Math.max(sticker[0], sticker[1]);

        int[] tmp1 = new int[n];
        int[] tmp2 = new int[n];
        IntStream.range(0, n).forEach(i -> tmp1[i] = sticker[i]);
        IntStream.range(0, n-1).forEach(i -> tmp2[i] = sticker[i+1]);
        tmp1[n-1] = 0;
        tmp2[n-1] = 0;


        int[] dp1 = new int[n];  //스티커를 선택
        int[] dp2 = new int[n];  //스티커를 선탟하지 않음

        dp1[0] =tmp1[0];
        dp1[1] = Math.max(dp1[0], tmp1[1]);
        IntStream.range(2, n).forEach(i -> {
            dp1[i] = Math.max(dp1[i-2] + tmp1[i], dp1[i-1]);
        });
        dp2[0] =tmp2[0];
        dp2[1] = Math.max(dp2[0], tmp2[1]);
        IntStream.range(2, n).forEach(i -> {
            dp2[i] = Math.max(dp2[i-2] + tmp2[i], dp2[i-1]);
        });

        return Math.max(dp1[n-1], dp2[n-1]);
    }
}
728x90