Skip to content

Commit 8e1b174

Browse files
committed
feat: Solve pdf viewer
- Hackerrank
1 parent 724b47f commit 8e1b174

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pdf-viewer.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Hackerrank
3+
* Complete the designerPdfViewer function in the editor below. It should return an integer representing the size of the highlighted area.
4+
* designerPdfViewer has the following parameter(s):
5+
* - h: an array of integers representing the heights of each letter
6+
* - word: a string
7+
*
8+
* Constraints:
9+
* 1. 1 <= h[?] <= 7 where ? is an Englisher lowercase letter
10+
* 2. word contains no more thant 10 letters
11+
*
12+
* This solution got 20 points
13+
* Problem link: http://hr.gs/vnu
14+
*/
15+
16+
/**
17+
* @param {number[]} h Height of each letter in [a-z]
18+
* @param {string} word The word to be evaluated to find the area to be highlighted
19+
* @return {number} Area to be highlighted
20+
*/
21+
function designerPdfViewer(h, word) {
22+
// Edge case
23+
if (!word || word.length === 0) {
24+
return 0;
25+
}
26+
const aCharCode = 'a'.charCodeAt(0);
27+
let maxHeight = -Infinity;
28+
for (let i = 0; i < word.length; i++) {
29+
const height = h[word.charCodeAt(i) % aCharCode];
30+
if (height > maxHeight) {
31+
maxHeight = height;
32+
}
33+
}
34+
return maxHeight * word.length;
35+
}

0 commit comments

Comments
 (0)