알고리즘 문제 풀이

프로그래머스 - 주식가격

블린더르 2020. 7. 30. 23:09

자바

/*
 * 2020-07-30
 * https://programmers.co.kr/learn/courses/30/lessons/42584?language=java
 */

public class StockPrice {
    public int[] solution(int[] prices) {
        int[] answer = new int[prices.length];

        for (int i = 0; i < prices.length; i++) {
            int time = prices.length - 1 - i;
            for (int j = i + 1; j < prices.length; j++) {
                System.out.println(prices[i] + ":" + prices[j]);
                if (prices[i] > prices[j]) {
                    time = j - i;
                    System.out.print(j);
                    break;
                }
            }
            answer[i] = time;
            System.out.println("time:" + time);
        }

        return answer;
    }

    public static void main(String[] args) {
        int[] result = new StockPrice().solution(new int[] { 1, 2, 3, 2, 3 });
        System.out.println(result);
    }
}

코틀린

/*
 * 2020-07-30
 * https://programmers.co.kr/learn/courses/30/lessons/42584?language=java
 */
class KtStockPrice {
    fun solution(prices: IntArray): IntArray {
        return prices.mapIndexed { index, i ->
            var time = prices.count() - 1 - index
            for (j in index + 1 until prices.count()) {
                print(j-index)
                if (i > prices[j]) {
                    time = j - index
                    break
                }
            }
            time
        }.toIntArray()
    }
}

fun main() {
    val result = KtStockPrice().solution(
        intArrayOf(1, 2, 3, 2, 3)
    )
    println(result)
}

자바로 제출한 코드를 코틀린으로 바꿔보았다

반응형