코딩 테스트/Level 2
11. 주식 가격
컴닥
2020. 7. 25. 22:39
반응형
https://programmers.co.kr/learn/courses/30/lessons/42584
난이도가 높지 않습니다.
공개된 지 얼마되지 않아서 푼 사람이 적은 것 같습니다.
코딩테스트 연습 - 주식가격
초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00
programmers.co.kr
파이썬
def solution(prices):
a = []
for i in range(len(prices)):
check = 0
for j in range(i, len(prices)-1):
if prices[i] <= prices[j] :
check +=1
else:
break
a.append(check)
return a
자바
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
for (int i = 0; i < prices.length; i++) {
for (int j = i; j < prices.length - 1 & prices[i] <= prices[j]; j++) {
answer[i]++;
}
}
return answer;
}
}
for 문 안에 조건문을 우겨 넣으면 무려 3줄이나 줄일 수 있습니다만...
C#
public class Solution
{
public int[] solution(int[] prices)
{
int[] answer = new int[prices.Length];
for (int i = 0; i < prices.Length; i++)
{
for (int j = i; j < prices.Length - 1 & prices[i] <= prices[j]; j++)
{
answer[i]++;
}
}
return answer;
}
}
반응형