Skip to content

Commit 4d3179c

Browse files
committed
feat: add solutions to lc problem: No.1558
No.1558.Minimum Numbers of Function Calls to Make Target Array
1 parent 0dd886b commit 4d3179c

File tree

9 files changed

+184
-6
lines changed

9 files changed

+184
-6
lines changed

.nojekyll

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

solution/0600-0699/0676.Implement Magic Dictionary/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class MagicDictionary:
2-
32
def __init__(self):
43
"""
54
Initialize your data structure here.
65
"""
76

87
def gen(self, word):
9-
return [word[:i] + '*' + word[i + 1:] for i in range(len(word))]
8+
return [word[:i] + '*' + word[i + 1 :] for i in range(len(word))]
109

1110
def buildDict(self, dictionary: List[str]) -> None:
1211
self.s = set(dictionary)

solution/1500-1599/1558.Minimum Numbers of Function Calls to Make Target Array/README.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,77 @@
7979
<!-- 这里可写当前语言的特殊实现逻辑 -->
8080

8181
```python
82-
82+
class Solution:
83+
def minOperations(self, nums: List[int]) -> int:
84+
return sum(v.bit_count() for v in nums) + max(0, max(nums).bit_length() - 1)
8385
```
8486

8587
### **Java**
8688

8789
<!-- 这里可写当前语言的特殊实现逻辑 -->
8890

8991
```java
92+
class Solution {
93+
public int minOperations(int[] nums) {
94+
int ans = 0;
95+
int mx = 0;
96+
for (int v : nums) {
97+
mx = Math.max(mx, v);
98+
ans += Integer.bitCount(v);
99+
}
100+
ans += Integer.toBinaryString(mx).length() - 1;
101+
return ans;
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int minOperations(vector<int>& nums) {
112+
int ans = 0;
113+
int mx = 0;
114+
for (int v : nums)
115+
{
116+
mx = max(mx, v);
117+
ans += __builtin_popcount(v);
118+
}
119+
if (mx) ans += 31 - __builtin_clz(mx);
120+
return ans;
121+
}
122+
};
123+
```
90124
125+
### **Go**
126+
127+
```go
128+
func minOperations(nums []int) int {
129+
ans, mx := 0, 0
130+
for _, v := range nums {
131+
mx = max(mx, v)
132+
for v > 0 {
133+
ans += v & 1
134+
v >>= 1
135+
}
136+
}
137+
if mx > 0 {
138+
for mx > 0 {
139+
ans++
140+
mx >>= 1
141+
}
142+
ans--
143+
}
144+
return ans
145+
}
146+
147+
func max(a, b int) int {
148+
if a > b {
149+
return a
150+
}
151+
return b
152+
}
91153
```
92154

93155
### **...**

solution/1500-1599/1558.Minimum Numbers of Function Calls to Make Target Array/README_EN.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,75 @@ Total of operations: 2 + 1 = 3.
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def minOperations(self, nums: List[int]) -> int:
62+
return sum(v.bit_count() for v in nums) + max(0, max(nums).bit_length() - 1)
6163
```
6264

6365
### **Java**
6466

6567
```java
68+
class Solution {
69+
public int minOperations(int[] nums) {
70+
int ans = 0;
71+
int mx = 0;
72+
for (int v : nums) {
73+
mx = Math.max(mx, v);
74+
ans += Integer.bitCount(v);
75+
}
76+
ans += Integer.toBinaryString(mx).length() - 1;
77+
return ans;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int minOperations(vector<int>& nums) {
88+
int ans = 0;
89+
int mx = 0;
90+
for (int v : nums)
91+
{
92+
mx = max(mx, v);
93+
ans += __builtin_popcount(v);
94+
}
95+
if (mx) ans += 31 - __builtin_clz(mx);
96+
return ans;
97+
}
98+
};
99+
```
66100
101+
### **Go**
102+
103+
```go
104+
func minOperations(nums []int) int {
105+
ans, mx := 0, 0
106+
for _, v := range nums {
107+
mx = max(mx, v)
108+
for v > 0 {
109+
ans += v & 1
110+
v >>= 1
111+
}
112+
}
113+
if mx > 0 {
114+
for mx > 0 {
115+
ans++
116+
mx >>= 1
117+
}
118+
ans--
119+
}
120+
return ans
121+
}
122+
123+
func max(a, b int) int {
124+
if a > b {
125+
return a
126+
}
127+
return b
128+
}
67129
```
68130

69131
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums) {
4+
int ans = 0;
5+
int mx = 0;
6+
for (int v : nums)
7+
{
8+
mx = max(mx, v);
9+
ans += __builtin_popcount(v);
10+
}
11+
if (mx) ans += 31 - __builtin_clz(mx);
12+
return ans;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func minOperations(nums []int) int {
2+
ans, mx := 0, 0
3+
for _, v := range nums {
4+
mx = max(mx, v)
5+
for v > 0 {
6+
ans += v & 1
7+
v >>= 1
8+
}
9+
}
10+
if mx > 0 {
11+
for mx > 0 {
12+
ans++
13+
mx >>= 1
14+
}
15+
ans--
16+
}
17+
return ans
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int minOperations(int[] nums) {
3+
int ans = 0;
4+
int mx = 0;
5+
for (int v : nums) {
6+
mx = Math.max(mx, v);
7+
ans += Integer.bitCount(v);
8+
}
9+
ans += Integer.toBinaryString(mx).length() - 1;
10+
return ans;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def minOperations(self, nums: List[int]) -> int:
3+
return sum(v.bit_count() for v in nums) + max(0, max(nums).bit_length() - 1)

solution/2300-2399/2332.The Latest Time to Catch a Bus/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def latestTimeCatchTheBus(self, buses: List[int], passengers: List[int], capacity: int) -> int:
2+
def latestTimeCatchTheBus(
3+
self, buses: List[int], passengers: List[int], capacity: int
4+
) -> int:
35
buses.sort()
46
passengers.sort()
57
j = 0

0 commit comments

Comments
 (0)