https://programmers.co.kr/learn/courses/30/lessons/42748
< 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 |
'JAVA > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฌธ์์ด ๋ด๋ฆผ์ฐจ์์ผ๋ก ๋ฐฐ์นํ๊ธฐ/java/level1/programmers (0) | 2020.02.17 |
---|---|
์์ ์ํธ/ํ๋ก๊ทธ๋๋จธ์ค/level1 (0) | 2020.02.12 |
ํ๋ ฌ ๋ง์ (0) | 2020.02.10 |
ํ๊ท ๊ตฌํ๊ธฐ/java/level1/ํ๋ก๊ทธ๋๋จธ์ค (0) | 2020.02.10 |
[ Java / Level 1] ์ฝ์์ ํฉ /programmers (0) | 2020.02.10 |