Cute Running Puppy

algorithm/Softeer

[softeer] ํ†ต๊ทผ๋ฒ„์Šค ์ถœ๋ฐœ ์ˆœ์„œ ๊ฒ€์ฆํ•˜๊ธฐ - python, java

R.silver 2024. 6. 29. 14:59
๋ฐ˜์‘ํ˜•

๐Ÿ“–๋ฌธ์ œ

Softeer - ํ˜„๋Œ€์ž๋™์ฐจ๊ทธ๋ฃน SW์ธ์žฌํ™•๋ณดํ”Œ๋žซํผ

 

Softeer - ํ˜„๋Œ€์ž๋™์ฐจ๊ทธ๋ฃน SW์ธ์žฌํ™•๋ณดํ”Œ๋žซํผ

 

softeer.ai

โœจํ•ต์‹ฌ ๋‚ด์šฉ

nums[i] < nums[j] and nums[i] > nums[k] ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๋ฐฐ์—ด์„ ์ฐพ์•„ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ธฐ

์ œ์•ฝ ์กฐ๊ฑด: 3 <= N <= 5000 ์ธ ์ •์ˆ˜

๐Ÿค”ํ•ด๊ฒฐ ์•„์ด๋””์–ด 

1. 3์ค‘ for ๋ฌธ ์‚ฌ์šฉ (์‹œ๊ฐ„ ์ดˆ๊ณผ)

๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ๋งŒ๋“ค๊ณ , if ๋ฌธ์œผ๋กœ ๋ฌธ์ œ ์กฐ๊ฑด์„ ํŒ๋‹จ -> O(n^3) ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ!

 

2. ๋ฐฑํŠธ๋ ˆํ‚น (์‹œ๊ฐ„ ์ดˆ๊ณผ)

๋ฐฐ์—ด์˜ ์กฐํ•ฉ์„ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ํ™œ์šฉํ•˜์—ฌ ๋งŒ๋“ค๊ณ  ์กฐ๊ฑด ํŒ๋‹จ -> O(n^3) ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ!

 

3. ๋ˆ„์ ํ•ฉ (์ •๋‹ต)

์ œ์•ฝ ์กฐ๊ฑด์ด 3 <= N <= 5000์œผ๋กœ ๊ฑธ๋ ค ์žˆ๊ธฐ์— O(n^2) ์ดํ•˜ ํ’€์ด ๋ฒ•์„ ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค.

 

i์™€ k์˜ ์กฐํ•ฉ์„ ๊ธฐ์ค€์œผ๋กœ nums[i]์™€ nums[k] ์‚ฌ์ด์˜ nums[i] ๋ณด๋‹ค ํฐ ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

1. nums[i] < nums[k] ๋ผ๋ฉด
nums[k] < nums[i] < nums[j] ๋ผ๋Š” ์กฐ๊ฑด์—๋Š” ๋งž์ง€ ์•Š์œผ๋‚˜
i ์ดํ›„์— ๋“ฑ์žฅํ•˜๋Š” nums[i] ๋ณด๋‹ค ํฐ ์ˆ˜์— ํ•ด๋‹นํ•œ๋‹ค. (bigger += 1)



2. nums[i] > nums[k] ๋ผ๋ฉด
๋ฌธ์ œ์˜ ์กฐ๊ฑด์˜ ์ผ๋ถ€์— ํ•ด๋‹นํ•œ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ i ์™€ k ์‚ฌ์ด์— ๋“ฑ์žฅํ•œ nums[i] ๋ณด๋‹ค ํฐ ์ˆ˜ (bigger)๋ฅผ ๋”ํ•ด ๊ฐ’(ans)์„ ๊ฐฑ์‹ ํ•˜๋Š” ๊ณผ์ •์„ ํ†ตํ•ด ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. 

 

โœ…์ •๋‹ต ์ฝ”๋“œ (python)

import sys
n = int(input())
nums = list(map(int, input().split()))
ans = 0

for i in range(n):
    bigger = 0
    for k in range(i + 1, n):
        if nums[i] < nums[k]: # ์กฐ๊ฑด์— ์•ˆ๋งž์Œ, a[i] ๋ณด๋‹ค ํฐ ์ˆ˜๋ผ๋Š” ๊ฒƒ๋งŒ ์ฒดํฌ
            bigger += 1
        else: # ์กฐ๊ฑด์— ๋งž์Œ - ๊ธฐ์กด์— ์„ธ์—ˆ๋˜ a[i] ๋ณด๋‹ค ํฐ ๊ฐ’๋“ค ๋”ํ•ด์ฃผ๊ธฐ
            ans += bigger

print(ans)

 

โœ…์ •๋‹ต ์ฝ”๋“œ (java)

import java.io.*;
import java.util.*;

public class Main {
    static int n;
    static int[] nums;
    static int bigger;
    static long ans; // ์ž๋ฃŒํ˜• ์ฃผ์˜!
    
    public static void main(String[] args) throws IOException{
        // input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        n = Integer.parseInt(st.nextToken());
        
        nums = new int[n];
        st = new StringTokenizer(br.readLine());
        for(int i = 0 ; i < n; i++){
            nums[i] = Integer.parseInt(st.nextToken());    
        }

        // ๋ˆ„์ ํ•ฉ
        for(int i = 0 ; i < n; i++) {
            bigger = 0;
            for (int k = i + 1; k < n; k++) {
                if (nums[i] < nums[k]) // ์กฐ๊ฑด์— ์•ˆ๋งž์Œ, a[i] ๋ณด๋‹ค ํฐ ์ˆ˜๋ผ๋Š” ๊ฒƒ๋งŒ ์ฒดํฌ
                    bigger += 1;
                else // ์กฐ๊ฑด์— ๋งž์Œ - ๊ธฐ์กด์— ์„ธ์—ˆ๋˜ a[i] ๋ณด๋‹ค ํฐ ๊ฐ’๋“ค ๋”ํ•ด์ฃผ๊ธฐ
                    ans += bigger;
            }
        }

        // output
        System.out.println(ans);
    }
}

 

 

๋ฐ˜์‘ํ˜•