1. 문제
https://programmers.co.kr/learn/courses/30/lessons/42584?language=java
코딩테스트 연습 - 주식가격
초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00
programmers.co.kr
2. 나의 풀이
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int [prices.length];
for(int i = 0; i < prices.length; i = i + 1) {
int imsi = 1;
if(i == (prices.length - 1)) {
imsi -= 1;
}else {
for(int j = i + 1; j < prices.length - 1; j = j + 1) {
if(prices[i] <= prices[j]) {
imsi += 1;
}else {
break;
}
}
}
answer[i] = imsi;
}
return answer;
}
}
3. 다른 사람 풀이
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;
}
else
answer[i] = j-i;
}
}
//System.out.println(Arrays.toString(answer));
return answer;
}
}
'알고리즘 공부 > 프로그래머스' 카테고리의 다른 글
(Java)프로그래머스 코딩테스트 연습 - 2017 카카오코드 예선 - 카카오프렌즈 컬러링북 (0) | 2020.10.15 |
---|---|
프로그래머스 코딩테스트 연습 - Summer/Winter Coding(~2018) - 스킬트리 (0) | 2020.10.13 |
프로그래머스 코딩테스트 연습 - 연습문제 - 124 나라의 숫자 (0) | 2020.10.12 |
프로그래머스 코딩테스트 연습 - 2018 KAKAO BLIND RECRUITMENT - [1차] 다트 게임 (0) | 2020.10.11 |
프로그래머스 코딩테스트 연습 - 2018 KAKAO BLIND RECRUITMENT - [1차] 비밀지도 (0) | 2020.10.08 |