반응형
sum과 avg을 int형으로만 생각해서 엣지케이스 통과 못했었음.
제약사항으로 준 내용은 방어코드로 코드 안짜도 됨. 이미 반영.
HackerRank가 좋은게 코드에만 집중하면 된다.
평균값과 현재값을 비교해야 함. (= 문제 이해)
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'countResponseTimeRegressions' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY responseTimes as parameter.
*/
public static int countResponseTimeRegressions(List<Integer> responseTimes) {
int resultCnt = 0;
long sum = 0L;
double avg = 0.0;
int resSize = responseTimes.size();
for(int i=0; i < resSize; i++){
if(i != 0){
sum += responseTimes.get(i-1);
avg = (double)sum / i;
if(responseTimes.get(i) > avg) {
resultCnt++;
}
}
}
return resultCnt;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int responseTimesCount = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> responseTimes = IntStream.range(0, responseTimesCount).mapToObj(i -> {
try {
return bufferedReader.readLine().replaceAll("\\s+$", "");
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.map(String::trim)
.map(Integer::parseInt)
.collect(toList());
int result = Result.countResponseTimeRegressions(responseTimes);
System.out.println(result);
bufferedReader.close();
}
}
반응형
'Developer : 태하팍 > 코딩 테스트' 카테고리의 다른 글
| 코테준비) 간단 DFS를 풀어보자! (0) | 2025.11.12 |
|---|---|
| 코테대비 문법정리 (0) | 2025.09.18 |
| Codility Lesson 참고 (0) | 2025.09.18 |
| Codility Test (0) | 2025.09.11 |
| 까먹을수 있는 문법 정리_1 (0) | 2023.08.25 |