🌷🌼λͺ¨μ—¬λ΄μš” 개발의숲🌷🌼

[JAVA/μ½”λ”©ν…ŒμŠ€νŠΈ] 4. κ°€μž₯ 높은 탑 μŒ“κΈ° λ³Έλ¬Έ

개발/μ•Œκ³ λ¦¬μ¦˜ λ¬Έμ œν’€μ΄ JAVA

[JAVA/μ½”λ”©ν…ŒμŠ€νŠΈ] 4. κ°€μž₯ 높은 탑 μŒ“κΈ°

μš”μΌμ΄ 2022. 7. 9. 13:54
λ°˜μ‘ν˜•

μ„€λͺ…

밑면이 μ •μ‚¬κ°ν˜•μΈ μ§μœ‘면체 λ²½λŒλ“€μ„ μ‚¬μš©ν•˜μ—¬ νƒ‘을 μŒ“κ³ μž ν•œλ‹€. νƒ‘은 λ²½λŒμ„ ν•œ κ°œμ”© μ•„λž˜μ—μ„œ μœ„λ‘œ μŒ“μœΌλ©΄μ„œ λ§Œλ“€μ–΄ κ°„λ‹€.

μ•„λž˜μ˜ μ‘°κ±΄μ„ λ§Œμ‘±ν•˜λ©΄μ„œ κ°€μž₯ λ†’은 νƒ‘을 μŒ“을 μˆ˜ μžˆλŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

(쑰건1) λ²½λŒμ€ νšŒμ „μ‹œν‚¬ μˆ˜ μ—†λ‹€. μ¦‰, μ˜†λ©΄μ„ λ°‘λ©΄μœΌλ‘œ μ‚¬μš©ν•  μˆ˜ μ—†λ‹€.

(쑰건2) λ°‘λ©΄μ˜ λ„“이가 κ°™μ€ λ²½λŒμ€ μ—†μœΌλ©°, λ˜ν•œ λ¬΄κ²Œκ°€ κ°™μ€ λ²½λŒλ„ μ—†λ‹€.

(쑰건3) λ²½λŒλ“€μ˜ λ†’μ΄λŠ” κ°™μ„ μˆ˜λ„ μžˆλ‹€.

(쑰건4) νƒ‘을 μŒ“을 λ•Œ λ°‘면이 μ’μ€ λ²½λŒ μœ„에 λ°‘면이 λ„“은 λ²½λŒμ€ λ†“을 μˆ˜ μ—†λ‹€.

(쑰건5) λ¬΄κ²Œκ°€ λ¬΄κ±°μš΄ λ²½λŒμ„ λ¬΄κ²Œκ°€ κ°€λ²Όμš΄ λ²½λŒ μœ„에 λ†“을 μˆ˜ μ—†λ‹€.


μž…λ ₯
μž…λ ₯ νŒŒμΌμ˜ μ²«μ§Έ μ€„μ—λŠ” μž…λ ₯될 λ²½λŒμ˜ μˆ˜κ°€ μ£Όμ–΄μ§„λ‹€. μž…λ ₯으둜 μ£Όμ–΄μ§€λŠ” λ²½λŒμ˜ μˆ˜λŠ” μ΅œλŒ€ 100κ°œμ΄λ‹€.

λ‘˜μ§Έ μ€„λΆ€ν„°λŠ” κ° μ€„에 ν•œ κ°œμ˜ λ²½λŒμ— κ΄€ν•œ μ •λ³΄μΈ λ²½λŒ λ°‘λ©΄μ˜ λ„“이, λ²½λŒμ˜ λ†’이 κ·Έλ¦¬κ³  λ¬΄κ²Œκ°€ μ°¨λ‘€λŒ€λ‘œ μ–‘μ˜ μ •μˆ˜λ‘œ μ£Όμ–΄μ§„λ‹€.

각 λ²½λŒμ€ μž…λ ₯λ˜λŠ” μˆœμ„œλŒ€λ‘œ 1λΆ€ν„° μ—°μ†μ μΈ λ²ˆν˜Έλ₯Ό κ°€μ§„λ‹€. λ²½λŒμ˜ λ„“이, λ†’이 λ¬΄κ²ŒλŠ” 10,000보닀 μž‘κ±°λ‚˜ κ°™μ€ μžμ—°μˆ˜μ΄λ‹€.


좜λ ₯
첫 λ²ˆμ§Έ μ€„에 κ°€μž₯ λ†’이 μŒ“을 μˆ˜ μžˆλŠ” νƒ‘μ˜ λ†’이λ₯Ό μΆœλ ₯ν•œλ‹€.


μ˜ˆμ‹œ μž…λ ₯ 1 

5
25 3 4
4 4 6
9 2 3
16 2 5
1 5 2

μ˜ˆμ‹œ μΆœλ ₯ 1

10

 

 

 

import java.util.*;

class Blick implements Comparable<Blick>{

    public int s;
    public int h;
    public int w;

    Blick(int s, int h, int w){
        this.s = s;
        this.h = h;
        this.w = w;

    }

    @Override
    public int compareTo(Blick o){
        if(this.w==o.w) {
            return o.w - this.w;
        }else{
            return o.s - this.s;
        }
    }

}

public class Main {


    public int solution(int n, ArrayList<Blick>  blick){

        int result = 0;

        int[] dy = new int[n];

        for(int i=0; i<blick.size(); i++){

            dy[i] = blick.get(i).h;

            for(int j=0; j<i; j++){

                if(blick.get(i).w < blick.get(j).w){
                    dy[i] = Math.max(dy[i],   dy[j] + blick.get(i).h );
                }

            }

            result = Math.max(result, dy[i]);
        }

        return result;
    }


    public static void main(String[] args) {

        Main main = new Main();

        Scanner in = new Scanner(System.in);

        int n = in.nextInt();

        ArrayList<Blick> blick = new ArrayList<>();

        for(int i=0; i<n; i++){
            Blick tmp = new Blick(in.nextInt(), in.nextInt(), in.nextInt());
            blick.add(tmp);
        }

        Collections.sort(blick);

        System.out.println(main.solution(n, blick));
    }
}
Comments