반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- sql
- 팩토리얼 진법
- MacOS
- mysql
- Do_it
- DFS
- 백준
- 자바
- matplotlib
- Extended Slices
- 박스그래프
- BFS
- 타입 변수
- 이것이 취업을 위한 코딩테스트다
- 다익스트라 알고리즘
- dacon
- java
- 2BPerfect
- Do it
- 참조 변수
- 프로그래머스
- 집 값 예측 분석
- 순열
- np.zeros_like
- 습작
- 이진수 변환
- jdbc
- PYTHON
- 브라우저 실행
- 점프 투 파이썬
Archives
- Today
- Total
🦕 공룡이 되자!
[프로그래머스] Java 주식가격 본문
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 = i + 1; j < prices.length; j++) {
if (i == prices.length - 1) {
cnt = 0;
break;
} else if (prices[i] <= prices[j]) {
cnt++;
} else if (prices[i] > prices[j]) {
cnt++;
break;
}
}
answer[i] = cnt;
}
return answer;
}
}
- 리턴되는 횟수를 cnt라고 지정을하고 1초 뒤의 가격과 비교하여 cnt를 측정했다.
- 조금 더 간단히 하려면 cnt를 없애고 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;
}
}
반응형
'Development > CodingTest' 카테고리의 다른 글
[순열] Swap을 이용한 순열 (0) | 2021.12.23 |
---|---|
[백준] Java 숫자카드 2 (0) | 2021.12.20 |
[프로그래머스] Java 모의고사 (0) | 2021.12.16 |
[프로그래머스] Java 다리를 지나는 트럭 (2) | 2021.12.14 |
[프로그래머스] Java 기능개발 (0) | 2021.12.13 |
Comments