728x90
SMALL

C#

 

 

using System;

public class Solution {
    public int[] solution(int[] prices) {
        int[] answer = new int[prices.Length];
        int[] sample = prices;

        for(int i = 0;i<prices.Length-1;i++)
        {
            for(int j = i+1;j<prices.Length;j++)
            {
                if(prices[i] <= prices[j])
                {
                    answer[i]++;
                }
                else
                {
                    answer[i]++;
                    break;
                }
            }
        }
        
        answer[prices.Length-1] = 0;
        return answer;
    }
}

 

 

 

//////////////////

 

 

using System;
using System.Collections.Generic;

public class Solution
{
    public int[] solution(int[] prices)
    {
        Stack<int> stack = new Stack<int>();
        int length = prices.Length;
        List<int> answer = new List<int>(prices);

        for (int i = 0; i < length; i++)
        {
            while (stack.Count != 0 && prices[stack.Peek()] > prices[i])
            {
                int top = stack.Pop();
                answer[top] = i - top;
                // 뒤에 있는 가격이 비교기준이 되는 가격보다 낮아질 경우에 대입하는 방법
            }
            stack.Push(i);
        }

        while (stack.Count != 0)
        {
            int top = stack.Pop();
            answer[top] = length - 1 - top;
            // 가격이 안 떨어졌을 때의 대입방법
        }

        return answer.ToArray();
    }
}

 

 

 

//////////////////////

 

 

 

자바

 

 

 

import java.util.Stack;

class Solution {
    public int[] solution(int[] prices) {
        Stack<Integer> 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;
    }
}

 

728x90

설정

트랙백

댓글