Skip to content

Commit 594e0b0

Browse files
committed
feat: add solutions to lcs problems: No.01~03
* LCS 01.下载插件 * LCS 02.完成一半题目 * LCS 03.主题空间
1 parent d802b8b commit 594e0b0

File tree

13 files changed

+276
-124
lines changed

13 files changed

+276
-124
lines changed

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

+30-21
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
```python
5757
class Solution:
5858
def leastMinutes(self, n: int) -> int:
59-
speed = res = 1
59+
speed = ans = 1
6060
while speed < n:
6161
speed <<= 1
62-
res += 1
63-
return res
62+
ans += 1
63+
return ans
6464
```
6565

6666
### **Java**
@@ -70,13 +70,11 @@ class Solution:
7070
```java
7171
class Solution {
7272
public int leastMinutes(int n) {
73-
int speed = 1;
74-
int res = 1;
75-
while (speed < n) {
76-
speed <<= 1;
77-
++res;
73+
int ans = 1;
74+
for (int speed = 1; speed < n; speed <<= 1) {
75+
++ans;
7876
}
79-
return res;
77+
return ans;
8078
}
8179
}
8280
```
@@ -87,13 +85,9 @@ class Solution {
8785
class Solution {
8886
public:
8987
int leastMinutes(int n) {
90-
int speed = 1, res = 1;
91-
while (speed < n)
92-
{
93-
speed <<= 1;
94-
++res;
95-
}
96-
return res;
88+
int ans = 1;
89+
for (int speed = 1; speed < n; speed <<= 1) ++ans;
90+
return ans;
9791
}
9892
};
9993
```
@@ -102,15 +96,30 @@ public:
10296
10397
```go
10498
func leastMinutes(n int) int {
105-
speed, res := 1, 1
106-
for speed < n {
107-
speed <<= 1
108-
res++
99+
ans := 1
100+
for speed := 1; speed < n; speed <<= 1 {
101+
ans++
109102
}
110-
return res
103+
return ans
111104
}
112105
```
113106

107+
### **JavaScript**
108+
109+
```js
110+
/**
111+
* @param {number} n
112+
* @return {number}
113+
*/
114+
var leastMinutes = function (n) {
115+
let ans = 1;
116+
for (let speed = 1; speed < n; speed <<= 1) {
117+
++ans;
118+
}
119+
return ans;
120+
};
121+
```
122+
114123
### **...**
115124

116125
```

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
class Solution {
22
public:
33
int leastMinutes(int n) {
4-
int speed = 1, res = 1;
5-
while (speed < n)
6-
{
7-
speed <<= 1;
8-
++res;
9-
}
10-
return res;
4+
int ans = 1;
5+
for (int speed = 1; speed < n; speed <<= 1) ++ans;
6+
return ans;
117
}
128
};

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
func leastMinutes(n int) int {
2-
speed, res := 1, 1
3-
for speed < n {
4-
speed <<= 1
5-
res++
2+
ans := 1
3+
for speed := 1; speed < n; speed <<= 1 {
4+
ans++
65
}
7-
return res
6+
return ans
87
}
+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class Solution {
22
public int leastMinutes(int n) {
3-
int speed = 1;
4-
int res = 1;
5-
while (speed < n) {
6-
speed <<= 1;
7-
++res;
3+
int ans = 1;
4+
for (int speed = 1; speed < n; speed <<= 1) {
5+
++ans;
86
}
9-
return res;
7+
return ans;
108
}
119
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var leastMinutes = function (n) {
6+
let ans = 1;
7+
for (let speed = 1; speed < n; speed <<= 1) {
8+
++ans;
9+
}
10+
return ans;
11+
};

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

+46-31
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@
5050
class Solution:
5151
def halfQuestions(self, questions: List[int]) -> int:
5252
counter = Counter(questions)
53-
counter = OrderedDict(counter.most_common())
5453
n = len(questions) >> 1
55-
res = 0
56-
for v in counter.values():
57-
res += 1
54+
ans = 0
55+
for i, v in counter.most_common():
56+
ans += 1
5857
if v >= n:
59-
return res
58+
return ans
6059
n -= v
61-
return res
60+
return ans
6261
```
6362

6463
### **Java**
@@ -69,20 +68,17 @@ class Solution:
6968
class Solution {
7069
public int halfQuestions(int[] questions) {
7170
int[] counter = new int[1010];
72-
for (int e : questions) {
73-
++counter[e];
71+
for (int q : questions) {
72+
++counter[q];
7473
}
75-
int n = questions.length >> 1;
7674
Arrays.sort(counter);
77-
int res = 0;
78-
for (int i = counter.length - 1; i >= 0; --i) {
79-
++res;
80-
if (counter[i] >= n) {
81-
return res;
82-
}
75+
int ans = 0;
76+
int n = questions.length >> 1;
77+
for (int i = counter.length - 1; n > 0; --i) {
78+
++ans;
8379
n -= counter[i];
8480
}
85-
return res;
81+
return ans;
8682
}
8783
}
8884
```
@@ -94,17 +90,16 @@ class Solution {
9490
public:
9591
int halfQuestions(vector<int>& questions) {
9692
vector<int> counter(1010);
97-
for (int e : questions) ++counter[e];
93+
for (int q : questions) ++counter[q];
9894
int n = questions.size() >> 1;
9995
sort(counter.begin(), counter.end());
100-
int res = 0;
101-
for (int i = counter.size() - 1; i >= 0; --i)
96+
int ans = 0;
97+
for (int i = counter.size() - 1; n > 0; --i)
10298
{
103-
++res;
104-
if (counter[i] >= n) return res;
99+
++ans;
105100
n -= counter[i];
106101
}
107-
return res;
102+
return ans;
108103
}
109104
};
110105
```
@@ -114,23 +109,43 @@ public:
114109
```go
115110
func halfQuestions(questions []int) int {
116111
counter := make([]int, 1010)
117-
for _, e := range questions {
118-
counter[e]++
112+
for _, q := range questions {
113+
counter[q]++
119114
}
120115
n := len(questions) >> 1
121116
sort.Ints(counter)
122-
res := 0
123-
for i := len(counter) - 1; i >= 0; i-- {
124-
res++
125-
if counter[i] >= n {
126-
return res
127-
}
117+
ans := 0
118+
for i := len(counter) - 1; n > 0; i-- {
119+
ans++
128120
n -= counter[i]
129121
}
130-
return res
122+
return ans
131123
}
132124
```
133125

126+
### **JavaScript**
127+
128+
```js
129+
/**
130+
* @param {number[]} questions
131+
* @return {number}
132+
*/
133+
var halfQuestions = function(questions) {
134+
let counter = new Array(1010).fill(0);
135+
for (const q of questions) {
136+
++counter[q];
137+
}
138+
counter.sort((a, b) => b - a);
139+
let ans = 0;
140+
let n = questions.length >> 1;
141+
for (let i = 0; n > 0; ++i) {
142+
++ans;
143+
n -= counter[i];
144+
}
145+
return ans;
146+
};
147+
```
148+
134149
### **...**
135150

136151
```

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ class Solution {
22
public:
33
int halfQuestions(vector<int>& questions) {
44
vector<int> counter(1010);
5-
for (int e : questions) ++counter[e];
5+
for (int q : questions) ++counter[q];
66
int n = questions.size() >> 1;
77
sort(counter.begin(), counter.end());
8-
int res = 0;
9-
for (int i = counter.size() - 1; i >= 0; --i)
8+
int ans = 0;
9+
for (int i = counter.size() - 1; n > 0; --i)
1010
{
11-
++res;
12-
if (counter[i] >= n) return res;
11+
++ans;
1312
n -= counter[i];
1413
}
15-
return res;
14+
return ans;
1615
}
1716
};
+6-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
func halfQuestions(questions []int) int {
22
counter := make([]int, 1010)
3-
for _, e := range questions {
4-
counter[e]++
3+
for _, q := range questions {
4+
counter[q]++
55
}
66
n := len(questions) >> 1
77
sort.Ints(counter)
8-
res := 0
9-
for i := len(counter) - 1; i >= 0; i-- {
10-
res++
11-
if counter[i] >= n {
12-
return res
13-
}
8+
ans := 0
9+
for i := len(counter) - 1; n > 0; i-- {
10+
ans++
1411
n -= counter[i]
1512
}
16-
return res
13+
return ans
1714
}
+7-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
class Solution {
22
public int halfQuestions(int[] questions) {
33
int[] counter = new int[1010];
4-
for (int e : questions) {
5-
++counter[e];
4+
for (int q : questions) {
5+
++counter[q];
66
}
7-
int n = questions.length >> 1;
87
Arrays.sort(counter);
9-
int res = 0;
10-
for (int i = counter.length - 1; i >= 0; --i) {
11-
++res;
12-
if (counter[i] >= n) {
13-
return res;
14-
}
8+
int ans = 0;
9+
int n = questions.length >> 1;
10+
for (int i = counter.length - 1; n > 0; --i) {
11+
++ans;
1512
n -= counter[i];
1613
}
17-
return res;
14+
return ans;
1815
}
1916
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} questions
3+
* @return {number}
4+
*/
5+
var halfQuestions = function(questions) {
6+
let counter = new Array(1010).fill(0);
7+
for (const q of questions) {
8+
++counter[q];
9+
}
10+
counter.sort((a, b) => b - a);
11+
let ans = 0;
12+
let n = questions.length >> 1;
13+
for (let i = 0; n > 0; ++i) {
14+
++ans;
15+
n -= counter[i];
16+
}
17+
return ans;
18+
};
+5-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class Solution:
22
def halfQuestions(self, questions: List[int]) -> int:
33
counter = Counter(questions)
4-
counter = OrderedDict(counter.most_common())
54
n = len(questions) >> 1
6-
res = 0
7-
for v in counter.values():
8-
res += 1
5+
ans = 0
6+
for i, v in counter.most_common():
7+
ans += 1
98
if v >= n:
10-
return res
9+
return ans
1110
n -= v
12-
return res
11+
return ans

0 commit comments

Comments
 (0)