๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Programmers lv-1

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

by ๊ทคํ”ผํ‚ค 2022. 12. 13.

 

โค๏ธ Problem

๋”๋ณด๊ธฐ
  • ๋ฌธ์ œ
    ๋ฐฐ์—ด 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์ž…๋‹ˆ๋‹ค.

 

  • ์ž…์ถœ๋ ฅ ์˜ˆ & ์„ค๋ช…
no array commands return
1 [1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
  1. [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์ž…๋‹ˆ๋‹ค.

 


 

๐Ÿ’› Solution

ํ’€์ด 1

import java.util.Arrays;
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]-1;
            int point = commands[i][2]-1;

            int[] sorting = new int[end-start+1];
            int index = 0;
            for(int j=start; j<=end; j++) {
                sorting[index] = array[j];
                index++;
            }
            Arrays.sort(sorting);
            answer[i] = sorting[point];
//            System.out.println(answer[i]);
        }
        return answer;
    }
}

 

์ฒ˜๋ฆฌ์†๋„ SO SO๐Ÿค”



ํ’€์ด 2

import java.util.Arrays;
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 point = commands[i][2]-1;

            int[] sorting = Arrays.copyOfRange(array, start, end);
            Arrays.sort(sorting);
            answer[i] = sorting[point];
            System.out.println(answer[i]);
        }
        return answer;
    }
}

 

์ฒ˜๋ฆฌ์†๋„ imporved?๐Ÿ˜…


 


 

๐Ÿ’œ Comment

์ด ๋ฌธ์ œ๋Š” ๋ฐฐ์—ด์˜ ํŠน์ • ๊ตฌ๊ฐ„(commands[i][0]๋ถ€ํ„ฐ commands[i][1])์˜ ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๊ฒŒ ํฌ์ธํŠธ์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•จ์ •์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ๋ฐฐ์—ด์˜ n๋ฒˆ์งธ ์œ„์น˜๋กœ ์ฃผ์–ด์ง€๋Š” commands[i][2]๊ฐ€ ์‹ค์ œ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์ฒ˜๋Ÿผ 0์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ -1์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค.

ํ’€์ด 1์—์„œ๋Š” for ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ int[] array์˜ commands[i][0]๋ถ€ํ„ฐ commands[i][1]๊นŒ์ง€์˜ ๋ฐฐ์—ด์„ int[] sorting์œผ๋กœ ์ง์ ‘ ๋Œ€์ž…ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ ๋’ค, Array.sort()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ •๋ ฌํ•˜์˜€๋‹ค. ๊ทธ ๋‹ค์Œ commands[i][2]-1 ์— ํ•ด๋‹นํ•˜๋Š” ์œ„์น˜์˜ sorting ๊ฐ’์„ ์ถ”์ถœํ•ด์„œ int[] answer์— ์ง‘์–ด ๋„ฃ์–ด์„œ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋!

ํ’€์ด 2์—์„œ๋Š” for ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ง์ ‘ ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  Arrays.copyOfRange()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค. for ๋ฐ˜๋ณต๋ฌธ์„ ์“ฐ์ง€ ์•Š์œผ๋‹ˆ ์ฝ”๋“œ ์„ฑ๋Šฅ์ด ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š์„๊นŒ ๊ธฐ๋Œ€ํ–ˆ๋Š”๋ฐ ์ƒ๊ฐ๋ณด๋‹ค๋Š” ์œ ์˜๋ฏธํ•œ ์ฐจ์ด๊ฐ€ ์žˆ์ง„ ์•Š์•˜๋‹ค.

 

 

์บก์ณ๋ฅผ ๊นœ๋นกํ–ˆ๋‹ค..๐Ÿ˜๐Ÿ’ฆ 9069์œ„!

 

 

 

 

๋Œ“๊ธ€