728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

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

 

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

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

programmers.co.kr

 

๋‚ด ํ’€๐Ÿฆท

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        for(int i = 0; i < commands.length; i++) { 
            int start = commands[i][0] -1;
            int end = commands[i][1];
            int k = commands[i][2] - 1;
            
            // ๋ฐฐ์—ด์˜ ํŠน์ • ๊ตฌ๊ฐ„์„ ๋ณต์‚ฌํ•˜๊ณ  ์ •๋ ฌ
            int[] arr = Arrays.copyOfRange(array, start, end);
            Arrays.sort(arr);
            
            // k๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์— ์ถ”๊ฐ€
            answer[i] = arr[k];
            
            }
        return answer;
    }
}

 

์˜ค๋žœ๋งŒ์— ํ’€์–ด๋ณด๋Š” ์ •๋ ฌ๋ฌธ์ œ๋ผ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ง€๋งŒ ๋‹นํ™ฉํ–ˆ๋‹ค ใ…Žใ…Ž

์–ด๋–ป๊ฒŒ ๋ฐฐ์—ด์„ ๊ฐ€์ ธ์˜ค๋Š”๊ฒƒ์ด ์ข‹์„์ง€ for๋ฌธ ๋Œ๋ฉด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์–ด์ค„๊นŒํ–ˆ๋Š”๋ฐ ์ฐพ์•„๋ณด๋‹ˆ copyOfRange๋ผ๋Š” ๋ฐฐ์—ด ๋ณต์‚ฌ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜์—ฌ ์‚ฌ์šฉํ•ด์„œ ์“ฐ๋‹ˆ๊นŒ ๊ธˆ๋ฐฉ ํ•ด๊ฒฐ !!!

copyOfRange ๊ผญ ๊ธฐ์–ตํ•ด ๋‘๊ฒ ์Œ~

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

Arrays.copyOfRange(์›๋ณธ๋ฐฐ์—ด, ์‹œ์ž‘์ธ๋ฑ์Šค, ๋์ธ๋ฑ์Šค);

์›๋ณธ๋ฐฐ์—ด : ๋ณต์‚ฌํ•˜๋ ค๋Š” ๋ฐฐ์—ด
์‹œ์ž‘ ์ธ๋ฑ์Šค : ๋ณต์‚ฌ๋ฅผ ์‹œ์ž‘ํ•  ์ธ๋ฑ์Šค(ํฌํ•จ)
๋์ธ๋ฑ์Šค : ๋ณต์‚ฌ๋ฅผ ์ข…๋ฃŒํ•  ์ธ๋ฑ์Šค(๋ฏธํฌํ•จ)

int[] array = {1, 5, 2, 6, 3, 7, 4};
int[] slicedArray = Arrays.copyOfRange(array, 1, 5); // {5, 2, 6, 3}
728x90
๋ฐ˜์‘ํ˜•

+ Recent posts