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
- 백준
- JUnit 5
- 그리디
- Kotlin
- java
- mst
- 플로이드-와샬
- 위상정렬
- 알고리즘
- 수학
- 문자열
- swea
- Effective Java
- 에라토스테네스의 체
- 완전탐색
- 스택
- 후니의 쉽게 쓴 시스코 네트워킹
- Network
- dfs
- 투 포인터
- 유니온 파인드
- 이분탐색
- 세그먼트 트리
- 시뮬레이션
- 프로그래머스
- 백트래킹
- 구현
- CS
- BFS
- 동적계획법
Archives
반갑습니다!
[프로그래머스] 주식가격 본문
분류가 스택/큐로 되어있지만 단순 탐색으로 해결하였다.
풀이
prices의 길이가 100,000 이하이므로 N^2의 복잡도의 이중 for문으로 해결가능하다.
코드
C++
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> prices) {
vector<int> answer;
for(int i=0; i<prices.size(); i++){
int cnt=0;
for(int j=i+1; j<prices.size(); j++){
cnt++;
if(prices[i] > prices[j]) break;
}
answer.push_back(cnt);
}
return answer;
}
Java
import java.util.Arrays;
class Solution {
public int[] solution(int[] prices) {
int n = prices.length;
int[] answer = new int[n];
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = i + 1; j < n; j++) {
cnt++;
if (prices[i] > prices[j]) break;
}
answer[i] = cnt;
}
return answer;
}
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[프로그래머스] 기능개발 (0) | 2020.04.02 |
---|---|
[ 프로그래머스] 다리를 지나는 트럭 (0) | 2020.04.02 |
[프로그래머스] 가장 긴 팰린드롬 (0) | 2020.04.02 |
[프로그래머스] 가장 먼 노드 (0) | 2020.04.02 |
[프로그래머스] 2 x n 타일링 (0) | 2020.04.02 |