Post

๐Ÿ’ช๐Ÿป programmers ๊ณ ๋“์  Kit - ์ •๋ ฌ

K๋ฒˆ์งธ ์ˆ˜ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

๋ฐฐ์—ด array์˜ i๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆซ์ž๊นŒ์ง€ ์ž๋ฅด๊ณ  ์ •๋ ฌํ–ˆ์„ ๋•Œ, k๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ

  1. ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด arr๋ฅผ ๋งŒ๋“ค์–ด์„œ Arrays.copyOfRange ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ commands[i][0]-1๋ถ€ํ„ฐ commands[i][1]๊นŒ์ง€์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ commands[i][0]-1 ๋ฅผ ํ•˜๋Š” ์ด์œ ๋Š” ํ•ด๋‹น ๋ฌธ์ œ๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€๋งŒ Arrays.copyOfRange ๋ฉ”์„œ๋“œ๋Š” 0๋ถ€ํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— 1์„ ๋นผ์ฃผ์–ด ๋งž์ถฐ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

  1. ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.

  2. k๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด commands[i][2]-1๋ฅผ ํ•œ๋‹ค.

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


๊ฐ€์žฅ ํฐ ์ˆ˜ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

  1. ์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

  2. ๋‘ ๋ฌธ์ž์—ด์„ ์กฐํ•ฉํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ (ํฐ ์ˆซ์ž๋ฅผ ์•ž์— ์˜ค๊ฒŒ ํ•จ)

  3. ๋ฌธ์ž์—ด ๋”ํ•˜๊ธฐ

  4. ๊ฒฐ๊ณผ๊ฐ€ 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋ฉด 0 ๋ฐ˜ํ™˜. -> ์ตœ๋Œ€๊ฐ’์ด 0์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๊ฐ’์ด 0์ผ ๊ฒƒ.

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.*;
class Solution {
    public String solution(int[] numbers) {
        String answer = "";
        String[] str = new String[numbers.length];
        
        for(int i=0;i<numbers.length;i++)
        {
            // ์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
            str[i] = String.valueOf(numbers[i]);
        }
        
        // 6+10 = 610๊ณผ 10+6 = 106 ๋น„๊ต
        Arrays.sort(str,(o1,o2)-> (o2+o1).compareTo(o1+o2));
        
        // ๋ฌธ์ž์—ด ๋ถ™์ด๊ธฐ
        for(int i=0;i<str.length;i++)
        {
            answer+=str[i];
        }
        
        // ๊ฒฐ๊ณผ๊ฐ€ "0"์œผ๋กœ ์‹œ์ž‘ํ•˜๋ฉด "0" ๋ฐ˜ํ™˜
        if(answer.charAt(0)=='0')
        {
            answer = "0";
        }
        
        return answer;
    }
}


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

  1. ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์—ฌ ๋‚ฎ์€ ์ˆ˜๋ถ€ํ„ฐ ์ •๋ ฌ๋˜๊ฒŒ ํ•œ๋‹ค.

  2. h ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ ๋‚จ์•„์žˆ๋Š” ๋…ผ๋ฌธ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

  3. ๋…ผ๋ฌธ์˜ ์ธ์šฉํšŸ์ˆ˜๊ฐ€ h ์ด์ƒ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;
class Solution {
    public int solution(int[] citations) {
        int answer = 0;
        
        Arrays.sort(citations);
        
        for(int i=0;i<citations.length;i++)
        {
            int h = citations.length-i;
            
            if(citations[i] >= h)
            {
                answer = h;
                break;
            }
        }
        
        return answer;
    }
}
This post is licensed under CC BY 4.0 by the author.