본문 바로가기
Problem Solving/Baekjoon

[백준] 10657 Cow Jog - Data Structure / Java

by graycode 2023. 6. 30.

 문제 링크

 

10657번: Cow Jog

The cows are out exercising their hooves again!  There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at different speeds. With only one lane in the tr

www.acmicpc.net

 

 풀이 과정

 

 풀이 코드

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

public class Main {

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

        Stack<Integer> stk = new Stack<>();

        while (n-- > 0) {
            int speed = Integer.parseInt(br.readLine().split(" ")[1]);

            while (!stk.empty() && stk.peek() > speed) stk.pop();

            stk.push(speed);
        }

        bw.write(String.valueOf(stk.size()));
        bw.flush();
    }

}

댓글