본문 바로가기
코딩테스트 문제집/Softeer(Lv1): FIN

[Softeer] Lv1: 연탄 배달의 시작(7626)

by cogito30 2025. 2. 9.
반응형

문제

- 링크: https://softeer.ai/practice/7626

 

풀이

더보기
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        long[] towns = new long[n];
        for (int i = 0; i < n; ++i) {
            towns[i] = sc.nextLong();
        }

        long diff = towns[1] - towns[0];
        long count = 0;
        for (int i = 1; i < towns.length; ++i) {
            if ((towns[i] - towns[i-1]) == diff) {
                count += 1;
            } else if (towns[i] - towns[i-1] < diff) {
                count = 1;
                diff = towns[i] - towns[i-1];
            }
        }
        System.out.println(count);
    }
}

 

반응형