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

Feat: Add solution of #118 #127

Merged
merged 2 commits into from
Jul 9, 2022
Merged

Feat: Add solution of #118 #127

merged 2 commits into from
Jul 9, 2022

Conversation

Dltmd202
Copy link
Contributor

@Dltmd202 Dltmd202 commented Jul 8, 2022

#118 문제풀이

2022-07-08
@Dltmd202

문제

소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringBuilder sb = new StringBuilder();

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(br.readLine());

        int[] data = new int[n];
        for (int i = 0; i < n; i++) {
            data[i] = Integer.parseInt(br.readLine());
        }

        make(n, data);
    }

    private static void make(int n, int[] data) {
        Stack<Integer> stack = new Stack<>();
        int cursor = 1;

        try {
            for (int i = 0; i < n; i++) {
                if (stack.isEmpty() || stack.peek() < data[i]) {
                    for (int j = cursor; j <= data[i]; j++) {
                        stack.push(j);
                        sb.append("+\n");
                    }
                    cursor = data[i] + 1;
                }
                while (!stack.isEmpty() && stack.peek() >= data[i]) {
                    if(stack.pop() > data[i]) throw new RuntimeException();
                    sb.append("-\n");
                }
            }
            System.out.println(sb.toString().trim());
        }catch (Exception e){
            System.out.println("NO");
        }
    }
}

@Dltmd202 Dltmd202 merged commit b55b470 into master Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant