-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c316a71
commit 6b1aa4d
Showing
3 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package test.boj.dp; | ||
|
||
import org.junit.*; | ||
|
||
import java.io.*; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class o_계단_오르기 { | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
|
||
int n = Integer.parseInt(br.readLine()); | ||
int[] stairPoints = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
stairPoints[i] = Integer.parseInt(br.readLine()); | ||
} | ||
|
||
Stairs stairs = new Stairs(n, stairPoints); | ||
bw.write(String.valueOf(stairs.climb())); | ||
|
||
bw.flush(); | ||
bw.close(); | ||
br.close(); | ||
} | ||
|
||
|
||
@Test | ||
public void test() { | ||
Stairs stairs = new Stairs(6, new int[]{10, 20, 15, 25, 10, 20}); | ||
assertThat(stairs.climb()) | ||
.isEqualTo(75); | ||
} | ||
|
||
private static class Stairs { | ||
|
||
private static final int MAX_COUNT = 300; | ||
|
||
private final int[] stairPoints; | ||
|
||
private final int[] dp; | ||
|
||
public Stairs(int n, int[] stairPoints) { | ||
this.stairPoints = new int[n + 1]; | ||
this.dp = new int[MAX_COUNT + 1]; | ||
for (int i = 1; i <= n; i++) { | ||
this.stairPoints[i] = stairPoints[i - 1]; | ||
} | ||
} | ||
|
||
public int climb() { | ||
dp[1] = this.stairPoints[1]; | ||
dp[2] = dp[1] + this.stairPoints[2]; | ||
|
||
for (int i = 3; i < this.stairPoints.length; i++) { | ||
dp[i] = Math.max(climbOneStair(i), climbTwoStair(i)); | ||
} | ||
|
||
return dp[this.stairPoints.length - 1]; | ||
} | ||
|
||
// 두 칸을 오르는 경우: 두 칸을 오르는 경우에는 3칸 연속 법칙이 성립할 수가 없음 | ||
// 그러므로 두번 째 이전 칸에서의 최대 포인트과 현재 칸의 포인트를 더함 | ||
private int climbTwoStair(int pos) { | ||
if (pos < 3) throw new IllegalArgumentException(); | ||
return this.stairPoints[pos] + this.dp[pos - 2]; | ||
} | ||
|
||
// 한 칸을 오르는 경우: 3칸 연속 법칙 때문에 두번 째 이전 칸에 있는 계단과는 함께 오를 수 없음 | ||
// 그러므로 현재 칸의 포인트와 이전 칸의 포인트를 더한 뒤 | ||
// 세번 째 이전 칸에서의 최대 포인트와 더함 | ||
private int climbOneStair(int pos) { | ||
if (pos < 3) throw new IllegalArgumentException(); | ||
return this.stairPoints[pos] + this.stairPoints[pos - 1] + this.dp[pos - 3]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package test.boj.dp; | ||
|
||
import org.junit.*; | ||
|
||
import java.io.*; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
// https://cheolhojung.github.io/posts/algorithm/boj- sum-of-squares.html | ||
public class p_제곱수의_합 { | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
|
||
bw.write(String.valueOf(solution2(Integer.parseInt(br.readLine())))); | ||
|
||
bw.flush(); | ||
bw.close(); | ||
br.close(); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
// assertThat(solution2(7)) | ||
// .isEqualTo(4); | ||
// assertThat(solution2(11)) | ||
// .isEqualTo(3); | ||
// assertThat(solution2(12)) | ||
// .isEqualTo(3); | ||
// assertThat(solution2(100_000)) | ||
// .isEqualTo(2); | ||
// assertThat(solution2(10_000)) | ||
// .isEqualTo(1); | ||
assertThat(solution2(142)) | ||
.isEqualTo(3); | ||
} | ||
|
||
private static int solution2(int n) { | ||
int [] dp = new int[n + 1]; | ||
|
||
for(int i = 1; i <= n; i++) { | ||
dp[i] = i; | ||
for(int j = 1, pow = j * j; pow <= i; j++, pow = j * j) | ||
if(dp[i] > dp[i - pow] + 1) | ||
dp[i] = dp[i - pow] + 1; | ||
} | ||
return dp[n]; | ||
} | ||
|
||
// n이 제곱이면 dp[n] = 2 | ||
// n이 제곱이 아니라면, (n - i => i) dp[n] = min((dp[n - 1] + dp[1]) .... dp[n - i] + dp[i]) | ||
private static int solution(int n) { | ||
final int MAX_SQUARE = 100_001; | ||
final int MAX_SQRT = (int) Math.sqrt(MAX_SQUARE); | ||
boolean[] isSquare = new boolean[MAX_SQUARE]; | ||
int[] dp = new int[n + 1]; | ||
|
||
isSquare[1] = true; | ||
dp[1] = 1; | ||
|
||
for (int i = 2; i <= n; i++) { | ||
if (isSquare[i]) { | ||
dp[i] = 1; | ||
} else { | ||
dp[i] = getMin(dp, i); | ||
} | ||
|
||
if (i <= MAX_SQRT) { | ||
isSquare[i * i] = true; | ||
} | ||
} | ||
|
||
return dp[n]; | ||
} | ||
|
||
private static int getMin(int[] dp, int n) { | ||
int min = Integer.MAX_VALUE; | ||
for (int i = 1; n - i >= i; i++) { | ||
min = Math.min(min, dp[n - i] + dp[i]); | ||
} | ||
return min; | ||
} | ||
} |