๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algoritm/Quiz-Solutions

[Programmers/์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—ฐ์Šต/์ •๋ ฌ] k๋ฒˆ์งธ ์ˆ˜

by ๐Ÿ’œautumn 2020. 8. 30.

๋ฌธ์ œ

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - K๋ฒˆ์งธ์ˆ˜

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

programmers.co.kr

๋‚ด ํ’€์ด

import java.util.Arrays;

public int[] solution(int[] array, int[][] commands) {
    int[] answer = new int[commands.length];
   
    for(int i=0; i<commands.length; i++) {
        int[] currentArray=Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
        	
        Arrays.sort(currentArray);
        answer[i]=currentArray[commands[i][2]-1];
    }
    return answer;
}

๋‹ค๋ฅธ ๋‹ต์•ˆ๋“ค๋„ ๋™์ผํ•˜๊ฒŒ ์ž‘์„ฑ

 


 

 check Point! 

1. ๋ฐฐ์—ด์€ ๊ฒฐ๊ตญ ์กฐ์ž‘ํ•˜๊ธฐ์œ„ํ•ด ๋ณต์‚ฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ! ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ด๋“  ์‚ญ์ œ๋ฅผ ํ•˜๋“  ๋“ฑ๋“ฑ..

2. ์ฒ˜์Œ์— ์›ํ•˜๋Š” ๋ฐฐ์—ด์„ ์ฐจ๋ฅด๊ธฐ ์œ„ํ•ด ๋นˆ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ  for๋ฌธ์„ ๋Œ๋ ค์„œ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ,

Arrays.copyOfRange(array, beginIndex, endIndex) : ์ง€์ •ํ•œ ์ธ๋ฑ์Šค ๋ฒ”์œ„๋งŒํผ ์ธ๋ฑ์Šค ๋ณต์‚ฌ(์ข…๋ฃŒ์ธ๋ฑ์Šค๋ฏธํฌํ•จ)

๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์•Œ๊ฒŒ๋˜์–ด ํ™œ์šฉ

3. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ–ˆ๋‹ค๋ฉด ๊ผญ! import๋ฅผ ์‹œ์ผœ์ค˜์•ผํ•œ๋‹ค๋Š” ๊ฒƒ ๋ช…์‹ฌ!!!!!

 

 

 doubtful Point! 

์ œ์ถœ๋œ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ์ž๋™์œผ๋กœ currentArray๊ฐ€ for๋ฌธ ์œ„์— ์ƒ์„ฑ๋˜์–ด์žˆ๊ณ  for๋ฌธ ์•ˆ์—์„œ ๋ณ€๊ฒฝ๋˜๋Š” ํ˜•์‹์œผ๋กœ ๋˜์—ˆ๋‹ค..?

์™œ์ธ์ค„์€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š”๊ฒƒ์ด ๋” ํšจ์œจ์ ์ธ๊ฐ€?

 

๋Œ“๊ธ€