[HackerRank] Running Time of Algorithms (Java)
https://www.hackerrank.com/challenges/runningtime/problem?isFullScreen=true
Solution
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;
class Result {
static int runningTime(List<Integer> arr) {
int answer = 0;
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) < arr.get(i - 1)) {
int tmp = arr.get(i);
arr.set(i, arr.get(i - 1));
arr.set(i - 1, tmp);
answer++;
i = 0;
}
}
return answer;
}
}
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> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.runningTime(arr);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Leave a comment