๐Ÿ“Œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ค€๋น„ :: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค :: ์ •๋ ฌ - K๋ฒˆ์งธ ์ˆ˜ ๐Ÿ‘€

Dev-Oยท2021๋…„ 11์›” 23์ผ
0

CodingTest

๋ชฉ๋ก ๋ณด๊ธฐ
2/18
post-thumbnail

๋ฌธ์ œ ์„ค๋ช…

๋ฐฐ์—ด array์˜ i๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆซ์ž๊นŒ์ง€ ์ž๋ฅด๊ณ  ์ •๋ ฌํ–ˆ์„ ๋•Œ, k๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด array๊ฐ€ [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3์ด๋ผ๋ฉด

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

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

์ž…์ถœ๋ ฅ ์˜ˆ
array
[1, 5, 2, 6, 3, 7, 4]
commands
[[2, 5, 3], [4, 4, 1], [1, 7, 3]]
return
[5, 6, 3]


๋ฌธ์ œ ํ•ด์„

copy of range๋ฅผ ํ™œ์šฉํ•ด์„œ ๋ฐฐ์—ด๋ณต์‚ฌ๋ฅผ ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ์žˆ๋˜๋ฐ ๊ทธ๊ฑธ ๋ชฐ๋ž๋‹ค.
๊ทธ๋ƒฅ ์–ด๋””๋ถ€ํ„ฐ ์–ด๋””๊นŒ์ง€ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•ด๋†“๊ณ  ๊ทธ ์ˆœ์„œ๋Œ€๋กœ ์ž„์‹œ๋ฐฐ์—ด์— ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ €์žฅํ•œ๋’ค ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ 
k๋ฒˆ์งธ ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ๋‹ค.

์ฝ”๋“œ

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int k = 0;
        int start = 0;
        int end = 0;
        int[] answer = new int[commands.length];
        for(int i = 0 ; i < commands.length ; i++){
            start = commands[i][0]-1;
            end = commands[i][1];
            k = commands[i][2];
            int[] tmp = new int[end-start];
            for(int j = 0 ; j < tmp.length ; j++) {
                tmp[j] = array[start++];
            }
            Arrays.sort(tmp);
            answer[i] = tmp[k-1];
        }
        return answer;
    }
}
profile
Being Outstanding needs Understanding๐Ÿš€

0๊ฐœ์˜ ๋Œ“๊ธ€