๐ช๐ป programmers ๊ณ ๋์ Kit - ์ ๋ ฌ
K๋ฒ์งธ ์ - ํ๋ก๊ทธ๋๋จธ์ค
๋ฐฐ์ด array์ i๋ฒ์งธ ์ซ์๋ถํฐ j๋ฒ์งธ ์ซ์๊น์ง ์๋ฅด๊ณ ์ ๋ ฌํ์ ๋, k๋ฒ์งธ์ ์๋ ์๋ฅผ ๊ตฌํ๊ธฐ
- ์ ์ํ ๋ฐฐ์ด
arr
๋ฅผ ๋ง๋ค์ด์Arrays.copyOfRange
๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌcommands[i][0]-1
๋ถํฐcommands[i][1]
๊น์ง์ ์๋ฅผ ๊ตฌํ๋ค.
์ฌ๊ธฐ์ commands[i][0]-1
๋ฅผ ํ๋ ์ด์ ๋ ํด๋น ๋ฌธ์ ๋ 1๋ถํฐ ์์ํ์ง๋ง Arrays.copyOfRange
๋ฉ์๋๋ 0๋ถํฐ์ด๊ธฐ ๋๋ฌธ์ 1์ ๋นผ์ฃผ์ด ๋ง์ถฐ์ฃผ๋ ๊ฒ์ด๋ค.
์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๋ค.
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;
}
}
๊ฐ์ฅ ํฐ ์ - ํ๋ก๊ทธ๋๋จธ์ค
์ ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ๊ธฐ
๋ ๋ฌธ์์ด์ ์กฐํฉํ์ฌ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ (ํฐ ์ซ์๋ฅผ ์์ ์ค๊ฒ ํจ)
๋ฌธ์์ด ๋ํ๊ธฐ
๊ฒฐ๊ณผ๊ฐ 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 - ํ๋ก๊ทธ๋๋จธ์ค
์ค๋ฆ์ฐจ์ ์ ๋ ฌํ์ฌ ๋ฎ์ ์๋ถํฐ ์ ๋ ฌ๋๊ฒ ํ๋ค.
h ๋ณ์๋ฅผ ์ ์ธํ์ฌ ๋จ์์๋ ๋ ผ๋ฌธ์ ์๋ฅผ ๊ณ์ฐํ๋ค.
๋ ผ๋ฌธ์ ์ธ์ฉํ์๊ฐ 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;
}
}