Skip to content

Commit aef9e1c

Browse files
committed
feat: add solutions to lcs problems
* No.01. 下载插件 * No.02. 完成一半题目
1 parent 89f02e5 commit aef9e1c

File tree

11 files changed

+133
-73
lines changed

11 files changed

+133
-73
lines changed

lcs/LCS 01. 下载插件/README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@
4545

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

48+
**方法一:贪心**
49+
4850
如果不能在一分钟内下载完,那么可以先加速,循环直至能在一分钟内下载完。那么“循环次数 + 1”即为最少消耗的分钟数。
4951

52+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为插件数量。
53+
5054
<!-- tabs:start -->
5155

5256
### **Python3**
@@ -86,7 +90,9 @@ class Solution {
8690
public:
8791
int leastMinutes(int n) {
8892
int ans = 1;
89-
for (int speed = 1; speed < n; speed <<= 1) ++ans;
93+
for (int speed = 1; speed < n; speed <<= 1) {
94+
++ans;
95+
}
9096
return ans;
9197
}
9298
};
@@ -120,6 +126,18 @@ var leastMinutes = function (n) {
120126
};
121127
```
122128

129+
### **TypeScript**
130+
131+
```ts
132+
function leastMinutes(n: number): number {
133+
let ans = 1;
134+
for (let speed = 1; speed < n; speed <<= 1) {
135+
++ans;
136+
}
137+
return ans;
138+
}
139+
```
140+
123141
### **...**
124142

125143
```

lcs/LCS 01. 下载插件/Solution.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class Solution {
22
public:
33
int leastMinutes(int n) {
44
int ans = 1;
5-
for (int speed = 1; speed < n; speed <<= 1) ++ans;
5+
for (int speed = 1; speed < n; speed <<= 1) {
6+
++ans;
7+
}
68
return ans;
79
}
810
};

lcs/LCS 01. 下载插件/Solution.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function leastMinutes(n: number): number {
2+
let ans = 1;
3+
for (let speed = 1; speed < n; speed <<= 1) {
4+
++ans;
5+
}
6+
return ans;
7+
}

lcs/LCS 02. 完成一半题目/README.md

+55-35
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636

3737
<!-- 这里可写通用的实现逻辑 -->
3838

39-
统计各个问题类型出现的次数,按照次数降序排列。
39+
**方法一:计数 + 排序**
4040

41-
然后依次选择问题类型,直至满足条件。
41+
我们可以用哈希表或数组 `cnt` 统计每种知识点类型的题目数量,然后对 `cnt` 进行排序,从大到小遍历 `cnt`,直到遍历的题目数量之和大于等于 `n` 即可,此时遍历的次数即为所求。
42+
43+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为 `questions` 的长度。
4244

4345
<!-- tabs:start -->
4446

@@ -49,14 +51,13 @@
4951
```python
5052
class Solution:
5153
def halfQuestions(self, questions: List[int]) -> int:
52-
counter = Counter(questions)
53-
n = len(questions) >> 1
54-
ans = 0
55-
for i, v in counter.most_common():
54+
cnt = Counter(questions)
55+
ans, n = 0, len(questions) >> 1
56+
for _, v in cnt.most_common():
5657
ans += 1
57-
if v >= n:
58-
return ans
5958
n -= v
59+
if n <= 0:
60+
break
6061
return ans
6162
```
6263

