프로그래머스는 다 좋은데, 테스트케이스를 다 볼수 있으면 너무 좋을듯 혼자 하는 사람들 어쩌라고
1차답안
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int[] answer = {};
LinkedList<Integer> ll = new LinkedList<Integer>();
for(int i=0;i<prices.length;i++) {
int price = prices[i];
boolean result = false;
for(int a = i+1; a<prices.length;a++) {
if (price>prices[a]) {
ll.add(a-i);
result = true;
break;
}
}
if(result == false) {
ll.add(prices.length-(i+1));
}
}
int[] array = new int[ll.size()];
for (int i = 0; i < ll.size(); i++) {
array[i] = ll.get(i);
}
return array;
}
}
정확성 테스트는 통과했지만 효율성 테스트 다 날라가버린 코드
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
for(int i=0;i<prices.length;i++) {
for(int a = i+1; a<prices.length;a++) {
if (prices[i]>prices[a]) {
answer[i] = a-i;
break;
}
if(a == prices.length-1) {
answer[i] = a-i;
}
}
}
return answer;
}
}
더 간결하고 쉬운 코드다