Skip to content

Commit d6e50f1

Browse files
committed
feat: update solutions to lc problems: No.1158,1165
* No.1158.Market Analysis I * No.1165.Single-Row Keyboard
1 parent 4c365e8 commit d6e50f1

File tree

10 files changed

+189
-143
lines changed

10 files changed

+189
-143
lines changed

solution/1100-1199/1158.Market Analysis I/README.md

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,29 +114,17 @@ Items 表:
114114
### **SQL**
115115

116116
```sql
117-
SELECT user_id AS buyer_id,
118-
join_date,
119-
COUNT(order_id) AS orders_in_2019
120-
FROM users AS u
121-
LEFT JOIN orders AS o ON u.user_id = o.buyer_id
122-
AND YEAR(order_date) = 2019
123-
GROUP BY user_id;
124-
```
125-
126-
```sql
117+
# Write your MySQL query statement below
127118
SELECT
128-
user_id AS buyer_id,
129-
join_date,
130-
(
131-
SELECT
132-
COUNT(*)
133-
FROM
134-
orders AS o
135-
WHERE
136-
u.user_id = o.buyer_id AND YEAR(order_date) = 2019
137-
) AS orders_in_2019
119+
u.user_id AS buyer_id,
120+
u.join_date,
121+
count( order_id ) AS orders_in_2019
138122
FROM
139-
users AS u;
123+
Users u
124+
LEFT JOIN Orders o ON u.user_id = o.buyer_id
125+
AND YEAR ( order_date ) = 2019
126+
GROUP BY
127+
user_id;
140128
```
141129

142130
<!-- tabs:end -->

solution/1100-1199/1158.Market Analysis I/README_EN.md

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,17 @@ Items table:
111111
### **SQL**
112112

113113
```sql
114-
SELECT user_id AS buyer_id,
115-
join_date,
116-
COUNT(order_id) AS orders_in_2019
117-
FROM users AS u
118-
LEFT JOIN orders AS o ON u.user_id = o.buyer_id
119-
AND YEAR(order_date) = 2019
120-
GROUP BY user_id;
121-
```
122-
123-
```sql
114+
# Write your MySQL query statement below
124115
SELECT
125-
user_id AS buyer_id,
126-
join_date,
127-
(
128-
SELECT
129-
COUNT(*)
130-
FROM
131-
orders AS o
132-
WHERE
133-
u.user_id = o.buyer_id AND YEAR(order_date) = 2019
134-
) AS orders_in_2019
116+
u.user_id AS buyer_id,
117+
u.join_date,
118+
count( order_id ) AS orders_in_2019
135119
FROM
136-
users AS u;
120+
Users u
121+
LEFT JOIN Orders o ON u.user_id = o.buyer_id
122+
AND YEAR ( order_date ) = 2019
123+
GROUP BY
124+
user_id;
137125
```
138126

139127
<!-- tabs:end -->
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
SELECT user_id AS buyer_id,
2-
join_date,
3-
COUNT(order_id) AS orders_in_2019
4-
FROM users AS u
5-
LEFT JOIN orders AS o ON u.user_id = o.buyer_id
6-
AND YEAR(order_date) = 2019
7-
GROUP BY user_id;
1+
# Write your MySQL query statement below
2+
SELECT
3+
u.user_id AS buyer_id,
4+
u.join_date,
5+
count( order_id ) AS orders_in_2019
6+
FROM
7+
Users u
8+
LEFT JOIN Orders o ON u.user_id = o.buyer_id
9+
AND YEAR ( order_date ) = 2019
10+
GROUP BY
11+
user_id;

solution/1100-1199/1165.Single-Row Keyboard/README.md

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48-
哈希表实现。
48+
**方法一:哈希表或数组**
49+
50+
我们可以用哈希表或者一个长度为 $26$ 的数组 $pos$ 来存储每个字符在键盘上的位置,其中 $pos[c]$ 表示字符 $c$ 在键盘上的位置。
51+
52+
然后我们遍历字符串 $word$,用一个变量 $i$ 记录当前手指所在的位置,初始时 $i = 0$。每次计算当前字符 $c$ 在键盘上的位置 $j$,并将答案增加 $|i - j|$,然后将 $i$ 更新为 $j$。继续遍历下一个字符,直到遍历完整个字符串 $word$。
53+
54+
遍历完字符串 $word$ 之后,即可得到答案。
55+
56+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $word$ 的长度;而 $C$ 为字符集大小,本题中 $C = 26$。
4957

