Reputation: 319
Let's say I am trying to solve this problem:
https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem
I wrote this code:
function breakingRecords(arr) {
let n = arr.length;
let best = 0;
let worst = 100000000; // 10^8 (constraints :. cannot get higher)
let rec1 = 0; // times broken best
let rec2 = 0; // times broken worst
for (let i = 0; i < n; i++) {
if (arr[i] > best) {
best = arr[i];
if (i != 0) {
rec1++;
}
} else if (arr[i] < worst) {
if (i != 0) {
worst = arr[i];
rec2++;
}
}
}
return [rec2, rec1];
}
//And for one test case, I say:
console.log(breakingRecords([3, 4, 21, 36, 10, 28, 35, 5, 24, 42]))
The output is [4, 2]
, which is equal to the expected output (the expected output is 4 2
, but they add the space themselves). But, when I enter this code, my output is shown as 4 0
. I test it in my browser and I still get [4, 2]
.
Can someone explain why this is so, and what I can do to fix it? Refreshing doesn't work, and neither does closing and reopening.
Upvotes: 2
Views: 334
Reputation: 41
Maybe you can run this one
function breakingRecords(scores) {
let best = 0;
let worst = 0;
let bestScore = scores[0];
let worstScore = scores[0];
const lengthOfData = scores.length;
for(let i = 1; i < scores.length; i++) {
if (scores[i] > bestScore) {
bestScore = scores[i];
best++;
continue;
}
if (scores[i] < worstScore) {
worstScore = scores[i]
worst++;
continue;
}
}
return [best, worst];
}
Upvotes: 0
Reputation: 18619
Actually, your code was wrong. The expected results were 4 0
, but your code returned 4 2
.
That was because of the first iteration. The first match's result is both the current best and worst but doesn't count into the score. Your code set it only as the best score (because of the else if
), but not as the worst.
So, let's treat this as a special case, and shift
the first element out of the array (we can do that safely, as constraints list that the array must contain at least 1 element):
function breakingRecords(arr) {
let best;
let worst;
best = worst = arr.shift() //Get and remove the first element
let n = arr.length;
let recBest = 0; //Use more descriptive names here
let recWorst = 0; //...and here as well
for (let i = 0; i < n; i++) {
if (arr[i] > best) {
best = arr[i];
recBest++;
} else if (arr[i] < worst) {
worst = arr[i];
recWorst++;
}
}
return [recBest, recWorst];
}
//And for one test case, I say:
console.log(breakingRecords([3, 4, 21, 36, 10, 28, 35, 5, 24, 42])) //[4, 0]
Upvotes: 3
Reputation: 15268
You need to initialize the values to the first score for comparison.
function breakingRecords(arr) {
let n = arr.length;
let best = 0;
let worst = 100000000; // 10^8 (constraints :. cannot get higher)
let rec1 = 0; // times broken best
let rec2 = 0; // times broken worst
worst = best = arr[0]; // initialize worst and best to first score
for (let i = 0; i < n; i++) {
if (arr[i] > best) {
if (i != 0) {
best = arr[i];
rec1++;
}
} else if (arr[i] < worst) {
if (i != 0) {
worst = arr[i];
rec2++;
}
}
}
return [rec1, rec2]; // order of records is also wrong?
}
//And for one test case, I say:
console.log(breakingRecords([3, 4, 21, 36, 10, 28, 35, 5, 24, 42]))
Upvotes: 2