-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_42862.java
More file actions
66 lines (50 loc) · 1.53 KB
/
_42862.java
File metadata and controls
66 lines (50 loc) · 1.53 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.*;
import java.util.stream.*;
class Solution {
private int n;
private int[] arr;
public int solution(int n, int[] lost, int[] reserve) {
init(n, lost, reserve);
// printStatus();
borrow();
// printStatus();
return countLost();
}
private void init(int n, int[] lost, int[] reserve) {
this.n = n;
arr = new int[n];
for (int lostIndex : lost) {
arr[lostIndex - 1]--;
}
for (int reserveIndex : reserve) {
arr[reserveIndex - 1]++;
}
}
private void borrow() {
for (int lostIndex = 0; lostIndex < n; lostIndex++) {
// 이미 체육복을 가지고 있음
if (arr[lostIndex] >= 0) {
continue;
}
// 왼쪽에서 빌려옴
int left = lostIndex - 1;
if (left >= 0 && arr[left] == 1) {
arr[lostIndex] = 0;
arr[left] = 0;
continue;
}
// 오른쪽에서 빌려옴
int right = lostIndex + 1;
if (right < n && arr[right] == 1) {
arr[lostIndex] = 0;
arr[right] = 0;
continue;
}
}
}
private int countLost() {
return Arrays.stream(arr)
.map(value -> value >= 0 ? 1 : 0)
.reduce(0, (a, b) -> a + b);
}
}