Problem overview
Three parallel arrays describe the brands: ratings[i] and reviews[i] are the rating and the review count of brands[i]. The program prints the top k brands, best first, one per line, in the form rank. Brand (rating, reviews), with ranks starting at 1.
Rating decides first. If two brands have the same rating, the one with more reviews wins. If both are equal, the one listed first wins. The constant k is never larger than the number of brands.
In the starting data NeoByte and MegaCorp both have a rating of 5, with 500 and 150 reviews, and TechPro and Zenith both have a rating of 4. With k = 3 the output is:
1. NeoByte (5, 500)
2. MegaCorp (5, 150)
3. Zenith (4, 300)
Approach: the same repeated search, a second comparison
The structure is the repeated search from version 4: for each rank, scan all the brands that are not used yet, keep the position of the best one, mark it as used and add a line. Only the question "is this brand better than the current best?" changes. This version doesn't filter anything.
That question now has three cases, checked in order:
- Nothing has been found in this round yet (
bestIndexis-1). This brand is the best so far, because it has nobody to beat. - Otherwise, its rating is higher than the best one's. A higher rating wins before the reviews count at all.
- Otherwise, the ratings are equal and this brand has more reviews. Reviews only break a tie, so both parts of the condition must hold.
If none of the three holds, the current best stays. That includes a brand with exactly the same rating and review count: it isn't strictly better, so the earlier one keeps the lead, which is the third rule.
The chain is written with else if, so a brand triggers at most one case, and the order of the cases matters.
The second case needs a rating higher than the best, and the third needs an equal rating, so the two never both hold for the same brand.
In the starting data, the first round picks NeoByte: its rating of 5 ties MegaCorp's, and 500 reviews beat 150. In the second round NeoByte is used, so MegaCorp is the best rating that is left. In the third round the two 5s are gone, and among the 4s Zenith's 300 reviews beat TechPro's 200.
Step by step
- Create the flags. A
boolean[]namedused, withfalsefor each rating. - Create the output list. An empty
string[]namedlines. - Write the rank loop.
rankgoes from 1 up to and includingk. It is also the number printed in front of the brand. - Reset the best. Inside it,
bestIndexstarts at-1, so each round starts over. - Write the inner loop, where
itakes every position ofratings. - Skip used brands with an
ifonused[i]being false. - First case. Inside it, if
bestIndexis-1, set it toi. - Second case, an
else if:ratings[i]is greater thanratings[bestIndex]. SetbestIndextoi. - Third case, another
else if: the ratings are equal andreviews[i]is greater thanreviews[bestIndex]. SetbestIndextoi. - Mark the winner. After the inner loop, set
used[bestIndex]totrue. - Add the line.
pushthe rank, a dot and a space, the name, then the rating and the review count in parentheses, separated by a comma and a space. - Print
lines.join("\n")withconsole.logafter the loop.
The solution in TypeScript
const brands: string[] = ["TechPro", "MegaCorp", "NeoByte", "Quantum", "Zenith"];
const ratings: number[] = [4, 5, 5, 3, 4];
const reviews: number[] = [200, 150, 500, 100, 300];
const k: number = 3;
const used: boolean[] = new Array(ratings.length).fill(false);
const lines: string[] = [];
for (let rank = 1; rank <= k; rank++) {
let bestIndex: number = -1;
for (let i = 0; i < ratings.length; i++) {
if (!used[i]) {
if (bestIndex === -1) {
bestIndex = i;
}
else if (ratings[i] > ratings[bestIndex]) {
bestIndex = i;
}
else if (ratings[i] === ratings[bestIndex] && reviews[i] > reviews[bestIndex]) {
bestIndex = i;
}
}
}
used[bestIndex] = true;
lines.push(rank + ". " + brands[bestIndex] + " (" + ratings[bestIndex] + ", " + reviews[bestIndex] + ")");
}
console.log(lines.join("\n"));
Details worth knowing:
===with numbers.ratings[i] === ratings[bestIndex]compares two numbers without any conversion.&&in the third case. Both sides must be true: same rating, and strictly more reviews.elseon a new line. The solution putselse ifon its own line after the closing brace. TypeScript doesn't mind, because whitespace between the brace andelseis ignored.- Parallel arrays. The three arrays are tied together only by position. The same index reads the name, the rating and the review count of one brand.
- Types are erased. The annotations help the compiler and don't exist in the JavaScript that runs.
Complexity
Time: O(k * n). Each of the k rounds scans all n brands, and each comparison takes constant time. For k equal to n this is O(n²).
Space: O(n). The used array has n entries, and lines holds k strings.
Edge cases and common mistakes
- Dropping the rating test from the third case. If the third condition is only
reviews[i] > reviews[bestIndex], a brand with a lower rating can beat a better one just because it has more reviews. For the starting data andk = 3the output becomes NeoByte (5, 500), Zenith (4, 300), then MegaCorp (5, 150), which is wrong. - Using
||to join the rating and the review count. A single condition such as "higher rating, or more reviews" gives the same wrong order as above. - Equal rating and equal reviews. With reviews of
[200, 150, 150, 100, 300], MegaCorp and NeoByte tie on both numbers. The output is MegaCorp (5, 150), NeoByte (5, 150) and Zenith (4, 300): the one listed first wins. If the third case used>=, NeoByte would come first. - The tie-break going the other way. With reviews of
[200, 500, 150, 100, 300], MegaCorp has 500 reviews and NeoByte only 150, so MegaCorp is first, with1. MegaCorp (5, 500)and2. NeoByte (5, 150). The order in the arrays only matters when both numbers are equal. - Forgetting to mark the winner. Without
used[bestIndex] = true, every round returns the same brand. - Extreme values of
k.k = 1prints only1. NeoByte (5, 500). Withk = 5all brands are printed, and the last two lines are4. TechPro (4, 200)and5. Quantum (3, 100).
Testing
- The starting data with
k = 3prints1. NeoByte (5, 500),2. MegaCorp (5, 150)and3. Zenith (4, 300). k = 1prints1. NeoByte (5, 500).k = 5prints NeoByte, MegaCorp, Zenith, TechPro and Quantum, in that order.- Reviews
[200, 500, 150, 100, 300]withk = 3print1. MegaCorp (5, 500),2. NeoByte (5, 150)and3. Zenith (4, 300).
