Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[G2] 12738 가장 긴 증가하는 부분 수열 3 #114

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions java/src/boj12738/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package boj12738;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {

private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) {
int n = Integer.parseInt(readLine());
long[] data = Arrays.stream(readLine().split(" "))
.mapToLong(Long::parseLong)
.toArray();

ArrayList<Long> list = new ArrayList<>();
list.add(data[0]);

for (int i = 1; i < n; i++) {
long target = data[i];
if (list.get(list.size() - 1) < target) {
list.add(target);
} else {
int index = indexOf(list, target);
list.set(index, target);
}
}

System.out.println(list.size());
}

private static int indexOf(ArrayList<Long> list, long target) {
int start = 0;
int end = list.size() - 1;

while (start <= end) {
int middle = (start + end) / 2;
long compareTarget = list.get(middle);
if (compareTarget < target) {
start = middle + 1;
} else if (compareTarget == target) {
return middle;
} else {
end = middle - 1;
}
}

return start;
}

private static String readLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Loading