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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž ๋ฐฐ์—ด | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

array์˜ ๊ฐ element ์ค‘ divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” element๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์— -1์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”. ์ œํ•œ์‚ฌํ•ญ arr์€ ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ์ •์ˆ˜ i, j์— ๋Œ€ํ•ด i ≠ j ์ด๋ฉด arr[i] ≠ arr[j] ์ž…๋‹ˆ๋‹ค. divisor๋Š” ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค. array๋Š” ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ arr divi

programmers.co.kr

 

 

 

< ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž ๋ฐฐ์—ด >

 

 

 

 

 

โ—‹ ์ฒ˜์Œ ํ‘ผ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Arrays;
class Solution {
  public int[] solution(int[] arr, int divisor) {
      int[] temp2;
      int[] temp = new int[arr.length];
      int k=0;
      //n์œผ๋กœ ๋‚˜๋ˆ ์ง€๋Š” ๊ฒƒ์„ temp์— ์ €์žฅ
      for(int i=0; i<arr.length; i++){
          if(arr[i]%divisor==0){  
              temp[k]=arr[i]; 
              k++;  }
      }
      //k=0์ผ ๋•Œ 0 ์•„๋‹ ๋•Œ๋กœ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌ
      if(k==0){
          temp2 =new int[1];
          temp2[0]=-1;
      }else{
          temp2 =Arrays.copyOfRange(temp,0,k);
          Arrays.sort(temp2);
      }
      return temp2;
  }
}

 

โ—‹ ์ƒ๊ฐ๊ณผ์ •

1.divisor๋กœ ๋‚˜๋ˆ„์–ด์ง€๋Š” ์ˆ˜๋ฅผ temp ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค

2.temp์˜ ๊ธธ์ด๋ฅผ arr.length๋กœ ํ•ด์„œ ๋‹ด๊ธฐ์ง€ ์•Š์€ ์ชฝ์ด 0์œผ๋กœ ์ดˆ๊ธฐํ™” ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌ์‹œ์— 0๋„ ์ •๋ ฌ ๋˜๊ธฐ ๋•Œ๋ฌธ์— K๋กœ ๊ฐ’์ด ๋„ฃ์–ด ์งˆ๋•Œ     ๋งˆ๋‹ค ์นด์šดํŠธ ํ•ด์„œ k๊ฐ’์œผ๋กœ temp2์— ๋ณต์‚ฌํ•ด์ค€๋‹ค.

3.Arrays.sort() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ •๋ ฌํ•œ๋‹ค.

 

โ—‹ ๊ฐ™์ด ๋ณด๊ณ ์‹ถ์€ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Arrays;
 
class Divisible {
    public int[] divisible(int[] array, int divisor) {
        //ret์— array์— ํฌํ•จ๋œ ์ •์ˆ˜์ค‘, divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์œผ์„ธ์š”.
        return Arrays.stream(array).filter(factor -> factor % divisor == 0).toArray();
    }
    // ์•„๋ž˜๋Š” ํ…Œ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•ด ๋ณด๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
    public static void main(String[] args) {
        Divisible div = new Divisible();
        int[] array = {59710};
        System.out.println( Arrays.toString( div.divisible(array, 5) ));
    }
}
cs

 

โ—‹ ๋‹ค์‹œ ํ‘ผ ํ’€์ด

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
import java.util.Arrays;
class Solution {
  public int[] solution(int[] arr, int divisor) {
        int[] temp =new int[arr.length];
        int[] answer;
        int cnt=0;
      for(int i =0; i<arr.length;i++){
          if(arr[i]%divisor==0){
            temp[cnt]=arr[i]; 
              cnt++;
          }
      }     
      
      if(cnt==0){
          answer =new int[1];
          answer[0]=-1;
          
      }else { 
           answer =new int[cnt];
          for(int i=0; i<temp.length; i++){
            if(temp[i]!=0)
              answer[i]=temp[i];
            }
      }
      Arrays.sort(answer);
   
      return answer;
 
  }
}
cs

 

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