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
62
63
| #include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
string solution(vector<int> numbers, string hand) {
unordered_map<int, pair<int, int>> keypad = {
{1, {0, 0}}, {2, {0, 1}}, {3, {0, 2}},
{4, {1, 0}}, {5, {1, 1}}, {6, {1, 2}},
{7, {2, 0}}, {8, {2, 1}}, {9, {2, 2}},
{10, {3, 0}}, {0, {3, 1}}, {11, {3, 2}}
};
pair<int, int> left = keypad[10];
pair<int ,int> right = keypad[11];
string answer = "";
for (auto num : numbers) {
if (num == 1 or num == 4 or num == 7) {
left = keypad[num];
answer += "L";
continue;
}
if (num == 3 or num == 6 or num == 9) {
right = keypad[num];
answer += "R";
continue;
}
pair<int ,int> target = keypad[num];
int dl = abs(left.fi - target.fi) + abs(left.se - target.se);
int dr = abs(right.fi - target.fi) + abs(right.se - target.se);
if (dl < dr) {
left = keypad[num];
answer += "L";
continue;
}
if (dr < dl) {
right = keypad[num];
answer += "R";
continue;
}
if (dr == dl) {
if (hand == "left") {
left = keypad[num];
answer += "L";
} else {
right = keypad[num];
answer += "R";
}
continue;
}
}
return answer;
}
|