Skip to content

Conversation

@Mordiumaco
Copy link
Collaborator

처음에는 객체지향 방법으로 다가가 문제를 풀었으나 정확성은 100%이나 효율성 테스트 부분에서 실패 다시 문제를 풀었습니다.
새로운 문제 풀이 방법은 이중 for문을 사용하여 비교하였고 앞에 price부터 하나하나 timer를 구하는 형식으로 문제를 풀었습니다.
더 나은 문제 풀이 방법이 있을 것 같은데 문득 생각이 나지가 않았습니다.

그냥 밑은 자료구조를 이용해 문제를 풀었길래 한번 봤습니다.
개인적으로는 크게 의미가 있을지는 모르겠습니다.

public int[] solution(int[] prices) {
Stack beginIdxs = new Stack<>();
int i=0;
int[] terms = new int[prices.length];

    beginIdxs.push(i);
    for (i=1; i<prices.length; i++) {
        while (!beginIdxs.empty() && prices[i] < prices[beginIdxs.peek()]) {
            int beginIdx = beginIdxs.pop();
            terms[beginIdx] = i - beginIdx;
        }
        beginIdxs.push(i);
    }
    while (!beginIdxs.empty()) {
        int beginIdx = beginIdxs.pop();
        terms[beginIdx] = i - beginIdx - 1;
    }

    return terms;
}

@whdgns5059
Copy link
Collaborator

다른사람의 풀이나 이전 코테 리뷰에서 했던걸 봐도 굳이 자료구조나 객체지향 설계를 하지 않아도 나쁘지 않게 평가하네요
필요한데 올바른 방법을 쓰는게 좋은 코딩인거 같습니다. 수고하셧습니다

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants