‡ CODING TEST STUDY ‡/º 백준

[백준 | Java Bronze I ] (#14659) 한조서열정리하고옴ㅋㅋ

Trudy | 송연 2024. 7. 10. 13:35

문제

https://www.acmicpc.net/problem/14659


첫번째 코드 - 실패 

package week10.baek.july9;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class B14659 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());

        int[] mountains = new int[n];
        for (int i = 0; i < n; i++) {
            mountains[i] = Integer.parseInt(st.nextToken());
        }

        int[] scores = new int[n];
        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = i+1; j < n; j++) {
                if(mountains[i] >= mountains[j]) {
                    count++;
                }
                else {
                    scores[i] = count;
                    break;
                }
            }
        }


        for (int i = 0; i < n; i++) {
            System.out.println(scores[i]);
        }

        Arrays.sort(scores);

        System.out.println(scores[scores.length - 1]);
    }
}

 

else문 밖에서 scores[i]=count로 지정해놔야했다... 뭐가 다르지?

 

수정된 코드

package week10.baek.july9;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class B14659 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());

        int[] mountains = new int[n];
        for (int i = 0; i < n; i++) {
            mountains[i] = Integer.parseInt(st.nextToken());
        }

        int[] scores = new int[n];
        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = i+1; j < n; j++) {
                if(mountains[i] >= mountains[j]) {
                    count++;
                }
                else {
                    break;
                }
            }
            scores[i] = count;
        }

        Arrays.sort(scores);

        System.out.println(scores[scores.length - 1]);
    }
}

 

근데 이렇게 말고 scores[]에 모두 저장하는 것보다 int max 값을 두고 계속해서 그걸 저장하는게 더 효율적이다. 

sort하는 불필요한 연산도 줄일 수 있고, 배열을 저장하는 메모리 부분에서도 효율적이다. 

 

최종 코드

package week10.baek.july9;

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

public class B14659 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());

        int[] mountains = new int[n];
        for (int i = 0; i < n; i++) {
            mountains[i] = Integer.parseInt(st.nextToken());
        }

        int maxCount = 0;
        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = i + 1; j < n; j++) {
                if (mountains[i] >= mountains[j]) {
                    count++;
                } else {
                    break;
                }
            }
            if (count > maxCount) {
                maxCount = count;
            }
        }

        System.out.println(maxCount);
    }
}