-
Notifications
You must be signed in to change notification settings - Fork 0
/
prg_대충만든자판.js
61 lines (53 loc) · 1.45 KB
/
prg_대충만든자판.js
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
57
58
59
60
61
function solution(keymap, targets) {
let nums = [];
let count = 0;
let result = [];
for (let i = 0; i < targets.length; i++) {
for (let j = 0; j < targets[i].length; j++) {
for (let k = 0; k < keymap.length; k++) {
nums.push(keymap[k].indexOf(targets[i][j]));
}
if (nums.filter((num) => num !== -1).length === 0) {
//어디에도 없는 경우
count = -1;
break;
} else {
nums = nums.filter((num) => num > -1);
count += Math.min(...nums) + 1;
}
nums = [];
}
result.push(count);
count = 0;
}
return result;
}
//더 효율적인 코드
function solution(keymap, targets) {
const minKeyPresses = {};
//ex) minKeyPresses['A'] = 1
for (const row of keymap) {
for (let i = 0; i < row.length; i++) {
const char = row[i];
console.log(char);
if (minKeyPresses[char] === undefined || minKeyPresses[char] > i + 1) {
// 포인트는 이 부분! minKeyPresses[char] > i + 1 , 기존께 현재보다 크다면 현재꺼로 업데이트
minKeyPresses[char] = i + 1;
}
}
}
const result = [];
for (const target of targets) {
let count = 0;
for (const char of target) {
if (minKeyPresses[char] === undefined) {
count = -1;
break;
}
count += minKeyPresses[char];
}
result.push(count);
}
return result;
}
console.log(solution(["ABACD", "BCEFD"], ["ABCD", "AABB"]));