@@ -67,16 +68,16 @@ class Solution:
6768
```java
6869
class Solution {
6970
public int halfQuestions(int[] questions) {
70-
int[] counter = new int[1010];
71-
for (int q : questions) {
72-
++counter[q];
71+
int[] cnt = new int[1010];
72+
for (int x : questions) {
73+
++cnt[x];
7374
}
74-
Arrays.sort(counter);
75+
Arrays.sort(cnt);
7576
int ans = 0;
7677
int n = questions.length >> 1;
77-
for (int i = counter.length - 1; n > 0; --i) {
78+
for (int i = cnt.length - 1; n > 0; --i) {
7879
++ans;
79-
n -= counter[i];
80+
n -= cnt[i];
8081
}
8182
return ans;
8283
}
@@ -89,14 +90,15 @@ class Solution {
8990
class Solution {
9091
public:
9192
int halfQuestions(vector<int>& questions) {
92-
vector<int> counter(1010);
93-
for (int q : questions) ++counter[q];
94-
int n = questions.size() >> 1;
95-
sort(counter.begin(), counter.end());
96-
int ans = 0;
97-
for (int i = counter.size() - 1; n > 0; --i) {
93+
int cnt[1001]{};
94+
for (int& x : questions) {
95+
++cnt[x];
96+
}
97+
sort(cnt, cnt + 1001);
98+
int ans = 0, n = questions.size() / 2;
99+
for (int i = 1000; n > 0; --i) {
98100
++ans;
99-
n -= counter[i];
101+
n -= cnt[i];
100102
}
101103
return ans;
102104
}
@@ -106,19 +108,18 @@ public:
106108
### **Go**
107109
108110
```go
109-
func halfQuestions(questions []int) int {
110-
counter := make([]int, 1010)
111-
for _, q := range questions {
112-
counter[q]++
111+
func halfQuestions(questions []int) (ans int) {
112+
cnt := make([]int, 1010)
113+
for _, x := range questions {
114+
cnt[x]++
113115
}
114116
n := len(questions) >> 1
115-
sort.Ints(counter)
116-
ans := 0
117-
for i := len(counter) - 1; n > 0; i-- {
117+
sort.Ints(cnt)
118+
for i := len(cnt) - 1; n > 0; i-- {
118119
ans++
119-
n -= counter[i]
120+
n -= cnt[i]
120121
}
121-
return ans
122+
return
122123
}
123124
```
124125

@@ -130,21 +131,40 @@ func halfQuestions(questions []int) int {
130131
* @return {number}
131132
*/
132133
var halfQuestions = function (questions) {
133-
let counter = new Array(1010).fill(0);
134-
for (const q of questions) {
135-
++counter[q];
134+
const cnt = new Array(1010).fill(0);
135+
for (const x of questions) {
136+
++cnt[x];
136137
}
137-
counter.sort((a, b) => b - a);
138+
cnt.sort((a, b) => b - a);
138139
let ans = 0;
139140
let n = questions.length >> 1;
140141
for (let i = 0; n > 0; ++i) {
141142
++ans;
142-
n -= counter[i];
143+
n -= cnt[i];
143144
}
144145
return ans;
145146
};
146147
```
147148

149+
### **TypeScript**
150+
151+
```ts
152+
function halfQuestions(questions: number[]): number {
153+
const cnt = new Array(1010).fill(0);
154+
for (const x of questions) {
155+
++cnt[x];
156+
}
157+
cnt.sort((a, b) => b - a);
158+
let ans = 0;
159+
let n = questions.length >> 1;
160+
for (let i = 0; n > 0; ++i) {
161+
++ans;
162+
n -= cnt[i];
163+
}
164+
return ans;
165+
}
166+
```
167+
148168
### **...**
149169

150170
```

lcs/LCS 02. 完成一半题目/Solution.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class Solution {
22
public:
33
int halfQuestions(vector<int>& questions) {
4-
vector<int> counter(1010);
5-
for (int q : questions) ++counter[q];
6-
int n = questions.size() >> 1;
7-
sort(counter.begin(), counter.end());
8-
int ans = 0;
9-
for (int i = counter.size() - 1; n > 0; --i) {
4+
int cnt[1001]{};
5+
for (int& x : questions) {
6+
++cnt[x];
7+
}
8+
sort(cnt, cnt + 1001);
9+
int ans = 0, n = questions.size() / 2;
10+
for (int i = 1000; n > 0; --i) {
1011
++ans;
11-
n -= counter[i];
12+
n -= cnt[i];
1213
}
1314
return ans;
1415
}
+8-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
func halfQuestions(questions []int) int {
2-
counter := make([]int, 1010)
3-
for _, q := range questions {
4-
counter[q]++
1+
func halfQuestions(questions []int) (ans int) {
2+
cnt := make([]int, 1010)
3+
for _, x := range questions {
4+
cnt[x]++
55
}
66
n := len(questions) >> 1
7-
sort.Ints(counter)
8-
ans := 0
9-
for i := len(counter) - 1; n > 0; i-- {
7+
sort.Ints(cnt)
8+
for i := len(cnt) - 1; n > 0; i-- {
109
ans++
11-
n -= counter[i]
10+
n -= cnt[i]
1211
}
13-
return ans
12+
return
1413
}

lcs/LCS 02. 完成一半题目/Solution.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public int halfQuestions(int[] questions) {
3-
int[] counter = new int[1010];
4-
for (int q : questions) {
5-
++counter[q];
3+
int[] cnt = new int[1010];
4+
for (int x : questions) {
5+
++cnt[x];
66
}
7-
Arrays.sort(counter);
7+
Arrays.sort(cnt);
88
int ans = 0;
99
int n = questions.length >> 1;
10-
for (int i = counter.length - 1; n > 0; --i) {
10+
for (int i = cnt.length - 1; n > 0; --i) {
1111
++ans;
12-
n -= counter[i];
12+
n -= cnt[i];
1313
}
1414
return ans;
1515
}

lcs/LCS 02. 完成一半题目/Solution.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
* @return {number}
44
*/
55
var halfQuestions = function (questions) {
6-
let counter = new Array(1010).fill(0);
7-
for (const q of questions) {
8-
++counter[q];
6+
const cnt = new Array(1010).fill(0);
7+
for (const x of questions) {
8+
++cnt[x];
99
}
10-
counter.sort((a, b) => b - a);
10+
cnt.sort((a, b) => b - a);
1111
let ans = 0;
1212
let n = questions.length >> 1;
1313
for (let i = 0; n > 0; ++i) {
1414
++ans;
15-
n -= counter[i];
15+
n -= cnt[i];
1616
}
1717
return ans;
1818
};
+5-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Solution:
22
def halfQuestions(self, questions: List[int]) -> int:
3-
counter = Counter(questions)
4-
n = len(questions) >> 1
5-
ans = 0
6-
for i, v in counter.most_common():
3+
cnt = Counter(questions)
4+
ans, n = 0, len(questions) >> 1
5+
for _, v in cnt.most_common():
76
ans += 1
8-
if v >= n:
9-
return ans
107
n -= v
8+
if n <= 0:
9+
break
1110
return ans
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function halfQuestions(questions: number[]): number {
2+
const cnt = new Array(1010).fill(0);
3+
for (const x of questions) {
4+
++cnt[x];
5+
}
6+
cnt.sort((a, b) => b - a);
7+
let ans = 0;
8+
let n = questions.length >> 1;
9+
for (let i = 0; n > 0; ++i) {
10+
++ans;
11+
n -= cnt[i];
12+
}
13+
return ans;
14+
}

solution/1200-1299/1238.Circular Permutation in Binary Representation/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ int gray(x) {
6060
}
6161
```
6262

63-
我们可以直接将 $[0,..2^n - 1]$ 这些整数映射成对应的格雷码数组,然后找到 $start$ 在格雷码数组中的位置,将格雷码数组从该位置开始截取,再将截取的部分拼接到格雷码数组的前面,就得到了题目要求的排列。
63+
我们可以直接将 $[0,..2^n - 1]$ 这些整数转换成对应的格雷码数组,然后找到 $start$ 在格雷码数组中的位置,将格雷码数组从该位置开始截取,再将截取的部分拼接到格雷码数组的前面,就得到了题目要求的排列。
6464

65-
时间复杂度 $O(2^n)$,其中 $n$ 为题目给定的整数。忽略答案的空间消耗,空间复杂度 $O(1)$
65+
时间复杂度 $O(2^n)$,空间复杂度 $O(2^n)$。其中 $n$ 为题目给定的整数
6666

6767
**方法二:转换优化**
6868

6969
由于 $gray(0) = 0$,那么 $gray(0) \oplus start = start$,而 $gray(i)$ 与 $gray(i-1)$ 只有一个二进制位不同,所以 $gray(i) \oplus start$ 与 $gray(i-1) \oplus start$ 也只有一个二进制位不同。
7070

71-
因此,我们也可以直接将 $[0,..2^n - 1]$ 这些整数映射成对应的 $gray(i) \oplus start$,即可得到首项为 $start$ 的格雷码排列。
71+
因此,我们也可以直接将 $[0,..2^n - 1]$ 这些整数转换成对应的 $gray(i) \oplus start$,即可得到首项为 $start$ 的格雷码排列。
7272

7373
时间复杂度 $O(2^n)$,其中 $n$ 为题目给定的整数。忽略答案的空间消耗,空间复杂度 $O(1)$。
7474

0 commit comments

Comments
 (0)