-
프로그래머스 - 주식가격알고리즘 문제 풀이 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) }
자바로 제출한 코드를 코틀린으로 바꿔보았다
반응형'알고리즘 문제 풀이' 카테고리의 다른 글
프로그래머스 - 다리를 지나는 트럭 (0) 2020.08.08 프로그래머스 - 기능개발 (0) 2020.08.05 프로그래머스 - 쇠막대 (0) 2020.07.28 프로그래머스 - 탑 (0) 2020.07.27 프로그래머스 - H-Index (0) 2020.07.26