코딩테스트

[2023-01-20]

nayoon 2023. 1. 20. 08:04

Start

[2023-01-18] 시작 화면과 동일하게..

// you can also use imports, for example:
import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

class Solution {
    public int solution(int[] A) {
        int size = A.length;
        boolean isPeak = false;
        int peakSize = 0;
        int result = 0;
        boolean[] peaks = new boolean[size];

        if (size <= 2) {
            return result;
        }

        for(int i = 0; i < size-1; i++) {
            if (isPeak) {
                if (A[i] > A[i+1]) {
                    peaks[i] = true;
                    peakSize += 1;
                    isPeak = false;
                }
            } else {
                if (A[i] < A[i+1]) {
                    isPeak = true;
                }
            }
        }
        
        // start point
        int s = 0;
        for(int i = 0; i < size; i++) {
            if (peaks[i]) {
                s = i;
                break;
            }
        }


        int maxFlag = peakSize;
        int p, temp = 0;
        for(int i = maxFlag; i >= 2; i--) {
            temp = 0;
            for(int j = s; j < size; j++) {
                if (temp == i) {
                    break;
                }
                if (peaks[j]) {
                    temp++;
                    j += (i-1);
                } else {
                   continue;
                }
            }
            if (temp >= result) {
                result = temp;
            } else {
                break;
            }
        }

        return result;
    }
}

'코딩테스트' 카테고리의 다른 글

[2023-01-24]  (0) 2023.01.24
[2023-01-21]  (0) 2023.01.21
[2023-01-18]  (0) 2023.01.18
[2023-01-17]  (2) 2023.01.17
[2023-01-16]  (0) 2023.01.16