https://school.programmers.co.kr/learn/courses/30/lessons/42584
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제
초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요.
제출 답안
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
for (int i=0; i<prices.length; i++){
int cnt = 0;
for (int j=1; j<prices.length-i; j++){
if (prices[i]>prices[i+j]){
cnt++;
break;
}
cnt++;
}
answer[i] = cnt;
}
return answer;
}
}
개선 답안
class Solution {
public int[] solution(int[] prices) {
int len = prices.length;
int[] answer = new int[len];
int i, j;
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; j++) {
answer[i]++;
if (prices[i] > prices[j])
break;
}
}
return answer;
}
}
참고 자료
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
'알고리즘 > JAVA' 카테고리의 다른 글
[JAVA | 백준] 1330: 두 수 비교하기 (0) | 2023.05.04 |
---|---|
[JAVA | 프로그래머스] 더 맵게 (0) | 2023.04.27 |
[JAVA | 백준] 1806: 부분합 (0) | 2023.04.27 |
[JAVA | 프로그래머스] 신규 아이디 추천 (1) | 2023.04.25 |
[JAVA | 백준] 11399: ATM (0) | 2023.04.25 |