Problem
Solution
Max distance that we can move at a time is 2.
Look at the index value larger than current position by 2.
If the value is 0, move 2 times and add a jump.
If the value is 1, move just one time and add a jump.
(If we are at a position where only one number is left in the array, it is not possible to move up 2 times... so just add one jump and end the loop.)
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 jump = 0;
int currPos = 0;
while(true){
if(currPos == c.size()-1)
break;
if(currPos == c.size()-2 || c.get(currPos+2) == 1)
currPos++;
else
currPos = currPos+2;
jump++;
}
return jump;
}
}
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();
}
}
'CODE > Algorithms & Data Structures' 카테고리의 다른 글
[LeetCode] Minimum Deletions to Make Character Frequencies Unique (0) | 2021.10.03 |
---|---|
[LeetCode] String to Integer (atoi) (0) | 2021.10.02 |
[LeetCode] Spiral Matrix (0) | 2021.10.02 |
[LeetCode] Two Sum (0) | 2021.10.01 |
[HackerRank] Counting Valleys _Java8 (0) | 2021.08.06 |