Java solution for Breaking best and worst Records HackerRank problem
Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.
Example 1 :
scores = [12, 24, 10, 24]
Scores are in the same order as the games played. She tabulates her results as follows:
Count
Game Score Minimum Maximum Min Max
0 12 12 12 0 0
1 24 12 24 0 1
2 10 10 24 1 1
3 24 10 24 1 1
Given the scores for a season, determine the number of times Maria breaks her records for most and least points scored during the season.
Example 2 :
Input :
scores = [10, 5, 20, 20, 4, 5, 2, 25, 1]
Output :
2 4
She broke her best record twice (after games 2 and 7) and her worst record four times (after games 1, 4, 6 and 8) so we print 2 and 4 as our answer. Note that she did not break her record for best score during game, as her score during that game was not strictly greater than her best record at the time.
See full problem description on HackerRank :
In this problem, simply we have to find total maximum and minimum record break.
Solution 1
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 {
public static List<Integer> breakingRecords(List<Integer> scores) {
List<Integer> output = new ArrayList<>();
int minRecord = scores.get(0);
int minRecordTotal = 0;
int maxRecord = scores.get(0);
int maxRecordTotal = 0;
for (int i = 1; i < scores.size(); i++) {
if (scores.get(i) < minRecord) {
minRecord = scores.get(i);
minRecordTotal++;
}
if (scores.get(i) > maxRecord) {
maxRecord = scores.get(i);
maxRecordTotal++;
}
}
output.add(maxRecordTotal);
output.add(minRecordTotal);
return output;
}
}
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> scores = Stream
.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = Result.breakingRecords(scores);
bufferedWriter.write(result.stream()
.map(Object::toString)
.collect(joining(" ")) + "\n");
bufferedReader.close();
bufferedWriter.close();
}
}
Solution explanation :
- First we are declaring 4 integer variable and initialize 2 variable as 0 and other 2 as 0th index of list means first list value.
- Traverse through given scores list and check two below conditions
- If current score is less than minRecord variable value, then store current list value to minRcord and increment minRecordTotal variable value.
- If current score is greater than maxRecord variable value, then store current list value to maxRecord and increment maxRecordTotal variable value.
- Store maxRecordTotal at 0th index and minRecordTotal to 1st index of output list. Return output.
Output explanation :
scores = [10, 5, 20, 20, 4, 5, 2, 25, 1]
- minRecord = 10, maxRecord = 10, minRecordTotal = 0, maxRecordTotal = 0
- i = 1
- recordList.get(i) < minRecord | 5 < 10 becomes true
- minRecord = recordList.get(i) | minRecord = 5
- minRecordTotal++ | minRecordTotal = 1
- recordList.get(i) > maxRecord | 5 > 10 becomes false
- i = 2, minRecord = 5, maxRecord = 10, minRecordTotal = 1, maxRecordTotal = 0
- 20 < 5 becomes false
- 20 > 10 becomes true
- maxRecord = 20
- maxRecordTotal = 1
- i = 3, minRecord = 5, maxRecord = 20, minRecordTotal = 1, maxRecordTotal = 1
- 20 < 5 becomes false
- 20 > 20 becomes false
- i = 4, minRecord = 5, maxRecord = 20, minRecordTotal = 1, maxRecordTotal = 1
- 4 < 5 becomes true
- minRecord = 4
- minRecordTotal = 2
- 4 > 20 becomes false
- i = 5, minRecord = 4, maxRecord = 20, minRecordTotal = 2, maxRecordTotal = 1
- 5 < 4 becomes false
- 5 > 20 becomes false
- i = 6, minRecord = 4, maxRecord = 20, minRecordTotal = 2, maxRecordTotal = 1
- 2 < 4 becomes true
- minRecord = 2
- minRecordTotal = 3
- 2 > 20 becomes false
- i = 7, minRecord = 2, maxRecord = 20, minRecordTotal = 3, maxRecordTotal = 1
- 25 < 2 becomes false
- 25 > 20 becomes true
- maxRecord = 25
- maxRecordTotal = 2
- i = 8, minRecord = 2, maxRecord = 25, minRecordTotal = 3, maxRecordTotal = 2
- 1 < 2 becomes true
- minRecord = 1
- minRecordTotal = 4
- 1 > 25 becomes false
- minRecordTotal = 4, maxRecordTotal = 2
In above solution we are using 2 if condition, so we can use else if condition for better run time. because both condition can not be true at same time (You can see in output explanation).
Solution 2
for (int i = 1; i < scores.size(); i++) {
if (scores.get(i) < minRecord) {
minRecord = scores.get(i);
minRecordTotal++;
}
else if (scores.get(i) > maxRecord) {
maxRecord = scores.get(i);
maxRecordTotal++;
}
}
Happy Coding
See other HackerRank problem and its solution :
Comments
Post a Comment