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

Conversation

wwan13
Copy link
Owner

@wwan13 wwan13 commented Nov 19, 2024

Problem Info

Code

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);
        }
    }
}

Note

@github-actions github-actions bot added binary_search 알고리즘: binary_search lis 알고리즘: lis labels Nov 19, 2024
@github-actions github-actions bot changed the title solve : 12738 가장 긴 증가하는 부분 수열 3 [G2] 12738 가장 긴 증가하는 부분 수열 3 Nov 19, 2024
@wwan13 wwan13 merged commit e8e31b7 into main Nov 19, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
binary_search 알고리즘: binary_search lis 알고리즘: lis
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant