-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path022-names-scores.js
More file actions
56 lines (45 loc) · 1.14 KB
/
022-names-scores.js
File metadata and controls
56 lines (45 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Name Scores
* Time Complexity: O(N log N)
* Space Complexity: O(N)
*/
function processData(input) {
const parts = input.trim().split(/\s+/);
let idx = 0;
const N = parseInt(parts[idx++]);
const names = [];
for (let i = 0; i < N; i++) {
names.push(parts[idx++]);
}
names.sort();
const scoreMap = new Map();
function nameValue(str) {
let sum = 0;
for (const ch of str) {
const code = ch.toUpperCase().charCodeAt(0) - 64;
sum += code;
}
return sum;
}
for (let i = 0; i < N; i++) {
const value = nameValue(names[i]);
const position = i + 1;
scoreMap.set(names[i], value * position);
}
const Q = parseInt(parts[idx++]);
const outputs = [];
for (let i = 0; i < Q; i++) {
const qname = parts[idx++];
outputs.push(scoreMap.get(qname));
}
console.log(outputs.join("\n"));
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});