알고리즘/프로그래머스
기능개발
가다파하
2020. 9. 8. 17:24
public int[] solution(int[] progresses, int[] speeds) {
int[] answer = {};
Map<Integer, Integer> tempMap = new TreeMap<Integer, Integer>();
int previousDDay = 0;
for(int i=0; i<progresses.length; i++) {
int progress = progresses[i]; // 93
int speed = speeds[i]; // 1
int remainProgress = 100-progress; // 7
int dDay = 1;
while((dDay*speed) < remainProgress) {
dDay++;
}
if(i!=0) {
if(previousDDay < dDay) {
previousDDay = dDay;
tempMap.put(dDay, 1);
}else {
tempMap.put(previousDDay, tempMap.get(previousDDay)+1);
}
}else {
previousDDay = dDay;
tempMap.put(dDay, 1);
}
}
answer = new int[tempMap.size()];
int idx = 0;
for(int tempKey : tempMap.keySet()) {
answer[idx] = tempMap.get(tempKey);
idx++;
}
return answer;
}
HashMap은 넣는 순서가 보장 안됨, 때문에 TreeMap 사용해야함.
다른 사람 풀이중 lamda filter 이용한거 있어서 가져옴.
int[] dayOfend = new int[100];
int day = -1;
for(int i=0; i<progresses.length; i++) {
while(progresses[i] + (day*speeds[i]) < 100) {
day++;
}
dayOfend[day]++;
}
return Arrays.stream(dayOfend).filter(i -> i!=0).toArray();