Skip to content

Commit 3465583

Browse files
authored
Merge pull request #1378 from AlgorithmWithGod/ksinji
[20251111] PGM / LV4 / 도둑질 / 강신지
2 parents f09ae8e + cad609f commit 3465583

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

ksinji/11 PGM 도둑질.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
```java
2+
class Solution {
3+
public int solution(int[] money) {
4+
int n = money.length;
5+
6+
int A0 = 0;
7+
int A1 = 0;
8+
9+
int B0 = 0;
10+
int B1 = money[0];
11+
12+
for (int i = 1; i < n; i++) {
13+
int newA0 = Math.max(A0, A1);
14+
int newA1 = A0 + money[i];
15+
A0 = newA0; A1 = newA1;
16+
17+
if (i <= n - 2) {
18+
int newB0 = Math.max(B0, B1);
19+
int newB1 = B0 + money[i];
20+
B0 = newB0; B1 = newB1;
21+
}
22+
}
23+
24+
int bestA = Math.max(A0, A1);
25+
int bestB = Math.max(B0, B1);
26+
return Math.max(bestA, bestB);
27+
}
28+
}
29+
```

0 commit comments

Comments
 (0)