-
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
67a144a
commit c316a71
Showing
3 changed files
with
215 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,125 @@ | ||
package test.boj.dp; | ||
|
||
import org.junit.*; | ||
|
||
import java.io.*; | ||
import java.util.StringTokenizer; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
// https://cheolhojung.github.io/posts/algorithm/boj-biotonic-partial-sequence.html | ||
public class m_가장_긴_바이토닉_부분_수열 { | ||
|
||
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()); | ||
BitonicSubSequence biTonicSubNumbers = new BitonicSubSequence(n, br.readLine()); | ||
System.out.println(biTonicSubNumbers.maxLength()); | ||
|
||
bw.flush(); | ||
bw.close(); | ||
br.close(); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
assertThat(solution(1, "1")) | ||
.isEqualTo(1); | ||
assertThat(solution(10, "1 5 2 1 4 3 4 5 2 1")) | ||
.isEqualTo(7); | ||
assertThat(solution(7, "10 5 10 20 10 30 5")) | ||
.isEqualTo(5); | ||
} | ||
|
||
private int solution(int n, String numbers) { | ||
BitonicSubSequence biTonicSubNumbers = new BitonicSubSequence(n, numbers); | ||
|
||
return biTonicSubNumbers.maxLength(); | ||
} | ||
|
||
static class BitonicSubSequence { | ||
|
||
private static final int MAX_LENGTH = 1000; | ||
private static final int PREV_SMALLER = 0; | ||
private static final int NEXT_SMALLER = 1; | ||
|
||
private final int n; | ||
private final int[] numbers; | ||
|
||
private int[][] dp; | ||
|
||
public BitonicSubSequence(int n, String numbers) { | ||
this.n = n; | ||
this.numbers = new int[n + 2]; | ||
this.dp = new int[2][MAX_LENGTH + 1]; | ||
|
||
StringTokenizer tokenizer = new StringTokenizer(numbers); | ||
for (int i = 1; i <= n; i++) { | ||
this.numbers[i] = Integer.parseInt(tokenizer.nextToken()); | ||
} | ||
} | ||
|
||
public int maxLength() { | ||
int result = Integer.MIN_VALUE; | ||
|
||
for (int i = 1; i <= n; i++) { | ||
dp[PREV_SMALLER][i] = maxLengthPrevSmallerNumbers(i) + 1; | ||
} | ||
|
||
for (int i = n; i >= 1; i--) { | ||
dp[NEXT_SMALLER][i] = maxLengthNextSmallerNumbers(i) + 1; | ||
result = Math.max(result, dp[NEXT_SMALLER][i] + dp[PREV_SMALLER][i] - 1); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private int maxLengthNextSmallerNumbers(int i) { | ||
return maxRangeClosed(i + 1, n, i, NEXT_SMALLER); | ||
} | ||
|
||
private int maxLengthPrevSmallerNumbers(int i) { | ||
return maxRangeClosed(1, i - 1, i, PREV_SMALLER); | ||
} | ||
|
||
private int maxRangeClosed(int start, int end, int i, int SMALLER_INDEX) { | ||
return IntStream.rangeClosed(start, end) | ||
.map(j -> max(i, j, SMALLER_INDEX)) | ||
.max() | ||
.orElse(0); | ||
} | ||
|
||
private int max(int i, int j, int SMALLER_INDEX) { | ||
if (this.numbers[i] > this.numbers[j]) { | ||
return dp[SMALLER_INDEX][j]; | ||
} | ||
return 0; | ||
} | ||
|
||
// private int maxLengthNextSmallerNumbers2(int i) { | ||
// int result = 0; | ||
// for (int j = i + 1; j <= n; j++) { | ||
// result = max(result, i, j, NEXT_SMALLER); | ||
// } | ||
// return result; | ||
// } | ||
// | ||
// private int maxLengthPrevSmallerNumbers2(int i) { | ||
// int result = 0; | ||
// for (int j = i - 1; j >= 1; j--) { | ||
// result = max(result, i, j, PREV_SMALLER); | ||
// } | ||
// return result; | ||
// } | ||
// | ||
// private int max(int result, int i, int j, int SMALLER_INDEX) { | ||
// if (this.numbers[i] > this.numbers[j]) { | ||
// return Math.max(result, dp[SMALLER_INDEX][j]); | ||
// } | ||
// return result; | ||
// } | ||
} | ||
} |
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,89 @@ | ||
package test.boj.dp; | ||
|
||
import org.junit.*; | ||
|
||
import java.io.*; | ||
import java.util.StringTokenizer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class n_연속합 { | ||
|
||
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()); | ||
ContinuousSum continuousSum = new ContinuousSum(n, br.readLine()); | ||
System.out.println(continuousSum.max()); | ||
|
||
bw.flush(); | ||
bw.close(); | ||
br.close(); | ||
|
||
} | ||
|
||
@Test | ||
public void test() { | ||
assertThat(solution(10, "10 -4 3 1 5 6 -35 12 21 -1")) | ||
.isEqualTo(33); | ||
assertThat(solution(1, "1")) | ||
.isEqualTo(1); | ||
assertThat(solution(1, "-1000")) | ||
.isEqualTo(-1000); | ||
assertThat(solution(2, "-2000 -1000")) | ||
.isEqualTo(-1000); | ||
assertThat(solution(2, "-1000 -2000")) | ||
.isEqualTo(-1000); | ||
assertThat(solution(3, "-1000 5 1000")) | ||
.isEqualTo(1005); | ||
assertThat(solution(5, "-3 -1 2 -1 5")) | ||
.isEqualTo(6); | ||
} | ||
|
||
private int solution(int n, String numbers) { | ||
ContinuousSum continuousSum = new ContinuousSum(n, numbers); | ||
return continuousSum.max(); | ||
} | ||
|
||
static class ContinuousSum { | ||
|
||
private final int n; | ||
private final int[] numbers; | ||
private final int[] dp; | ||
|
||
public ContinuousSum(int n, String numbers) { | ||
this.n = n; | ||
this.dp = new int[n + 1]; | ||
this.numbers = new int[n + 1]; | ||
StringTokenizer tokenizer = new StringTokenizer(numbers); | ||
for (int i = 1; i <= n; i++) { | ||
this.numbers[i] = Integer.parseInt(tokenizer.nextToken()); | ||
} | ||
} | ||
|
||
// 현재 숫자를 이전 숫자까지의 합과 더한 경우와 | ||
// 현재 숫자부터 시작하는 경우로 나눌 수 있음 | ||
// dp[i - 1] + this.numbers[i]: 현재 숫자를 이전 숫자까지의 합과 더한 경우 | ||
// this.numbers[i]: 현재 숫자부터 시작하는 경우 | ||
// -1000 5 1000 | ||
// dp -1000 5 1005 | ||
// O -1005 1005 | ||
// X 5 1000 | ||
public int max() { | ||
int result = this.numbers[1]; | ||
dp[1] = this.numbers[1]; | ||
|
||
for (int i = 2; i <= n; i++) { | ||
|
||
dp[i] = Math.max( | ||
dp[i - 1] + this.numbers[i], | ||
this.numbers[i] | ||
); | ||
|
||
result = Math.max(result, dp[i]); | ||
} | ||
return result; | ||
} | ||
} | ||
} |