BOJ 1158번 조세퍼스

|

조세퍼스

package BOJ;


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

/**
 * BOJ 1158
 * https://www.acmicpc.net/problem/1158
 */

public class JosePhus_1158 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split("\\s");
        int n = Integer.parseInt(line[0]);
        int k = Integer.parseInt(line[1]);
        List<Integer> result = new ArrayList<>();
        int cnt = 0;
        Deque<Integer> deque = new ArrayDeque();
        for (int i = 1; i <= n; i++) {
            deque.addLast(i);
        }

        while (!deque.isEmpty()){
            cnt++;
            if(deque.size() == 1){
                result.add(deque.removeFirst());
                break;
            }

            if(cnt == k){
                result.add(deque.removeFirst());
                cnt = 0;
            }
            else{
                Integer pushed = deque.pollFirst();
                deque.addLast(pushed);
            }
        }

        print(result);
    }

    private static void print(List<Integer> result) {
        StringBuilder sb = new StringBuilder();
        sb.append("<");
        result.forEach(x -> sb.append(x+", "));
        sb.delete(sb.length()-2,sb.length());
        sb.append(">");
        System.out.println(sb.toString());
    }
}

20191222_TIL

|

12/22 일요일

  1. 리뷰
    • 한 권으로 그리는 컴퓨터과학 로드맵
      • 챕터3 - 문제해결전략 포스팅

20191221_TIL

|

12/21 토요일

  1. 알고리즘
    • 리팩토링
      • 힙정렬
      • 우선순위 큐
      • 퀵정렬
      • 계수정렬
      • 기수정렬
      • 버킷정렬

BOJ 9012번 괄호

|

괄호


package BOJ;

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

/**
 * BOJ 9012 ( https://www.acmicpc.net/problem/9012 )
 */
public class VPS {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int size = Integer.parseInt(br.readLine());
        while(size >0){
            size--;
            vaild(br.readLine());
        }
    }

    private static void vaild(String input){
        Stack<Character> stack = new Stack();
        char[] chars = input.toCharArray();
        for(int i=0; i< chars.length; i++){
            switch (chars[i]){
                case '(':
                    stack.add(chars[i]);
                    break;
                case ')':
                    if(stack.size()==0){
                        System.out.println("NO");
                        return;
                    }
                    stack.pop();
                    break;
            }
        }
        if(stack.size() == 0)
            System.out.println("YES");
        else
            System.out.println("NO");

    }
}

BOJ 1874번 스택수열

|

스택수열


package BOJ;

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

/**
 * BOJ 1874( https://www.acmicpc.net/problem/1874 )
 */

public class StackSequence {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] a = new int[n];
        for (int i = 0; i < a.length; i++)
            a[i] = Integer.parseInt(br.readLine());
        solve(a);

    }

    private static void solve(int[] a) {
        int n = a.length;
        Stack<Integer> stack = new Stack<>();
        int curIdx = 0;
        StringBuilder sb = new StringBuilder();

        for (int x : a) {
            if(x > curIdx){
                while(x > curIdx){
                    stack.push(++curIdx);
                    sb.append("+\n");
                }
                stack.pop();
                sb.append("-\n");
            } else {
                // stack.peek() != x
                // -> 예를 들어 stack[1,2,3],x=2이면 3을 pop()해야 2를 꺼낼 수 있다.
                // 3을 pop()하면 3을 출력할 수 없으므로 해당 조건이 필요하다.
                if(stack.peek() != x){
                    System.out.println("NO");
                    return;
                }
                stack.pop();
                sb.append("-\n");
            }
        }
        System.out.println(sb.toString());
    }
}