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

โน๏ธ [SW Expert Academy] [Python] [D3] 1206. View

Dbswnstjd 2024. 3. 12. 22:14

๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh

 

SW Expert Academy

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

swexpertacademy.com

ํ’€์ด

SWEA์˜ D3 ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ์ œ๋Œ€๋กœ ์ฝ์ง€ ์•Š์•„์„œ ์™ผ์ชฝ 2์นธ๊ณผ ์˜ค๋ฅธ์ชฝ ๋‘์นธ์€ ๋†’์ด๊ฐ€ 0์ธ ๊ฒƒ์„ ๊ฐ„๊ณผํ•˜์˜€๋‹ค. ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฌด์กฐ๊ฑด ๋ชจ๋“  ์กฐ๊ฑด์„ ์ •ํ™•ํžˆ ์ฝ๊ณ  ํ•ด๊ฒฐํ•ด์•ผ๊ฒ ๋‹ค. ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ๋์˜ ๋†’์ด๊ฐ€ 0์ด๋ฏ€๋กœ ๋ฌธ์ œ์˜ ๋‚œ์ด๋„๊ฐ€ ํ™• ๋‚ฎ์•„์ง„๋‹ค. ๋‹จ์ˆœํžˆ ์ž์‹ ์˜ ์œ„์น˜์—์„œ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ 2์นธ ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  ์นธ ์ค‘์— ๊ฐ€์žฅ ๋†’์€ ์นธ์˜ ๊ฐ’์„ ๋นผ๋ฉด ์กฐ๋ง๊ถŒ์˜ ์„ธ๋Œ€ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.  

T = int(input())
# ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์ฃผ์–ด์ง€๋ฏ€๋กœ, ๊ฐ๊ฐ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
for test_case in range(1,11):
    result = 0
    houseCount = int(input())
    house = list(map(int , input().split()))
    for i in range(2, houseCount-2):
        arMax = max(house[i-1],house[i-2],house[i+1],house[i+2])
        if house[i] > arMax:
            result += ( house[i] - arMax )

    print("#{} {}".format(test_case,result))

SWEA๋Š” ์ฒ˜์Œ์ด๋ผ ๋ฌธ์ œ ์ œ์ถœ ๋ฐฉ์‹์ด๋‚˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์ ์‘์ด ์ข€ ์•ˆ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋ฐฑ์ค€์ด๋ž‘ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์— ๋น„ํ•ด ์–ด๋ ต๋‹ค๊ณ  ํ•ด์•ผํ•˜๋‚˜..? ์กฐ๊ธˆ ๋” ๋งŽ์€ ๋ฌธ์ œ๋“ค์„ ํ’€์–ด๋ณด๋ฉด์„œ ๋ฐฑ์ค€์ด๋ž‘ ๋‚œ์ด๋„ ๋น„๊ต๋ฅผ ํ•ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค. ์‚ผ์„ฑ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์•ž๋‘๊ณ  ๋” ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด์•ผ ํ•œ๋‹ค. 

 

Java ์ฝ”๋“œ

package SWEA;
import java.util.Scanner;

class swea_1204
{
    static int N;
    static int[] arr;

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);

        int T = 10; // TestCase ์ˆ˜
        for (int tc = 1; tc <= T; tc++) {
            N = sc.nextInt(); // ๊ฑด๋ฌผ ๊ฐฏ์ˆ˜
            arr = new int[N];
            for (int i = 0; i < N; i++) {
                arr[i] = sc.nextInt();
            }

            int cnt = 0;
            for (int i = 2; i < N - 2; i++) {
                int max = Math.max(arr[i - 2], Math.max(arr[i - 1], Math.max(arr[i + 1], arr[i + 2])));
                if (arr[i] - max > 0)
                    cnt += arr[i] - max;
            }

            System.out.println("#" + tc + " " + cnt);
        }
    }
}