๐Ÿ—๏ธ Algorithm/โน๏ธ SW Expert Academy [SWEA]

โน๏ธ[SW Expert Academy] [Python] [D2] [1859] ๋ฐฑ๋งŒ ์žฅ์ž ํ”„๋กœ์ ํŠธ

Dbswnstjd 2024. 3. 13. 22:12

๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&problemLevel=3&problemLevel=4&problemLevel=5&contestProbId=AV5LrsUaDxcDFAXc&categoryId=AV5LrsUaDxcDFAXc&categoryType=CODE&problemTitle=&orderBy=INQUERY_COUNT&selectCodeLang=ALL&select-1=5&pageSize=10&pageIndex=1

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

ํ’€์ด

์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณธ๋‹ค๋ฉด ์กฐ๊ธˆ ์ƒ๊ฐํ•˜๋Š” ๋ฐฉ์‹์„ ๋‹ฌ๋ฆฌํ•ด์•ผํ•ด์„œ ์–ด๋ ค์›€์ด ์žˆ์—ˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ๋‚˜๋„ ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ๋ดค์„ ๋•Œ ์Šคํƒ์ด๋‚˜ ํ๋ฅผ ์ด์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ–ˆ๋‹ค. ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ๋ฒˆ์งธ๋Š” 10, 7, 6 ์œผ๋กœ ๊ฐ’์ด ์ ์  ๊ฐ์†Œํ•˜๋ฏ€๋กœ ๊ตฌ๋งคํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ตœ์ ์˜ ํ•ด์ด๋‹ค. ๋‘๋ฒˆ์งธ๋Š” ๊ฐ’์ด ํ•ญ์ƒ ์ฆ๊ฐ€ํ•˜๋ฏ€๋กœ ๊ทธ๋‚  ๋ฐ”๋กœ ์‚ฌ์„œ ๋งˆ์ง€๋ง‰ ๋‚ ์— ํŒ”๋ฉด๋œ๋‹ค. ๋งˆ์ง€๋ง‰ ์„ธ๋ฒˆ์งธ ์ผ€์ด์Šค๋งŒ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ 1, 1, 3, 1, 2 ๋กœ ์ฒซ์งธ์™€ ๋‘˜์งธ๋‚ ์—๋Š” ๊ตฌ๋งค๋ฅผํ•ด์„œ ์…‹์งธ๋‚ ์— ํŒ”๊ณ , ๋„ท์งธ๋‚ ์— ์‚ฌ์„œ ๋‹ค์„ฏ์งธ๋‚ ์— ํŒ”๋ฉด๋œ๋‹ค. ๋จผ์ € ์ด ์ˆœ์„œ๋ฅผ ์—ญ์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด์ž. ๊ทธ๋Ÿฌ๋ฉด 2, 1, 3, 1, 1์ด ๋œ๋‹ค. 2->1๋กœ ๊ฐˆ๋•Œ ํŒ”๊ณ , 3->1๋กœ ๊ฐˆ๋•Œ ํŒ”๊ณ , 1->1๋กœ ๊ฐˆ๋•Œ 3-1์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.  ์ฆ‰, ์—ญ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ด์ „ ๊ฐ’์ด ๋‹ค์Œ ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด ํŒ”๊ณ , max_val์ด๋ผ๋Š” ๊ฐ’์— ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ €์žฅํ•ด ๋‘๋„๋กํ•œ๋‹ค. 

์ด๊ฒƒ์„ ์ฝ”๋“œ๋กœ ๋ณด์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

# 1859. ๋ฐฑ๋งŒ ์žฅ์ž ํ”„๋กœ์ ํŠธ D2
T = int(input())
for test_case in range(1, T+1):
    N = int(input())
    price = list(map(int, input().split()))
    price = price[::-1]
    length = len(price)
    max_val = price[0]
    result = 0

    for i in range(1, length):
        if max_val > price[i]:
            result += max_val - price[i]
        else:
            max_val = price[i]
    
    print("#{} {}".format(test_case, result))

1. price๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ๋’ค์ง‘์–ด ์ค€๋‹ค.

2. max_val์—๋Š” ์ฒ˜์Œ ์‹œ์ž‘๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.

3. ์ธ๋ฑ์Šค๋Š” 1๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰๊นŒ์ง€ for๋ฌธ์„ ๋Œ์•„์ค€๋‹ค.

- ๋งŒ์•ฝ max_val์ด ๊ฐ€๊ฒฉ๋ณด๋‹ค ๋น„์‹ธ๋‹ค๋ฉด ๋ฌผ๊ฑด์„ ํŒ” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ์— ๋”ํ•ด์ค€๋‹ค.

- max_val์ด ๊ฐ€๊ฒฉ๋ณด๋‹ค ์‹ธ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด max_val์˜ ๊ฐ’์„ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. 

 

Java ์ฝ”๋“œ

package SWEA;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class SW_1859 {
    public static void main(String[] args) throws Exception, NullPointerException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int T = Integer.parseInt(br.readLine());
        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = Integer.parseInt(br.readLine());
            int[] price = new int[N];
            st = new StringTokenizer(br.readLine(), " ");
            for(int i=0; st.hasMoreTokens(); i++){
                price[i] = Integer.parseInt(st.nextToken());
            }
            int length = price.length;
            int maxValue = price[N-1];
            long result = 0;

            for(int i=N-1; i>=0; i--){
                if(price[i] > maxValue) maxValue = price[i];
                result += maxValue-price[i];
            }
            System.out.println("#" + test_case + " " + result);
        }
    }
}

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 10๊ฐœ์ค‘ 7๊ฐœ ๋งž์•˜๋‹ค๊ณ  ํ•ด์„œ ์•„๋ฌด๋ฆฌ ๋ด๋„ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ž‘ ๋น„๊ตํ•ด๋ณด๊ณ  ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ž‘ ๋น„๊ตํ•ด๋ดค์„ ๋•Œ ํ‹€๋ฆฐ๊ฒŒ ์—†์—ˆ๋Š”๋ฐ ์•Œ๊ณ ๋ณด๋‹ˆ result์˜ ํƒ€์ž…์ด intํ˜•์ด์—ˆ๋‹ค. 100๋งŒ * 1๋งŒ ํ–ˆ์„ ๋•Œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” intํ˜• ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ด๋Ÿฐ ๊ฒƒ ๋•Œ๋งค ์‹œ๊ฐ„ ๋‚ญ๋น„๋ฅผ ํ•˜๋Š”๊ฒŒ ..