Skip to content

Commit 69d670b

Browse files
authored
feat: add solutions to lc problems: No.3309,3310 (#3604)
* No.3309.Maximum Possible Number by Binary Concatenation * No.3310.Remove Methods From Project
1 parent 42dea92 commit 69d670b

File tree

14 files changed

+963
-16
lines changed

14 files changed

+963
-16
lines changed

solution/3300-3399/3309.Maximum Possible Number by Binary Concatenation/README.md

+96-4
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,124 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3309.Ma
6161

6262
<!-- solution:start -->
6363

64-
### 方法一
64+
### 方法一:枚举
65+
66+
根据题目描述,数组 $\textit{nums}$ 的长度为 $3$,我们可以枚举 $\textit{nums}$ 的全排列,一共有 $3! = 6$ 种排列方式,然后将排列后的数组中的元素转换为二进制字符串,再将这些二进制字符串连接起来,最后将连接后的二进制字符串转换为十进制数,取最大值即可。
67+
68+
时间复杂度 $O(\log M)$,其中 $M$ 表示 $\textit{nums}$ 中的元素的最大值。空间复杂度 $O(1)$。
6569

6670
<!-- tabs:start -->
6771

6872
#### Python3
6973

7074
```python
71-
75+
class Solution:
76+
def maxGoodNumber(self, nums: List[int]) -> int:
77+
ans = 0
78+
for arr in permutations(nums):
79+
num = int("".join(bin(i)[2:] for i in arr), 2)
80+
ans = max(ans, num)
81+
return ans
7282
```
7383

7484
#### Java
7585

7686
```java
77-
87+
class Solution {
88+
private int[] nums;
89+
90+
public int maxGoodNumber(int[] nums) {
91+
this.nums = nums;
92+
int ans = f(0, 1, 2);
93+
ans = Math.max(ans, f(0, 2, 1));
94+
ans = Math.max(ans, f(1, 0, 2));
95+
ans = Math.max(ans, f(1, 2, 0));
96+
ans = Math.max(ans, f(2, 0, 1));
97+
ans = Math.max(ans, f(2, 1, 0));
98+
return ans;
99+
}
100+
101+
private int f(int i, int j, int k) {
102+
String a = Integer.toBinaryString(nums[i]);
103+
String b = Integer.toBinaryString(nums[j]);
104+
String c = Integer.toBinaryString(nums[k]);
105+
return Integer.parseInt(a + b + c, 2);
106+
}
107+
}
78108
```
79109

80110
#### C++
81111

82112
```cpp
83-
113+
class Solution {
114+
public:
115+
int maxGoodNumber(vector<int>& nums) {
116+
int ans = 0;
117+
auto f = [&](vector<int>& nums) {
118+
int res = 0;
119+
vector<int> t;
120+
for (int x : nums) {
121+
for (; x; x >>= 1) {
122+
t.push_back(x & 1);
123+
}
124+
}
125+
while (t.size()) {
126+
res = res * 2 + t.back();
127+
t.pop_back();
128+
}
129+
return res;
130+
};
131+
for (int i = 0; i < 6; ++i) {
132+
ans = max(ans, f(nums));
133+
next_permutation(nums.begin(), nums.end());
134+
}
135+
return ans;
136+
}
137+
};
84138
```
85139
86140
#### Go
87141
88142
```go
143+
func maxGoodNumber(nums []int) int {
144+
f := func(i, j, k int) int {
145+
a := strconv.FormatInt(int64(nums[i]), 2)
146+
b := strconv.FormatInt(int64(nums[j]), 2)
147+
c := strconv.FormatInt(int64(nums[k]), 2)
148+
res, _ := strconv.ParseInt(a+b+c, 2, 64)
149+
return int(res)
150+
}
151+
ans := f(0, 1, 2)
152+
ans = max(ans, f(0, 2, 1))
153+
ans = max(ans, f(1, 0, 2))
154+
ans = max(ans, f(1, 2, 0))
155+
ans = max(ans, f(2, 0, 1))
156+
ans = max(ans, f(2, 1, 0))
157+
return ans
158+
}
159+
```
89160

161+
#### TypeScript
162+
163+
```ts
164+
function maxGoodNumber(nums: number[]): number {
165+
const f = (i: number, j: number, k: number): number => {
166+
const a = nums[i].toString(2);
167+
const b = nums[j].toString(2);
168+
const c = nums[k].toString(2);
169+
const res = parseInt(a + b + c, 2);
170+
return res;
171+
};
172+
173+
let ans = f(0, 1, 2);
174+
ans = Math.max(ans, f(0, 2, 1));
175+
ans = Math.max(ans, f(1, 0, 2));
176+
ans = Math.max(ans, f(1, 2, 0));
177+
ans = Math.max(ans, f(2, 0, 1));
178+
ans = Math.max(ans, f(2, 1, 0));
179+
180+
return ans;
181+
}
90182
```
91183

92184
<!-- tabs:end -->

solution/3300-3399/3309.Maximum Possible Number by Binary Concatenation/README_EN.md

+96-4
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,124 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3309.Ma
5959

6060
<!-- solution:start -->
6161

62-
### Solution 1
62+
### Solution 1: Enumeration
63+
64+
According to the problem description, the length of the array $\textit{nums}$ is $3$. We can enumerate all permutations of $\textit{nums}$, which has $3! = 6$ permutations. Then, we convert the elements of the permuted array into binary strings, concatenate these binary strings, and finally convert the concatenated binary string into a decimal number to get the maximum value.
65+
66+
The time complexity is $O(\log M)$, where $M$ represents the maximum value of the elements in $\textit{nums}$. The space complexity is $O(1)$.
6367

6468
<!-- tabs:start -->
6569

6670
#### Python3
6771

6872
```python
69-
73+
class Solution:
74+
def maxGoodNumber(self, nums: List[int]) -> int:
75+
ans = 0
76+
for arr in permutations(nums):
77+
num = int("".join(bin(i)[2:] for i in arr), 2)
78+
ans = max(ans, num)
79+
return ans
7080
```
7181

7282
#### Java
7383

7484
```java
75-
85+
class Solution {
86+
private int[] nums;
87+
88+
public int maxGoodNumber(int[] nums) {
89+
this.nums = nums;
90+
int ans = f(0, 1, 2);
91+
ans = Math.max(ans, f(0, 2, 1));
92+
ans = Math.max(ans, f(1, 0, 2));
93+
ans = Math.max(ans, f(1, 2, 0));
94+
ans = Math.max(ans, f(2, 0, 1));
95+
ans = Math.max(ans, f(2, 1, 0));
96+
return ans;
97+
}
98+
99+
private int f(int i, int j, int k) {
100+
String a = Integer.toBinaryString(nums[i]);
101+
String b = Integer.toBinaryString(nums[j]);
102+
String c = Integer.toBinaryString(nums[k]);
103+
return Integer.parseInt(a + b + c, 2);
104+
}
105+
}
76106
```
77107

78108
#### C++
79109

80110
```cpp
81-
111+
class Solution {
112+
public:
113+
int maxGoodNumber(vector<int>& nums) {
114+
int ans = 0;
115+
auto f = [&](vector<int>& nums) {
116+
int res = 0;
117+
vector<int> t;
118+
for (int x : nums) {
119+
for (; x; x >>= 1) {
120+
t.push_back(x & 1);
121+
}
122+
}
123+
while (t.size()) {
124+
res = res * 2 + t.back();
125+
t.pop_back();
126+
}
127+
return res;
128+
};
129+
for (int i = 0; i < 6; ++i) {
130+
ans = max(ans, f(nums));
131+
next_permutation(nums.begin(), nums.end());
132+
}
133+
return ans;
134+
}
135+
};
82136
```
83137
84138
#### Go
85139
86140
```go
141+
func maxGoodNumber(nums []int) int {
142+
f := func(i, j, k int) int {
143+
a := strconv.FormatInt(int64(nums[i]), 2)
144+
b := strconv.FormatInt(int64(nums[j]), 2)
145+
c := strconv.FormatInt(int64(nums[k]), 2)
146+
res, _ := strconv.ParseInt(a+b+c, 2, 64)
147+
return int(res)
148+
}
149+
ans := f(0, 1, 2)
150+
ans = max(ans, f(0, 2, 1))
151+
ans = max(ans, f(1, 0, 2))
152+
ans = max(ans, f(1, 2, 0))
153+
ans = max(ans, f(2, 0, 1))
154+
ans = max(ans, f(2, 1, 0))
155+
return ans
156+
}
157+
```
87158

159+
#### TypeScript
160+
161+
```ts
162+
function maxGoodNumber(nums: number[]): number {
163+
const f = (i: number, j: number, k: number): number => {
164+
const a = nums[i].toString(2);
165+
const b = nums[j].toString(2);
166+
const c = nums[k].toString(2);
167+
const res = parseInt(a + b + c, 2);
168+
return res;
169+
};
170+
171+
let ans = f(0, 1, 2);
172+
ans = Math.max(ans, f(0, 2, 1));
173+
ans = Math.max(ans, f(1, 0, 2));
174+
ans = Math.max(ans, f(1, 2, 0));
175+
ans = Math.max(ans, f(2, 0, 1));
176+
ans = Math.max(ans, f(2, 1, 0));
177+
178+
return ans;
179+
}
88180
```
89181

90182
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int maxGoodNumber(vector<int>& nums) {
4+
int ans = 0;
5+
auto f = [&](vector<int>& nums) {
6+
int res = 0;
7+
vector<int> t;
8+
for (int x : nums) {
9+
for (; x; x >>= 1) {
10+
t.push_back(x & 1);
11+
}
12+
}
13+
while (t.size()) {
14+
res = res * 2 + t.back();
15+
t.pop_back();
16+
}
17+
return res;
18+
};
19+
for (int i = 0; i < 6; ++i) {
20+
ans = max(ans, f(nums));
21+
next_permutation(nums.begin(), nums.end());
22+
}
23+
return ans;
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func maxGoodNumber(nums []int) int {
2+
f := func(i, j, k int) int {
3+
a := strconv.FormatInt(int64(nums[i]), 2)
4+
b := strconv.FormatInt(int64(nums[j]), 2)
5+
c := strconv.FormatInt(int64(nums[k]), 2)
6+
res, _ := strconv.ParseInt(a+b+c, 2, 64)
7+
return int(res)
8+
}
9+
ans := f(0, 1, 2)
10+
ans = max(ans, f(0, 2, 1))
11+
ans = max(ans, f(1, 0, 2))
12+
ans = max(ans, f(1, 2, 0))
13+
ans = max(ans, f(2, 0, 1))
14+
ans = max(ans, f(2, 1, 0))
15+
return ans
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
private int[] nums;
3+
4+
public int maxGoodNumber(int[] nums) {
5+
this.nums = nums;
6+
int ans = f(0, 1, 2);
7+
ans = Math.max(ans, f(0, 2, 1));
8+
ans = Math.max(ans, f(1, 0, 2));
9+
ans = Math.max(ans, f(1, 2, 0));
10+
ans = Math.max(ans, f(2, 0, 1));
11+
ans = Math.max(ans, f(2, 1, 0));
12+
return ans;
13+
}
14+
15+
private int f(int i, int j, int k) {
16+
String a = Integer.toBinaryString(nums[i]);
17+
String b = Integer.toBinaryString(nums[j]);
18+
String c = Integer.toBinaryString(nums[k]);
19+
return Integer.parseInt(a + b + c, 2);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def maxGoodNumber(self, nums: List[int]) -> int:
3+
ans = 0
4+
for arr in permutations(nums):
5+
num = int("".join(bin(i)[2:] for i in arr), 2)
6+
ans = max(ans, num)
7+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function maxGoodNumber(nums: number[]): number {
2+
const f = (i: number, j: number, k: number): number => {
3+
const a = nums[i].toString(2);
4+
const b = nums[j].toString(2);
5+
const c = nums[k].toString(2);
6+
const res = parseInt(a + b + c, 2);
7+
return res;
8+
};
9+
10+
let ans = f(0, 1, 2);
11+
ans = Math.max(ans, f(0, 2, 1));
12+
ans = Math.max(ans, f(1, 0, 2));
13+
ans = Math.max(ans, f(1, 2, 0));
14+
ans = Math.max(ans, f(2, 0, 1));
15+
ans = Math.max(ans, f(2, 1, 0));
16+
17+
return ans;
18+
}

0 commit comments

Comments
 (0)