File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed
Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ function solution ( name , yearning , photo ) {
2+ const scoreMap = { } ;
3+
4+ name . forEach ( ( n , i ) => scoreMap [ n ] = yearning [ i ] ) ;
5+
6+ return photo . map ( p =>
7+ p . reduce ( ( sum , person ) => sum + ( scoreMap [ person ] || 0 ) , 0 )
8+ ) ;
9+ }
Original file line number Diff line number Diff line change 1+ function solution ( numbers , hand ) {
2+ // 키패드를 열 기준 2차원 배열로 정의
3+ const keypad = [
4+ [ 1 , 4 , 7 , "*" ] , // 왼쪽 열
5+ [ 2 , 5 , 8 , 0 ] , // 가운데 열
6+ [ 3 , 6 , 9 , "#" ] // 오른쪽 열
7+ ]
8+
9+ // 각 숫자의 좌표를 Map으로 저장
10+ const getPos = new Map ( )
11+ // 열과 행 기준으로 좌표 저장
12+ for ( let x = 0 ; x < keypad . length ; x ++ ) {
13+ for ( let y = 0 ; y < keypad [ x ] . length ; y ++ ) {
14+ getPos . set ( keypad [ x ] [ y ] , [ x , y ] )
15+ }
16+ }
17+
18+ let left = getPos . get ( "*" ) // 왼손 시작 위치
19+ let right = getPos . get ( "#" ) // 오른손 시작 위치
20+
21+ // 두 좌표 사이의 거리 계산
22+ const getDist = ( [ x1 , y1 ] , [ x2 , y2 ] ) =>
23+ Math . abs ( x1 - x2 ) + Math . abs ( y1 - y2 )
24+
25+ return numbers . map ( num => {
26+ const target = getPos . get ( num )
27+
28+ // 첫 번째 열은 무조건 왼손
29+ if ( target [ 0 ] === 0 ) {
30+ left = target
31+ return "L"
32+ }
33+
34+ // 세 번째 열은 무조건 오른손
35+ if ( target [ 0 ] === 2 ) {
36+ right = target
37+ return "R"
38+ }
39+
40+ // 가운데 열인 경우 거리 계산
41+ const leftDist = getDist ( left , target )
42+ const rightDist = getDist ( right , target )
43+
44+ // 거리가 같으면 주손잡이 기준
45+ if ( leftDist === rightDist ) {
46+ if ( hand === "right" ) {
47+ right = target
48+ return "R"
49+ } else {
50+ left = target
51+ return "L"
52+ }
53+ }
54+
55+ // 거리가 다르면 가까운 손 사용
56+ if ( leftDist < rightDist ) {
57+ left = target
58+ return "L"
59+ } else {
60+ right = target
61+ return "R"
62+ }
63+ } ) . join ( "" )
64+ }
You can’t perform that action at this time.
0 commit comments