Algorithm๐Ÿฅ‡

1149.RGB๊ฑฐ๋ฆฌ

hae02y 2023. 11. 7. 15:03
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

RGB๊ฑฐ๋ฆฌ์—๋Š” ์ง‘์ด N๊ฐœ ์žˆ๋‹ค. ๊ฑฐ๋ฆฌ๋Š” ์„ ๋ถ„์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ณ , 1๋ฒˆ ์ง‘๋ถ€ํ„ฐ N๋ฒˆ ์ง‘์ด ์ˆœ์„œ๋Œ€๋กœ ์žˆ๋‹ค.

์ง‘์€ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘ ์ค‘ ํ•˜๋‚˜์˜ ์ƒ‰์œผ๋กœ ์น ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ์ง‘์„ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•˜๋Š” ๋น„์šฉ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ๊ทœ์น™์„ ๋งŒ์กฑํ•˜๋ฉด์„œ ๋ชจ๋“  ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž.

  • 1๋ฒˆ ์ง‘์˜ ์ƒ‰์€ 2๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  • N๋ฒˆ ์ง‘์˜ ์ƒ‰์€ N-1๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  • i(2 ≤ i ≤ N-1)๋ฒˆ ์ง‘์˜ ์ƒ‰์€ i-1๋ฒˆ, i+1๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.

## ์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ง‘์˜ ์ˆ˜ N(2 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ง‘์„ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•˜๋Š” ๋น„์šฉ์ด 1๋ฒˆ ์ง‘๋ถ€ํ„ฐ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์€ 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

## ์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ชจ๋“  ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

์‹œ๊ฐ„ ์ œํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ์ œ์ถœ ์ •๋‹ต ๋งžํžŒ ์‚ฌ๋žŒ ์ •๋‹ต ๋น„์œจ
0.5 ์ดˆ (์ถ”๊ฐ€ ์‹œ๊ฐ„ ์—†์Œ) 128 MB 105821 58495 43577 54.494%

https://www.acmicpc.net/problem/1149

ํ’€์ด

์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ๋ฆ„ + ์ฝ”๋“œ 

์ฒซ๋ฒˆ์งธ ์‹œ๋„

package ๊ธฐ์ดˆ1.Day19.์ •ํ•ด์˜;  

import java.util.Scanner;  

public class RGB๊ฑฐ๋ฆฌ {  

    public static void main(String[] args) {  

        Scanner sc = new Scanner(System.in);  

        int n = sc.nextInt(); //์ง‘์˜ ์ˆ˜  

        int[] dp =new int[n+1];  
        int[][] arr = new int[n+1][3];  
        int index = 4;  


        for(int i=1; i<=n; i++){  
            for(int j=0; j<3; j++){  
                arr[i][j] = sc.nextInt();  
            }  
        }  

        for(int i=1; i<=n; i++){  
            dp[i] = Integer.MAX_VALUE;  

            for(int j=0; j<3; j++){  
                if(index != j){  
                    dp[i] = Math.min(dp[i], arr[i][j]);  
                    index = j;  
                }  
            }  
        }  

        int result = 0;  

        for(int i=1; i<=n; i++){  
            result += dp[i];  
        }  

        System.out.println(result);  
        sc.close();  
    }  
}

์‹คํŒจ..!

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๊ณ  ๋Œ๋ ค๋ณด๋‹ˆ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

if(index != j){  
    dp[i] = Math.min(dp[i], arr[i][j]);  
    index = j;  
}  

์—ฌ๊ธฐ์„œ ๋ฌด์กฐ๊ฑด ์ฒซ๋ฒˆ์งธ ๋ถ€ํ„ฐ ๋Œ๋ฉฐ 1๋ฒˆ์ง‘์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ–๊ฒŒ๋˜๋ฉฐ ์‹œ์ž‘ํ•œ๋‹ค. ํ•˜์ง€๋งŒ 1๋ฒˆ์ง‘์—์„œ ๋Š” ์ตœ์†Ÿ๊ฐ’์ด, ์ „์ฒด์ ์œผ๋กœ ๋ดค์„๋•Œ ์ตœ์†Ÿ๊ฐ’์„ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”

๊ฐ์ง‘์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด ๋ˆ„์ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ๋ชจ๋“  ๊ฒฝ๋กœ์˜ ์ˆ˜๋ฅผ ์ฐพ์•„์„œ ์ตœ์ข…์ ์œผ๋กœ ์ž‘์€ ๋ˆ„์ ํ•ฉ์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

//Red์ผ ๊ฒฝ์šฐ
Cost[N][0] = min( Cost[N-1][1], Cost[N-1][2] ) + Cost[N][0]

//Green์ผ ๊ฒฝ์šฐ
Cost[N][1] = min( Cost[N-1][0], Cost[N-1][2] ) + Cost[N][1]

//Blue์ผ ๊ฒฝ์šฐ
Cost[N][2] = min( Cost[N-1][0], Cost[N-1][1] ) + Cost[N][2]

๋‘๋ฒˆ์งธ ์‹œ๋„

import java.util.Scanner;

public class Main {

    static int Red = 0;
    static int Green = 1;
    static int Blue = 2;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt(); //์ง‘์˜ ์ˆ˜

        int[][] arr = new int[n][3];

        for(int i=0; i<n; i++){
            arr[i][Red] = sc.nextInt();
            arr[i][Green] = sc.nextInt();
            arr[i][Blue] = sc.nextInt();
        }

        for(int i=1; i<n; i++){
            arr[i][Red] += Math.min(arr[i - 1][Green], arr[i - 1][Blue]);
            arr[i][Green] += Math.min(arr[i - 1][Red], arr[i - 1][Blue]);
            arr[i][Blue] += Math.min(arr[i - 1][Red], arr[i - 1][Green]);
        }

        //n-1๊นŒ์ง€ ๊ตฌํ•ด๋†“๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ทธ๋“ค์ค‘ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด์ค€๋‹ค.
        //arr์—๋Š” ๋ˆ„์ ํ•ฉ์ด ์Œ“์—ฌ์žˆ๋Š”๊ฒƒ์ด๋‹ค.

        System.out.println(Math.min(Math.min(arr[n-1][Red], arr[n-1][Green]),arr[n-1][Blue]));
        sc.close();
    }
}

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด์„œ ์„ฑ๊ณต!

๋ฐ˜์‘ํ˜•