Created
February 7, 2018 06:12
-
-
Save caglarorhan/d1129bc841ea892f8c1f78fc27562d7d to your computer and use it in GitHub Desktop.
Breaking the Records - from https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.stdin.resume(); | |
process.stdin.setEncoding('ascii'); | |
var input_stdin = ""; | |
var input_stdin_array = ""; | |
var input_currentline = 0; | |
process.stdin.on('data', function (data) { | |
input_stdin += data; | |
}); | |
process.stdin.on('end', function () { | |
input_stdin_array = input_stdin.split("\n"); | |
main(); | |
}); | |
function readLine() { | |
return input_stdin_array[input_currentline++]; | |
} | |
/////////////// ignore above this line //////////////////// | |
function breakingRecords(score) { | |
// Complete this function | |
let breakHighCount=0; let newHigh=-1; | |
let breakLowCount=0; let newLow=100000001; | |
//let scores=[]; | |
score.forEach(function(i){ | |
if(i>newHigh){ | |
newHigh=i; | |
breakHighCount++; | |
} | |
if(i<newLow){ | |
newLow=i; | |
breakLowCount++; | |
} | |
}); | |
breakHighCount = breakHighCount<0?0:breakHighCount-1; | |
breakLowCount = breakLowCount<0?0:breakLowCount-1; | |
return [breakHighCount,breakLowCount]; | |
} | |
function main() { | |
var n = parseInt(readLine()); | |
score = readLine().split(' '); | |
score = score.map(Number); | |
var result = breakingRecords(score); | |
console.log(result.join(" ")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maria plays games of college basketball in a season. Because she wants to go pro, she tracks her points scored per game sequentially in an array defined as . After each game , she checks to see if score breaks her record for most or least points scored so far during that season.
Given Maria's array of for a season of games, find and print the number of times she breaks her record for most and least points scored during the season.
Note: Assume her records for most and least points at the start of the season are the number of points scored during the first game of the season.
Input Format
The first line contains an integer denoting (the number of games).
The second line contains space-separated integers describing the respective values of .
Constraints
Output Format
Print two space-seperated integers describing the respective numbers of times her best (highest) score increased and her worst (lowest) score decreased.
Sample Input 0
9
10 5 20 20 4 5 2 25 1
Sample Output 0
2 4
Explanation 0
The diagram below depicts the number of times Maria broke her best and worst records throughout the season:
image
She broke her best record twice (after games and ) and her worst record four times (after games , , , and ), so we print 2 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.
Sample Input 1
10
3 4 21 36 10 28 35 5 24 42
Sample Output 1
4 0
Explanation 1
The diagram below depicts the number of times Maria broke her best and worst records throughout the season:
image
She broke her best record four times (after games , , , and ) and her worst record zero times (no score during the season was lower than the one she earned during her first game), so we print 4 0 as our answer.