반응형
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 |
Tags
- 시스템설계
- 시스템설계면접
- 시스템설계면접예시
- 리액트구글애널리틱스
- 시스템설계방법
- 헤드퍼스트전략패턴
- git squash
- 리팩토링2판4장
- cypressBDD
- react-ga
- 가상면접으로대규모시스템
- git commit merge
- react
- 리팩터링2판테스트
- gitsquash
- FirebaseAnalytics
- formik react-query submitting not working
- 가상면접3장
- 디자인패턴
- 가상면접2장
- git commit 협업
- awss3
- formik submitting not working
- 시스템설계면접팁
- Git commit 합치기
- file not found Error
- 테스트코드책
- cypress React
- s3이미지다운로드됨
- 전략패턴
Archives
- Today
- Total
mingg IT
[JAVA] 프로그래머스 주식 가격 본문
programmers.co.kr/learn/courses/30/lessons/42584
코딩테스트 연습 - 주식가격
초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00
programmers.co.kr
1차 코드
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
List<Integer> test = new ArrayList<Integer>();
int count = 0;
for(int i = 0; i< prices.length; i++) {
int pri = prices[i];
boolean flag = false;
count = 0;
for(int j = i+1; j< prices.length; j++) {
if(flag == false) {
if(pri <= prices[j]) {
count++;
if(j == prices.length -1)
// test.add(count);
answer[i] = count;
} else {
count++;
test.add(count);
answer[i] = count;
flag = true;
}
}
}
}
answer[prices.length-1] = 0;
// test.add(0);
// test = test.toArray(new Integer[test.size()]);
return answer;
}
}
시간초과가 났다.
2차 코드
필요없는 if문을 제거했다.
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
List<Integer> test = new ArrayList<Integer>();
int count = 0;
for(int i = 0; i< prices.length; i++) {
int pri = prices[i];
boolean flag = false;
count = 0;
for(int j = i+1; j< prices.length; j++) {
if(pri <= prices[j]) {
count++;
if(j == prices.length -1)
answer[i] = count;
} else {
count++;
answer[i] = count;
break;
}
}
}
answer[prices.length-1] = 0;
return answer;
}
}
성공..
if문 하나가 많이 성능면에서 차이가 많이 큰가보다 ..
다른 날 도전
프로그래머스에서 푼 문제로 등록이 되어 있지 않아서 안 푼줄 알고 풀었다.
class Solution {
public int[] solution(int[] prices) {
int answer [] = new int[prices.length];
for(int i = 0 ; i< prices.length-1; i++) {
int target = prices[i];
int count = 0;
for(int j = i+1; j< prices.length; j++) {
if(target <= prices[j]) {
count++;
} else {
count++;
break;
}
}
answer[i] = count;
}
answer[prices.length -1] = 0;
return answer;
}
}
성공!
'코딩테스트' 카테고리의 다른 글
[JAVA] 프로그래머스 문자열 압축 (0) | 2021.04.17 |
---|---|
[JAVA] 프로그래머스 스킬 트리 (0) | 2021.04.17 |
[JAVA] 프로그래머스 프린터 (0) | 2021.04.13 |
[JAVA] 프로그래머스 1 2 4 나라의 숫자 (0) | 2021.04.06 |
[Java] 프로그래머스 두 개 뽑아서 더하기 (0) | 2021.02.06 |
Comments