๋ฐ์ํ
๐ ๋ฌธ์ URL ๐
ํ์๋ฒ ๋ฌธ์ !
ํ์๋ฒ์ด๋ ๋งค ์ ํ์์ ์ต์ ์ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด๋ด๋ ๊ฒ.
import java.util.Arrays;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int answer = n;
int[] student = new int[n];
//์ผ๋จ ๋ชจ๋ ์ฒด์ก๋ณต์ ๊ฐ์ ธ์จ ๊ฒ์ผ๋ก ์ธํ
(1,1,1,1,1)
Arrays.fill(student, 1);
Arrays.sort(lost);
Arrays.sort(reserve);
//์์ด๋ฒ๋ฆฐ ํ์์ -1 ์ฒ๋ฆฌ (1,0,1,0,1)
for (int i : lost) {
student[i - 1] -= 1;
}
//์ฌ๋ฒ์ ๊ฐ์ ธ์จ ํ์์ +1 ์ฒ๋ฆฌ (2,0,2,0,2)
for (int i : reserve) {
student[i - 1] += 1;
}
for (int i = 0; i < student.length; i++) {
if (student[i] == 0) {
//์ฒซ๋ฒ์งธ์ผ ๋๋ ์ค๋ฅธ์ชฝ๋ง ๊ฒ์ฌ
if(i==0){
if(student[i+1] == 2){
student[i+1] -= 1;
student[i] = 1;
}
//๋ง์ง๋ง์ผ ๋๋ ์ผ์ชฝ๋ง ๊ฒ์ฌ
}else if(i==n-1){
if(student[i-1] == 2){
student[i-1] -= 1;
student[i] = 1;
}
}else{
//์์ชฝ ๋ค ๊ฒ์ฌ, ๋จผ์ ์์ ์ ์ผ์ชฝ๋ถํฐ ๊ฒ์ฌ
if(student[i-1] == 2){
student[i-1] -= 1;
student[i] = 1;
}
else if(student[i+1] == 2){
student[i+1] -= 1;
student[i] = 1;
}
}
}
}
for(int s : student){
if( s == 0){
answer -= 1;
}
}
return answer;
}
}
728x90
๋ฐ์ํ
'๊ฐ๋ฐ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ํคํจ๋ ๋๋ฅด๊ธฐ (0) | 2022.07.25 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ ๊ท ์์ด๋ ์ถ์ฒ (0) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (0) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ (1) | 2022.07.24 |
๋๊ธ