Skip to content

Commit cc1f2e3

Browse files
authored
Merge pull request #30 from Jewan1120/main
[3์ฃผ์ฐจ] ๋ฐฑ์ œ์™„
2 parents 38550d4 + 17a8a07 commit cc1f2e3

8 files changed

+430
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.ArrayDeque;
2+
import java.util.Deque;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws Exception {
7+
int n = read(), w = read(), l = read();
8+
int[] trucks = new int[n];
9+
for (int i = 0; i < n; i++)
10+
trucks[i] = read();
11+
// ํŠธ๋Ÿญ์˜ ์›€์ง์ž„์„ ํ๋กœ ๊ตฌํ˜„
12+
Deque<Integer> dq = new ArrayDeque<>();
13+
// ๋‹ค๋ฆฌ์˜ ๊ธธ์ด๋งŒํผ ๋นˆ ํŠธ๋Ÿญ์„ ๋„ฃ์–ด์คŒ
14+
for (int i = 0; i < w; i++)
15+
dq.offer(0);
16+
int time = 0, sumWeight = 0, truckIdx = 0; // ํ˜„์žฌ ์‹œ๊ฐ„, ๋‹ค๋ฆฌ ์œ„ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ ์ดํ•ฉ, ํŠธ๋Ÿญ์˜ ์ธ๋ฑ์Šค
17+
while (!dq.isEmpty()) {
18+
time++; // ์‹œ๊ฐ„ ์ฆ๊ฐ€
19+
sumWeight -= dq.poll(); // ํŠธ๋Ÿญ์„ ๋‹ค๋ฆฌ์—์„œ ๊บผ๋ƒ„
20+
// ๋„ฃ์–ด์•ผํ•  ํŠธ๋Ÿญ์ด ๋‚จ์•„์žˆ๊ณ 
21+
if (truckIdx < n) {
22+
// ์ตœ๋Œ€ ํ•˜์ค‘์„ ๋„˜์ง€์•Š๋Š”๋‹ค๋ฉด ํŠธ๋Ÿญ ์‚ฝ์ž…
23+
if (l >= sumWeight + trucks[truckIdx]) {
24+
dq.offer(trucks[truckIdx]);
25+
sumWeight += trucks[truckIdx];
26+
truckIdx++;
27+
} else
28+
dq.offer(0); // ํŠธ๋Ÿญ๊ณผ ํŠธ๋Ÿญ ์‚ฌ์ด์˜ ๋นˆ ๊ณต๊ฐ„์„ ์œ ์ง€
29+
}
30+
}
31+
System.out.println(time);
32+
}
33+
34+
// ๋น ๋ฅธ ์ž…๋ ฅ ํ•จ์ˆ˜
35+
private static int read() throws Exception {
36+
int c, n = System.in.read() & 15;
37+
while ((c = System.in.read()) >= 48)
38+
n = (n << 3) + (n << 1) + (c & 15);
39+
if (c == 13)
40+
System.in.read();
41+
return n;
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Main {
2+
3+
public static void main(String[] args) throws Exception {
4+
int t = read();
5+
int[] dp = new int[10_001];
6+
dp[0] = 1;
7+
// ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ˜„์žฌ ๊ฐ’์—์„œ -1, -2, -3์˜ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๊ฐ’์„ ๋”ํ•ด์คŒ
8+
for (int i = 1; i <= 3; i++)
9+
for (int j = i; j < 10_001; j++)
10+
dp[j] += dp[j - i];
11+
StringBuilder sb = new StringBuilder();
12+
while (t-- > 0) {
13+
int n = read();
14+
sb.append(dp[n]).append("\n");
15+
}
16+
System.out.println(sb);
17+
}
18+
19+
private static int read() throws Exception {
20+
int c, n = System.in.read() & 15;
21+
while ((c = System.in.read()) >= 48)
22+
n = (n << 3) + (n << 1) + (c & 15);
23+
if (c == 13)
24+
System.in.read();
25+
return n;
26+
}
27+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.ArrayDeque;
2+
import java.util.Arrays;
3+
import java.util.Deque;
4+
5+
public class Main {
6+
7+
// ๋‚˜์ดํŠธ์˜ ์›€์ง์ž„
8+
static int[] dx = { -1, -2, -2, -1, 1, 2, 2, 1 };
9+
static int[] dy = { -2, -1, 1, 2, 2, 1, -1, -2 };
10+
static int n;
11+
12+
public static void main(String[] args) throws Exception {
13+
n = read();
14+
int m = read();
15+
int x = read(), y = read(); // ์ดˆ๊ธฐ ๋‚˜์ดํŠธ
16+
int[][] EArr = new int[m][2]; // ์ƒ๋Œ€ ๋ง์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
17+
for (int i = 0; i < m; i++) {
18+
int A = read(), B = read();
19+
EArr[i] = new int[] { A, B }; // ์ƒ๋Œ€ ๋ง์˜ ์œ„์น˜ ์ €์žฅ
20+
}
21+
int[][] board = new int[n + 1][n + 1]; // ๊ฐ ์ขŒํ‘œ์˜ ์ตœ์†Œ ์‹œ๊ฐ„์„ ์ €์žฅํ•  ๋ฐฐ์—ด
22+
for (int i = 0; i < n + 1; i++)
23+
Arrays.fill(board[i], -1); // ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฅผ ์œ„ํ•ด -1๋กœ ์ดˆ๊ธฐํ™”
24+
// BFS
25+
Deque<int[]> dq = new ArrayDeque<>();
26+
dq.offer(new int[] { x, y });
27+
board[x][y] = 0; // ์ดˆ๊ธฐ ๋ฐฉ๋ฌธ์ฒดํฌ
28+
while (!dq.isEmpty()) {
29+
int[] cur = dq.poll(); // ํ˜„์žฌ ๋‚˜์ดํŠธ์˜ ์œ„์น˜
30+
for (int i = 0; i < 8; i++) {
31+
int nx = cur[0] + dx[i];
32+
int ny = cur[1] + dy[i];
33+
// ๋‹ค์Œ ์œ„์น˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
34+
if (isValid(nx, ny) && board[nx][ny] == -1) {
35+
dq.offer(new int[] { nx, ny });
36+
board[nx][ny] = board[cur[0]][cur[1]] + 1; // ์ด์ „ ์œ„์น˜์˜ ์ตœ์†Œ ์‹œ๊ฐ„ + 1
37+
}
38+
}
39+
}
40+
StringBuilder sb = new StringBuilder();
41+
// ์ ๋“ค์˜ ์œ„์น˜๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์ตœ์†Œ ์‹œ๊ฐ„์„ ์ถœ๋ ฅ
42+
for (int[] E : EArr)
43+
sb.append(board[E[0]][E[1]]).append(" ");
44+
System.out.println(sb);
45+
}
46+
47+
// ๊ฒฝ๊ณ„ ์ฒดํฌ
48+
private static boolean isValid(int x, int y) {
49+
return 0 < x && x <= n && 0 < y && y <= n;
50+
}
51+
52+
// ๋น ๋ฅธ ์ž…๋ ฅ ํ•จ์ˆ˜
53+
private static int read() throws Exception {
54+
int c, n = System.in.read() & 15;
55+
while ((c = System.in.read()) >= 48)
56+
n = (n << 3) + (n << 1) + (c & 15);
57+
if (c == 13)
58+
System.in.read();
59+
return n;
60+
}
61+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
public class Main {
2+
3+
static int n, m, h;
4+
static boolean[][] ladders;
5+
6+
public static void main(String[] args) throws Exception {
7+
n = read();
8+
m = read();
9+
h = read();
10+
ladders = new boolean[h][n - 1]; // ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•  ๋ฐฐ์—ด
11+
// ๋ฏธ๋ฆฌ ์„ค์น˜๋˜์–ด ์žˆ๋Š” ์‚ฌ๋‹ค๋ฆฌ ์ž…๋ ฅ
12+
for (int i = 0; i < m; i++) {
13+
int y = read(), x = read();
14+
ladders[y - 1][x - 1] = true;
15+
}
16+
// ์‚ฌ๋‹ค๋ฆฌ๋ฅผ 3๊ฐœ ์ดํ•˜๋กœ ์„ค์น˜ํ•ด์„œ ์˜ฌ๋ฐ”๋ฅธ์ง€ ํ™•์ธ
17+
for (int i = 0; i <= 3; i++) {
18+
if (placeLadder(0, i)) {
19+
System.out.println(i);
20+
return;
21+
}
22+
}
23+
// 3๊ฐœ ์ดˆ๊ณผํ•œ๋‹ค๋ฉด ์‹คํŒจ, -1 ์ถœ๋ ฅ
24+
System.out.println(-1);
25+
}
26+
27+
// ์žฌ๊ท€์  ํ˜ธ์ถœ๋กœ ์‚ฌ๋‹ค๋ฆฌ ์„ค์น˜
28+
// depth : ์„ค์น˜ํ•œ ์‚ฌ๋‹ค๋ฆฌ์˜ ์ˆ˜
29+
// cnt : ์„ค์น˜ํ•ด์•ผ ํ•  ์‚ฌ๋‹ค๋ฆฌ ์ˆ˜
30+
private static boolean placeLadder(int depth, int cnt) {
31+
// ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ๋‹ค ์„ค์น˜๋˜์—ˆ๋‹ค๋ฉด ์›€์ง์ž„
32+
if (depth == cnt)
33+
return simulate();
34+
for (int i = 0; i < h; i++) {
35+
for (int j = 0; j < n - 1; j++) {
36+
// ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋ผ๋ฉด
37+
if (isPossible(i, j)) {
38+
ladders[i][j] = true; // ์‚ฌ๋‹ค๋ฆฌ ์„ค์น˜ ํ›„ ๋‹ค์Œ ๊นŠ์ด์˜ ์žฌ๊ท€ ํ˜ธ์ถœ
39+
if (placeLadder(depth + 1, cnt))
40+
return true;
41+
// ์‚ฌ๋‹ค๋ฆฌ ์„ค์น˜๊ฐ€ ์ž˜๋ชป๋˜์—ˆ๋‹ค๋ฉด
42+
ladders[i][j] = false; // ์‚ฌ๋‹ค๋ฆฌ ํ•ด์ฒด (๋ฐฑํŠธ๋ž˜ํ‚น)
43+
}
44+
}
45+
}
46+
// ์ „๋ถ€ ์‹คํŒจ
47+
return false;
48+
}
49+
50+
// ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธ
51+
private static boolean isPossible(int y, int x) {
52+
// ํ˜„์žฌ ์œ„์น˜๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š๊ณ 
53+
if (ladders[y][x])
54+
return false;
55+
// ์ขŒ, ์šฐ์— ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
56+
if ((x - 1 >= 0 && ladders[y][x - 1]) || (x + 1 < n - 1 && ladders[y][x + 1]))
57+
return false;
58+
return true;
59+
}
60+
61+
// ์›€์ง์ž„ ๊ตฌํ˜„ํ•˜๋Š” ์‹œ๋ฎฌ๋ ˆ์ด์…˜
62+
private static boolean simulate() {
63+
for (int i = 0; i < n - 1; i++) {
64+
int num = i; // ์ดˆ๊ธฐ ์œ„์น˜
65+
// ๋‚ด๋ ค๊ฐ€๋ฉด์„œ ์ขŒ, ์šฐ์— ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ์œ„์น˜ ๋ณ€ํ™˜
66+
for (int j = 0; j < h; j++) {
67+
int left = num - 1, right = num;
68+
// ์ขŒ์ธก์— ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ์™ผ์ชฝ์œผ๋กœ ์ด๋™
69+
if (left >= 0 && ladders[j][left])
70+
num--;
71+
// ์šฐ์ธก์— ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
72+
if (right < n - 1 && ladders[j][right])
73+
num++;
74+
}
75+
// ์ฒ˜์Œ ์œ„์น˜์™€ ๋งˆ์ง€๋ง‰ ์œ„์น˜๊ฐ€ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด
76+
if (num != i)
77+
return false;
78+
}
79+
return true;
80+
}
81+
82+
// ๋น ๋ฅธ ์ž…๋ ฅ์„ ์œ„ํ•œ ํ•จ์ˆ˜
83+
private static int read() throws Exception {
84+
int c, n = System.in.read() & 15;
85+
while ((c = System.in.read()) >= 48)
86+
n = (n << 3) + (n << 1) + (c & 15);
87+
if (c == 13)
88+
System.in.read();
89+
return n;
90+
}
91+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.StringTokenizer;
6+
7+
public class Main {
8+
9+
static int n;
10+
static int[][] board; // ๊ฒฉ์ž ์นธ
11+
static int[][] opers; // ๊ฐ ๋…„๋„์˜ ์ด๋™ ๊ทœ์น™
12+
static boolean[][] supple; // ์˜์–‘์ œ๊ฐ€ ์žˆ๋Š” ์œ„์น˜
13+
14+
// ๋Œ€๊ฐ์„  ์›€์ง์ž„ ๊ตฌํ˜„
15+
static int[] dy = { 0, 0, -1, -1, -1, 0, 1, 1, 1 };
16+
static int[] dx = { 0, 1, 1, 0, -1, -1, -1, 0, 1 };
17+
18+
public static void main(String[] args) throws Exception {
19+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
StringTokenizer st = new StringTokenizer(br.readLine());
21+
n = Integer.parseInt(st.nextToken());
22+
int m = Integer.parseInt(st.nextToken());
23+
board = new int[n][n];
24+
supple = new boolean[n][n];
25+
for (int i = 0; i < n; i++) {
26+
st = new StringTokenizer(br.readLine());
27+
for (int j = 0; j < n; j++)
28+
board[i][j] = Integer.parseInt(st.nextToken());
29+
}
30+
opers = new int[m][2]; // { ๋ฐฉํ–ฅ, ํฌ๊ธฐ }
31+
for (int i = 0; i < m; i++) {
32+
st = new StringTokenizer(br.readLine());
33+
opers[i][0] = Integer.parseInt(st.nextToken());
34+
opers[i][1] = Integer.parseInt(st.nextToken());
35+
}
36+
// ์ฒซ ์˜์–‘์ œ ์œ„์น˜
37+
supple[n - 2][0] = true;
38+
supple[n - 2][1] = true;
39+
supple[n - 1][0] = true;
40+
supple[n - 1][1] = true;
41+
// ๋…„๋„๋งŒํผ ๋ฐ˜๋ณต
42+
for (int year = 0; year < m; year++) {
43+
move(year); // ์˜์–‘์ œ์˜ ์›€์ง์ž„ ๊ตฌํ˜„
44+
grow(); // ์˜์–‘์ œ ์œ„์น˜์˜ ๋‚˜๋ฌด ์„ฑ์žฅ
45+
cut(); // ํฌ๊ธฐ๊ฐ€ 2 ์ด์ƒ์ธ ๋‚˜๋ฌด๋ฅผ ์ž˜๋ผ๋‚ด๊ณ  ์˜์–‘์ œ๋ฅผ ์‹ฌ์Œ
46+
}
47+
// ๋‚จ์•„์žˆ๋Š” ๋‚˜๋ฌด ๋†’์ด๋“ค์˜ ์ด ํ•ฉ
48+
int sum = 0;
49+
for (int i = 0; i < n; i++)
50+
for (int j = 0; j < n; j++)
51+
sum += board[i][j];
52+
System.out.println(sum);
53+
}
54+
55+
// ์˜์–‘์ œ์˜ ์›€์ง์ž„ ๊ตฌํ˜„
56+
private static void move(int year) {
57+
// ํ๋ฅผ ์ด์šฉํ•ด ๊ธฐ์กด ์˜์–‘์ œ์˜ ์œ„์น˜๋ฅผ ์ €์žฅ
58+
Deque<int[]> dq = new ArrayDeque<>();
59+
for (int i = 0; i < n; i++)
60+
for (int j = 0; j < n; j++)
61+
if (supple[i][j]) {
62+
supple[i][j] = false;
63+
dq.offer(new int[] { i, j });
64+
}
65+
// ๋…„๋„์— ๋”ฐ๋ฅธ ์ด๋™ ๋ฐฉํ–ฅ ๋ฐ ํฌ๊ธฐ
66+
int[] oper = opers[year];
67+
while (!dq.isEmpty()) {
68+
int[] cur = dq.poll();
69+
// ์˜์–‘์ œ ์œ„์น˜ ์ด๋™
70+
int y = (cur[0] + dy[oper[0]] * oper[1] + n) % n;
71+
int x = (cur[1] + dx[oper[0]] * oper[1] + n) % n;
72+
supple[y][x] = true; // ์ด๋™ํ•œ ์˜์–‘์ œ ์œ„์น˜ ํ‘œ์‹œ
73+
board[y][x]++; // ์˜์–‘์ œ๊ฐ€ ์žˆ๋Š” ์œ„์น˜์˜ ๋‚˜๋ฌด ์„ฑ์žฅ
74+
}
75+
}
76+
77+
// ์˜์–‘์ œ ์œ„์น˜์˜ ๋‚˜๋ฌด ์„ฑ์žฅ
78+
private static void grow() {
79+
for (int i = 0; i < n; i++)
80+
for (int j = 0; j < n; j++)
81+
// ์˜์–‘์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด
82+
if (supple[i][j]) {
83+
int cnt = 0;
84+
for (int k = 1; k <= 4; k++) {
85+
// ๋Œ€๊ฐ์„  ๋ฒกํ„ฐ๊ฐ’
86+
int y = i + dy[k * 2];
87+
int x = j + dx[k * 2];
88+
if (isValid(y, x) && board[y][x] > 0)
89+
cnt++;
90+
}
91+
// ๋Œ€๊ฐ์„ ์— ์žˆ๋Š” ๋‚˜๋ฌด์˜ ์ˆ˜ ๋งŒํผ ์„ฑ์žฅ
92+
board[i][j] += cnt;
93+
}
94+
}
95+
96+
// ํฌ๊ธฐ๊ฐ€ 2 ์ด์ƒ์ธ ๋‚˜๋ฌด๋ฅผ ์ž˜๋ผ๋‚ด๊ณ  ์˜์–‘์ œ๋ฅผ ์‹ฌ์Œ
97+
private static void cut() {
98+
for (int i = 0; i < n; i++)
99+
for (int j = 0; j < n; j++)
100+
// ์˜์–‘์ œ๊ฐ€ ์—†์—ˆ๋˜ ์ขŒํ‘œ์—์„œ ํฌ๊ธฐ๊ฐ€ 2 ์ด์ƒ์ธ ๋‚˜๋ฌด
101+
if (!supple[i][j]) {
102+
if (board[i][j] >= 2) {
103+
board[i][j] -= 2;
104+
supple[i][j] = true;
105+
}
106+
} else {
107+
// ๊ธฐ์กด์— ์žˆ๋˜ ์˜์–‘์ œ๋Š” ์—†์• ์คŒ
108+
supple[i][j] = false;
109+
}
110+
}
111+
112+
private static boolean isValid(int y, int x) {
113+
return 0 <= y && y < n && 0 <= x && x < n;
114+
}
115+
}

โ€ŽProgrammers/Level4/JW_43236.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Arrays;
2+
class Solution {
3+
public int solution(int distance, int[] rocks, int n) {
4+
int answer = 0;
5+
Arrays.sort(rocks); // ์ด๋ถ„ ํƒ์ƒ‰์„ ์œ„ํ•œ ์ •๋ ฌ
6+
int l = 1, r = distance; // ์ด๋ถ„ ํƒ์ƒ‰์˜ ์ตœ์†Œ, ์ตœ๋Œ€ ๊ฒฝ๊ณ„ ์„ค์ •
7+
// ์ด๋ถ„ ํƒ์ƒ‰์„ ํ†ตํ•ด ์ตœ์ ์˜ ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์Œ
8+
while (l <= r) {
9+
int m = (l + r) / 2; // ๊ฐ ๋ฐ”์œ„ ์‚ฌ์ด์˜ ์ตœ์†Œ ๊ฑฐ๋ฆฌ
10+
// ํ•ด๋‹น ๊ฑฐ๋ฆฌ๋กœ ์ง€์šธ ์ˆ˜ ์žˆ๋Š” ๋ฐ”์œ„ ์ˆ˜ ๊ณ„์‚ฐ
11+
if (isPossible(distance, rocks, m, n)) {
12+
answer = m; // ๊ฐ€๋Šฅํ•˜๋ฉด ๊ทธ ๊ฑฐ๋ฆฌ๋ฅผ ์ •๋‹ต์œผ๋กœ ์ €์žฅ
13+
l = m + 1; // ๋‹ค์Œ ํƒ์ƒ‰(์ตœ์†Œ ๊ฑฐ๋ฆฌ ์ค‘ ์ตœ๋Œ“๊ฐ’)์„ ์œ„ํ•ด ์™ผ์ชฝ ๊ฒฝ๊ณ„ ์žฌ์„ค์ •
14+
} else
15+
r = m - 1; // ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด ๋” ์ž‘์€ ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ฅผ ํƒ์ƒ‰
16+
}
17+
return answer;
18+
}
19+
20+
// ํ•ด๋‹น target์˜ ๊ธธ์ด๋กœ ์—†์•จ ์ˆ˜ ์žˆ๋Š” ๋ฐ”์œ„์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
21+
private boolean isPossible(int distance, int[] rocks, int target, int n) {
22+
int now = 0; // ์‹œ์ž‘ ๋ฐ”์œ„
23+
int removed = 0; // ์ œ๊ฑฐ๋œ ๋ฐ”์œ„ ์ˆ˜
24+
// ๋‘ ๋ฐ”์œ„ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๊ฐ€ target๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์—†์•ฐ
25+
for (int i = 0; i < rocks.length; i++) {
26+
if (rocks[i] < now + target)
27+
removed++;
28+
else
29+
now = rocks[i]; // ๊ฑฐ๋ฆฌ๊ฐ€ ์ถฉ๋ถ„ํ•˜๋ฉด ํ˜„์žฌ ๋ฐ”์œ„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐฑ์‹ 
30+
}
31+
// ๋งˆ์ง€๋ง‰ ๋ฐ”์œ„(distance)๊นŒ์ง€ ๊ฑฐ๋ฆฌ๋„ ํ™•์ธ
32+
if (distance < now + target)
33+
removed++;
34+
return removed <= n; // ํ˜„์žฌ ๊ฑฐ๋ฆฌ๋กœ ๊ฐ€๋Šฅํ–ˆ๋Š”์ง€ ๋ฐ˜ํ™˜
35+
// ๋ถ€๋“ฑํ˜ธ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ์ด์œ ๋Š” ์•„๋ฌด๊ฑฐ๋‚˜ ์ง€์›Œ์„œ n๊ฐœ๋ฅผ ๋งž์ถœ ์ˆ˜ ์žˆ์–ด์„œ ์ƒ๊ด€์—†์Œ.
36+
}
37+
}

0 commit comments

Comments
ย (0)