최적화 문제인줄 걱정하지 말자. 잘 생각해보면 while문 하나로 끝낼 수 있다.
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 'jumpingOnClouds' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY c as parameter.
*/
public static int jumpingOnClouds(List<Integer> c) {
int c_len = c.size() - 1;
int start_idx = 0;
int ans = 0;
while(start_idx < c_len){
if(start_idx+2 <= c_len && c.get(start_idx+2) == 0){
start_idx+=2;
ans += 1;
}
else if(c.get(start_idx+1) == 0){
start_idx+=1;
ans += 1;
}
}
return ans;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> c = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.jumpingOnClouds(c);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
'algorithm > java' 카테고리의 다른 글
hackerrank/arrays/Left Rotation (0) | 2021.06.27 |
---|---|
hackerrank/warm up/Repeated String (0) | 2021.06.20 |
java 개발이 처음일 때 볼 수 있는 에러 (0) | 2021.06.02 |
hackerrank/warm up/Counting Valleys (0) | 2021.05.23 |
java 조건문 사잇값 (0) | 2021.05.18 |