5058
<!-- tabs:start -->
5159

@@ -56,12 +64,12 @@
5664
```python
5765
class Solution:
5866
def calculateTime(self, keyboard: str, word: str) -> int:
59-
index = {c: i for i, c in enumerate(keyboard)}
60-
res = t = 0
67+
pos = {c: i for i, c in enumerate(keyboard)}
68+
ans = i = 0
6169
for c in word:
62-
res += abs(index[c] - t)
63-
t = index[c]
64-
return res
70+
ans += abs(pos[c] - i)
71+
i = pos[c]
72+
return ans
6573
```
6674

6775
### **Java**
@@ -71,16 +79,17 @@ class Solution:
7179
```java
7280
class Solution {
7381
public int calculateTime(String keyboard, String word) {
74-
Map<Character, Integer> index = new HashMap<>();
75-
for (int i = 0; i < keyboard.length(); ++i) {
76-
index.put(keyboard.charAt(i), i);
82+
int[] pos = new int[26];
83+
for (int i = 0; i < 26; ++i) {
84+
pos[keyboard.charAt(i) - 'a'] = i;
7785
}
78-
int res = 0, t = 0;
79-
for (char c : word.toCharArray()) {
80-
res += Math.abs(index.get(c) - t);
81-
t = index.get(c);
86+
int ans = 0, i = 0;
87+
for (int k = 0; k < word.length(); ++k) {
88+
int j = pos[word.charAt(k) - 'a'];
89+
ans += Math.abs(i - j);
90+
i = j;
8291
}
83-
return res;
92+
return ans;
8493
}
8594
}
8695
```
@@ -91,35 +100,36 @@ class Solution {
91100
class Solution {
92101
public:
93102
int calculateTime(string keyboard, string word) {
94-
unordered_map<char, int> index;
95-
for (int i = 0; i < keyboard.size(); ++i) {
96-
index[keyboard[i]] = i;
103+
int pos[26];
104+
for (int i = 0; i < 26; ++i) {
105+
pos[keyboard[i] - 'a'] = i;
97106
}
98-
int res = 0, t = 0;
99-
for (char c : word) {
100-
res += abs(index[c] - t);
101-
t = index[c];
107+
int ans = 0, i = 0;
108+
for (char& c : word) {
109+
int j = pos[c - 'a'];
110+
ans += abs(i - j);
111+
i = j;
102112
}
103-
return res;
113+
return ans;
104114
}
105115
};
106116
```
107117
108118
### **Go**
109119
110120
```go
111-
func calculateTime(keyboard string, word string) int {
112-
index := map[byte]int{}
113-
for i := 0; i < len(keyboard); i++ {
114-
index[keyboard[i]] = i
121+
func calculateTime(keyboard string, word string) (ans int) {
122+
pos := [26]int{}
123+
for i, c := range keyboard {
124+
pos[c-'a'] = i
115125
}
116-
res := 0
117-
t := 0
118-
for i := 0; i < len(word); i++ {
119-
res += abs(index[word[i]] - t)
120-
t = index[word[i]]
126+
i := 0
127+
for _, c := range word {
128+
j := pos[c-'a']
129+
ans += abs(i - j)
130+
i = j
121131
}
122-
return res
132+
return
123133
}
124134
125135
func abs(x int) int {
@@ -130,6 +140,25 @@ func abs(x int) int {
130140
}
131141
```
132142

143+
### **TypeScript**
144+
145+
```ts
146+
function calculateTime(keyboard: string, word: string): number {
147+
const pos: number[] = Array(26).fill(0);
148+
for (let i = 0; i < 26; ++i) {
149+
pos[keyboard.charCodeAt(i) - 97] = i;
150+
}
151+
let ans = 0;
152+
let i = 0;
153+
for (const c of word) {
154+
const j = pos[c.charCodeAt(0) - 97];
155+
ans += Math.abs(i - j);
156+
i = j;
157+
}
158+
return ans;
159+
}
160+
```
161+
133162
### **...**
134163

135164
```

solution/1100-1199/1165.Single-Row Keyboard/README_EN.md

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,30 @@ Total time = 2 + 1 + 1 = 4.
4646
```python
4747
class Solution:
4848
def calculateTime(self, keyboard: str, word: str) -> int:
49-
index = {c: i for i, c in enumerate(keyboard)}
50-
res = t = 0
49+
pos = {c: i for i, c in enumerate(keyboard)}
50+
ans = i = 0
5151
for c in word:
52-
res += abs(index[c] - t)
53-
t = index[c]
54-
return res
52+
ans += abs(pos[c] - i)
53+
i = pos[c]
54+
return ans
5555
```
5656

5757
### **Java**
5858

5959
```java
6060
class Solution {
6161
public int calculateTime(String keyboard, String word) {
62-
Map<Character, Integer> index = new HashMap<>();
63-
for (int i = 0; i < keyboard.length(); ++i) {
64-
index.put(keyboard.charAt(i), i);
62+
int[] pos = new int[26];
63+
for (int i = 0; i < 26; ++i) {
64+
pos[keyboard.charAt(i) - 'a'] = i;
6565
}
66-
int res = 0, t = 0;
67-
for (char c : word.toCharArray()) {
68-
res += Math.abs(index.get(c) - t);
69-
t = index.get(c);
66+
int ans = 0, i = 0;
67+
for (int k = 0; k < word.length(); ++k) {
68+
int j = pos[word.charAt(k) - 'a'];
69+
ans += Math.abs(i - j);
70+
i = j;
7071
}
71-
return res;
72+
return ans;
7273
}
7374
}
7475
```
@@ -79,35 +80,36 @@ class Solution {
7980
class Solution {
8081
public:
8182
int calculateTime(string keyboard, string word) {
82-
unordered_map<char, int> index;
83-
for (int i = 0; i < keyboard.size(); ++i) {
84-
index[keyboard[i]] = i;
83+
int pos[26];
84+
for (int i = 0; i < 26; ++i) {
85+
pos[keyboard[i] - 'a'] = i;
8586
}
86-
int res = 0, t = 0;
87-
for (char c : word) {
88-
res += abs(index[c] - t);
89-
t = index[c];
87+
int ans = 0, i = 0;
88+
for (char& c : word) {
89+
int j = pos[c - 'a'];
90+
ans += abs(i - j);
91+
i = j;
9092
}
91-
return res;
93+
return ans;
9294
}
9395
};
9496
```
9597
9698
### **Go**
9799
98100
```go
99-
func calculateTime(keyboard string, word string) int {
100-
index := map[byte]int{}
101-
for i := 0; i < len(keyboard); i++ {
102-
index[keyboard[i]] = i
101+
func calculateTime(keyboard string, word string) (ans int) {
102+
pos := [26]int{}
103+
for i, c := range keyboard {
104+
pos[c-'a'] = i
103105
}
104-
res := 0
105-
t := 0
106-
for i := 0; i < len(word); i++ {
107-
res += abs(index[word[i]] - t)
108-
t = index[word[i]]
106+
i := 0
107+
for _, c := range word {
108+
j := pos[c-'a']
109+
ans += abs(i - j)
110+
i = j
109111
}
110-
return res
112+
return
111113
}
112114
113115
func abs(x int) int {
@@ -118,6 +120,25 @@ func abs(x int) int {
118120
}
119121
```
120122

123+
### **TypeScript**
124+
125+
```ts
126+
function calculateTime(keyboard: string, word: string): number {
127+
const pos: number[] = Array(26).fill(0);
128+
for (let i = 0; i < 26; ++i) {
129+
pos[keyboard.charCodeAt(i) - 97] = i;
130+
}
131+
let ans = 0;
132+
let i = 0;
133+
for (const c of word) {
134+
const j = pos[c.charCodeAt(0) - 97];
135+
ans += Math.abs(i - j);
136+
i = j;
137+
}
138+
return ans;
139+
}
140+
```
141+
121142
### **...**
122143

123144
```
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
class Solution {
22
public:
33
int calculateTime(string keyboard, string word) {
4-
unordered_map<char, int> index;
5-
for (int i = 0; i < keyboard.size(); ++i) {
6-
index[keyboard[i]] = i;
4+
int pos[26];
5+
for (int i = 0; i < 26; ++i) {
6+
pos[keyboard[i] - 'a'] = i;
77
}
8-
int res = 0, t = 0;
9-
for (char c : word) {
10-
res += abs(index[c] - t);
11-
t = index[c];
8+
int ans = 0, i = 0;
9+
for (char& c : word) {
10+
int j = pos[c - 'a'];
11+
ans += abs(i - j);
12+
i = j;
1213
}
13-
return res;
14+
return ans;
1415
}
1516
};

0 commit comments

Comments
 (0)