코딩테스트 연습 - 주식가격
초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00
programmers.co.kr
<내 코드>
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+1; j< prices.length; j++){
if (prices[i] > prices[j]) {
answer[i] = j-i;
break;
}
if(j == prices.length-1){ // 마지막 요소 확인
answer[i] = j-i;
}
}
}
return answer;
}
}
def solution(prices):
answer = []
n = len(prices)
for i in range(n): #0~4
cnt = 0
if i == n-1:
answer.append(cnt)
break
for j in range(i+1,n): #1~4
if prices[i] <= prices[j]:
cnt += 1
else:
cnt += 1
break
answer.append(cnt)
return answer
반응형
'알고리즘 문제풀기 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 기능개발 - Python, Java (스택, 큐) (0) | 2020.10.20 |
---|---|
[2019 카카오 겨울 인턴십] 튜플 - Python (0) | 2020.09.11 |
[2019 카카오 겨울 인턴십] 크레인 인형뽑기 게임 - Python (0) | 2020.09.11 |
[2020 카카오 인턴십] 수식 최대화 - Python (0) | 2020.09.11 |
[JavaScript] 프로그래머스 - 다리를 지나는 트럭 (0) | 2020.05.11 |