๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿงฉ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/๐Ÿต ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด(Java)

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_K๋ฒˆ์งธ์ˆ˜

by carrot0911 2024. 12. 4.

๋ฌธ์ œ ์„ค๋ช…

https://school.programmers.co.kr/learn/courses/30/lessons/42748

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

๋ฐฐ์—ด array์˜ i๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆซ์ž๊นŒ์ง€ ์ž๋ฅด๊ณ  ์ •๋ ฌํ–ˆ์„ ๋•Œ, k๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด array๊ฐ€ [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3์ด๋ผ๋ฉด

  1. array์˜ 2๋ฒˆ์งธ๋ถ€ํ„ฐ 5๋ฒˆ์งธ๊นŒ์ง€ ์ž๋ฅด๋ฉด [5, 2, 6, 3]์ž…๋‹ˆ๋‹ค.
  2. 1์—์„œ ๋‚˜์˜จ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๋ฉด [2, 3, 5, 6]์ž…๋‹ˆ๋‹ค.
  3. 2์—์„œ ๋‚˜์˜จ ๋ฐฐ์—ด์˜ 3๋ฒˆ์งธ ์ˆซ์ž๋Š” 5์ž…๋‹ˆ๋‹ค.

๋ฐฐ์—ด array, [i, j, k]๋ฅผ ์›์†Œ๋กœ ๊ฐ€์ง„ 2์ฐจ์› ๋ฐฐ์—ด commands๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, commands์˜ ๋ชจ๋“  ์›์†Œ์— ๋Œ€ํ•ด ์•ž์„œ ์„ค๋ช…ํ•œ ์—ฐ์‚ฐ์„ ์ ์šฉํ–ˆ์„ ๋•Œ ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • array์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • array์˜ ๊ฐ ์›์†Œ๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • commands์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 50 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • commands์˜ ๊ฐ ์›์†Œ๋Š” ๊ธธ์ด๊ฐ€ 3์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

array commands return
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

[1, 5, 2, 6, 3, 7, 4]๋ฅผ 2๋ฒˆ์งธ๋ถ€ํ„ฐ 5๋ฒˆ์งธ๊นŒ์ง€ ์ž๋ฅธ ํ›„ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. [2, 3, 5, 6]์˜ ์„ธ ๋ฒˆ์งธ ์ˆซ์ž๋Š” 5์ž…๋‹ˆ๋‹ค.
[1, 5, 2, 6, 3, 7, 4]๋ฅผ 4๋ฒˆ์งธ๋ถ€ํ„ฐ 4๋ฒˆ์งธ๊นŒ์ง€ ์ž๋ฅธ ํ›„ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. [6]์˜ ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋Š” 6์ž…๋‹ˆ๋‹ค.
[1, 5, 2, 6, 3, 7, 4]๋ฅผ 1๋ฒˆ์งธ๋ถ€ํ„ฐ 7๋ฒˆ์งธ๊นŒ์ง€ ์ž๋ฆ…๋‹ˆ๋‹ค. [1, 2, 3, 4, 5, 6, 7]์˜ ์„ธ ๋ฒˆ์งธ ์ˆซ์ž๋Š” 3์ž…๋‹ˆ๋‹ค.

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        for (int m = 0; m < commands.length; m++) {
            int i = commands[m][0];
            int j = commands[m][1];
            int k = commands[m][2];
            
            int[] temp = new int[j-i+1];
            int o = 0;
            for (int n = i-1; n < j; n++) {
                temp[o] = array[n];
                o++;
            } 
            Arrays.sort(temp);
            
            answer[m] = temp[k-1];
        }
        return answer;
    }
}

์ฝ”๋“œ ์„ค๋ช…

  • int[ ] answer = new int[commands.length] : commands ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ํฌ๊ธฐ๋ฅผ ๊ฐ–๋Š” answer ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.
  • for (int m = 0; m < commands.length; m++) { } : for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ commands ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • int i = commands[m][0], int j = commands[m][1], int k = commands[m][2] : commands ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ, ์„ธ ๋ฒˆ์งธ ๊ฐ’์„ ๊ฐ๊ฐ i, j, k์— ์ €์žฅํ•œ๋‹ค.
    • int[ ] temp = new int[j - i + 1] : array ๋ฐฐ์—ด์˜ i๋ฒˆ์งธ๋ถ€ํ„ฐ j๋ฒˆ์งธ๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์ €์žฅํ•  temp ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.
    • int o = 0 : ์ •์ˆ˜ํ˜• ๋ณ€์ˆ˜ o๋ฅผ ์ƒ์„ฑํ•˜๊ณ  0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
    • for (int n = i; n < j; n++) { temp[o] = array[n] } : for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ array์˜ i๋ฒˆ์งธ๋ถ€ํ„ฐ j๋ฒˆ์งธ ์š”์†Œ๋ฅผ temp ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•œ๋‹ค.
  • Arrays.sort(temp) : temp ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค.
  • answer[m] = temp[k-1] : ์ •๋ ฌ๋œ temp ๋ฐฐ์—ด์—์„œ k๋ฒˆ์งธ ์ˆ˜๋ฅผ answer์˜ m๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์ €์žฅํ•œ๋‹ค.