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

< K๋ฒˆ์งธ์ˆ˜ >

 

 

 

โ—‹ ์‹œํ–‰์ฐฉ์˜ค

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];//commands์˜ i๊ฐ’ ๋งŒํผ ๋งŒ๋“ค๊ธฐ
        int temp=0;
        int count=0;
        
        for(int i=0 ; i<commands.length; i++){
            for(int j=commands[i][0]-1 ; j< commands[i][1]-1;j++){
                 if(array[commands[i][0]-1]>array[commands[i][0]]){
                        temp=array[commands[i][0]];
                        array[commands[i][0]]=array[commands[i][0]-1];
                        array[commands[i][0]-1]=temp;
                            count++;
                    }             
            }if(count==0){
                
                answer[i]=array[commands[i][2]];
                break;
            }
        }
        return answer;
    }
}

1.์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ์ œ๋Œ€๋กœ ํ™•์ธํ•˜์ง€ ์•Š์•˜๋‹ค.
2.i์™€ j๊ฐ€ ๊ฐ™์€ ๋•Œ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š์•˜๋‹ค.
3."์ž๋ฅธํ›„ ์ •๋ ฌ" ์ž๋ฅด์ง€์•Š๊ณ  ๊ทธ๋Œ€๋กœ ์ธ๋ฑ์Šค ๊ฐ’๋งŒ ํ•ด์ฃผ๋ฉด ๋ฐฐ์—ด์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๋ฒˆ์งธ ์ •๋ ฌ ๋˜์žˆ์„๋•Œ ์ฒ˜์Œ ๋ฐฐ์—ด ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ๋ฌด์กฐ๊ฑด ์ž˜๋ผ์„œ ํ•ด์•ผํ•œ๋‹ค.

 

โ—‹ ํ…Œ์ŠคํŠธ๋Š” ์„ฑ๊ณตํ•˜์ง€๋งŒ ํ’€์ด ์ œ์ถœ์‹œ ์‹คํŒจํ•œ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        int temp=0;
        int count=0;
        int[][] arr1= new int[commands.length][array.length];
        
        //์ž๋ฅด๋ฉด์„œ ๋ฐฐ์—ด ๋ณต์‚ฌ
        for(int i=0 ;i<commands.length; i++){
            for(int j=commands[i][0]-1, k=0;j<=commands[i][1]-1; j++,k++)
                arr1[i][k]=array[j];
        }
        //๋ฒ„๋ธ”์ •๋ ฌ
        for(int i=0 ; i<commands.length; i++){
            if(commands[i][0]==commands[i][1]){
                answer[i]=arr1[i][commands[i][2]-1];
                continue;
                }
            for(int k=0; k<commands[i][1]-2; k++){
            for(int j=0 ; j<commands[i][1]-2;j++){
                 if(arr1[i][j]>arr1[i][j+1]){
                        temp=arr1[i][j];
                       arr1[i][j]=arr1[i][j+1];
                        arr1[i][j+1]=temp;
                               
                    } 
                }
            }
            answer[i]=arr1[i][commands[i][2]-1];
        }
        return answer;
    }
}

 

1.ํ…Œ์ŠคํŠธ๋Š” ์„ฑ๊ณตํ–ˆ๋Š”๋ฐ ์ฝ”๋“œ ์ œ์ถœํ–ˆ์„ ๋•Œ ํ•œ๊ฐœ ๋นผ๊ณ  ๋ชจ๋‘ ์‹คํŒจํ–ˆ๋‹ค. sort ํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์‹œ ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

 ---------------------------------------------------- ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ ์„ฑ๊ณตํ•œ ํ’€์ด  --------------------------------------------------

โ—‹ ๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด๋ฅผ ๋ณด๊ณ  ํ‘ผ ํ’€์ด( sort() , copyOfRange() ์‚ฌ์šฉ)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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[] temp= Arrays.copyOfRange(array,commands[i][0]-1,commands[i][1]);
            Arrays.sort(temp);
            answer[i]=temp[commands[i][2]-1];
        }
        
        return answer;
    }
}

- Arrays.sort(๋ฐฐ์—ด) : ๋ฐฐ์—ด์•ˆ์— ์žˆ๋Š” ๋ฌธ์ž์—ด, ์˜๋ฌธ์ž , ์ˆซ์ž ๋ชจ๋‘ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ•ด์ค€๋‹ค.
- Arrays.copyOfRange(๋ฐฐ์—ด์ด๋ฆ„,์‹œ์ž‘,๋) : ๋ฐฐ์—ด์˜ ์‹œ์ž‘๊ณผ ๋์„ ์ •ํ•ด์„œ ๋ฐฐ์—ด๋กœ ๋ณต์‚ฌํ•ด์ค€๋‹ค.

 

โ—‹ sort() ํ•จ์ˆ˜๋งŒ ์‚ฌ์šฉํ•œ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 finish= commands[i][1]-1;
            int pick=commands[i][2]-1;
            int temp[]=new int [finish-start+1];//4-1=3+1=4 -> 0 1 2 3
            
            //๋ฐฐ์—ด ๋ณต์‚ฌ
            for(int j=start,k=0; j<=finish; j++,k++)//j=1 2 3 4 k=0 1 2 3
                temp[k]=array[j];                
            
            //์ •๋ ฌ                   
            Arrays.sort(temp);
            answer[i]=temp[pick];
        }
        return answer;
    }
 }
 
 

 

โ—‹sort()ํ•จ์ˆ˜ , copyOfRange()ํ•จ์ˆ˜ ์—†์ด ํ‘ผ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 finish= commands[i][1]-1;
            int pick=commands[i][2]-1;
            int temp[]=new int [finish-start+1];//4-1=3+1=4 -> 0 1 2 3
            
            //๋ฐฐ์—ด ๋ณต์‚ฌ
            for(int j=start,k=0; j<=finish; j++,k++)//j=1 2 3 4 k=0 1 2 3
                temp[k]=array[j];                
            
            //ํ–ฅ์ƒ๋œ ๋ฒ„๋ธ”์†ŒํŠธ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌ
            int count=0;
            for(int j=0; j<temp.length-1;j++){
                for(int k=0;k<temp.length-1; k++){//0 1 2 
                    int swap;
                    if(temp[k]>temp[k+1]){
                        swap=temp[k];
                        temp[k]=temp[k+1];
                        temp[k+1]=swap;
                        count++;
                    }
                }if(count==0)   //swap์ด ๋”์ด์ƒ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค๋ฉด break
                    break;
            }
            //์ •๋ ฌ์ดํ›„ answer์— ๋„ฃ๊ธฐ
            answer[i]=temp[pick];
        }
        return answer;
    }
 }
 
 
 
cs

+ Recent posts