Breaking the Records Hacker rank solution – 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.
Input
9
10 5 20 20 4 5 2 25 1
Output
2 4
Explanation:
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 4
as our answer. Note that she did not break her record for the best score during game 3, as her score during that game was not strictly greater than her best record at the time. (Here 0,1,2,3.. are indices of the array s )
Here, I presented my logic. I have written the function breaking records which is giving the desired output.
Breaking the Records Hacker rank Problem Solution:
Problem solution in C++ programming:
vector<int> breakingRecords(vector<int> scores) { int minscr {0[scores.data()]}; int maxscr {0[scores.data()]}; vector<int> result{0,0}; for (const auto &n : scores) { if (n < minscr) { minscr = n; 1[result.data()]++; } if (n > maxscr) { maxscr = n; 0[result.data()]++; } } return result; }
Problem solution in JavaScript programming:
function breakingRecords(scores) { // Write your code here let min = 0; let max = 0; let totalMin = 0; let totalMax = 0; scores.map((num, index) => { if (max === 0 && min === 0) { if (index > 0) { max = num; totalMax++; } else { min = num; max = num; } } else if (num > max) { max = num; totalMax++; } else if (num < min) { min = num; totalMin++; } }); return [totalMax, totalMin]; }
Problem solution in Python programming:
def breakingRecords(scores): min = scores[0] max = scores[0] break_rec = [0, 0] for i in scores: if i < min: break_rec[1] += 1 min = i if i > max: break_rec[0] += 1 max = i print(break_rec[0], break_rec[1])
Problem solution in Ruby programming:
def breakingRecords(scores) maxmin = [0, 0] last_max, last_min = scores[0], scores[0] scores.each do |score| if score > last_max maxmin[0] += 1 last_max = score elsif score < last_min maxmin[1] += 1 last_min = score end end maxmin end
Leave a Reply