Skip to content

Commit fc9fb4d

Browse files
authored
feat: add solutions to lc problem: No.2708 (doocs#2740)
1 parent ccf3b44 commit fc9fb4d

File tree

18 files changed

+414
-136
lines changed

18 files changed

+414
-136
lines changed

solution/2700-2799/2708.Maximum Strength of a Group/README.md

+106-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,112 @@
3939

4040
## 解法
4141

42-
### 方法一
42+
### 方法一:二进制枚举
43+
44+
题目实际上是求所有子集的乘积的最大值,由于数组长度不超过 $13$,我们可以考虑使用二进制枚举的方法。
45+
46+
我们在 $[1, 2^n)$ 的范围内枚举所有的子集,对于每个子集,我们计算其乘积,最后返回最大值即可。
47+
48+
时间复杂度 $O(2^n \times n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
49+
50+
<!-- tabs:start -->
51+
52+
```python
53+
class Solution:
54+
def maxStrength(self, nums: List[int]) -> int:
55+
ans = -inf
56+
for i in range(1, 1 << len(nums)):
57+
t = 1
58+
for j, x in enumerate(nums):
59+
if i >> j & 1:
60+
t *= x
61+
ans = max(ans, t)
62+
return ans
63+
```
64+
65+
```java
66+
class Solution {
67+
public long maxStrength(int[] nums) {
68+
long ans = (long) -1e14;
69+
int n = nums.length;
70+
for (int i = 1; i < 1 << n; ++i) {
71+
long t = 1;
72+
for (int j = 0; j < n; ++j) {
73+
if ((i >> j & 1) == 1) {
74+
t *= nums[j];
75+
}
76+
}
77+
ans = Math.max(ans, t);
78+
}
79+
return ans;
80+
}
81+
}
82+
```
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
long long maxStrength(vector<int>& nums) {
88+
long long ans = -1e14;
89+
int n = nums.size();
90+
for (int i = 1; i < 1 << n; ++i) {
91+
long long t = 1;
92+
for (int j = 0; j < n; ++j) {
93+
if (i >> j & 1) {
94+
t *= nums[j];
95+
}
96+
}
97+
ans = max(ans, t);
98+
}
99+
return ans;
100+
}
101+
};
102+
```
103+
104+
```go
105+
func maxStrength(nums []int) int64 {
106+
ans := int64(-1e14)
107+
for i := 1; i < 1<<len(nums); i++ {
108+
var t int64 = 1
109+
for j, x := range nums {
110+
if i>>j&1 == 1 {
111+
t *= int64(x)
112+
}
113+
}
114+
ans = max(ans, t)
115+
}
116+
return ans
117+
}
118+
```
119+
120+
```ts
121+
function maxStrength(nums: number[]): number {
122+
let ans = -Infinity;
123+
const n = nums.length;
124+
for (let i = 1; i < 1 << n; ++i) {
125+
let t = 1;
126+
for (let j = 0; j < n; ++j) {
127+
if ((i >> j) & 1) {
128+
t *= nums[j];
129+
}
130+
}
131+
ans = Math.max(ans, t);
132+
}
133+
return ans;
134+
}
135+
```
136+
137+
<!-- tabs:end -->
138+
139+
### 方法二:排序 + 贪心
140+
141+
我们可以先对数组进行排序,然后根据数组的特点,我们可以得到以下结论:
142+
143+
- 如果数组中只有一个元素,那么最大实力值就是这个元素;
144+
- 如果数组中有两个及以上的元素,且数组中 $nums[1] = nums[n - 1] = 0$,那么最大实力值就是 $0$;
145+
- 否则,我们从小到大遍历数组,如果当前元素小于 $0$,且下一个元素也小于 $0$,那么我们将这两个元素相乘,累乘到答案中;否则,如果当前元素小于等于 $0$,我们直接跳过;如果当前元素大于 $0$,我们将这个元素累乘到答案中,最后返回答案。
146+
147+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组的长度。
43148

44149
<!-- tabs:start -->
45150

solution/2700-2799/2708.Maximum Strength of a Group/README_EN.md

+106-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,112 @@
3737

3838
## Solutions
3939

40-
### Solution 1
40+
### Solution 1: Binary Enumeration
41+
42+
The problem is actually to find the maximum product of all subsets. Since the length of the array does not exceed $13$, we can consider using the method of binary enumeration.
43+
44+
We enumerate all subsets in the range of $[1, 2^n)$, and for each subset, we calculate its product, and finally return the maximum value.
45+
46+
The time complexity is $O(2^n \times n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
47+
48+
<!-- tabs:start -->
49+
50+
```python
51+
class Solution:
52+
def maxStrength(self, nums: List[int]) -> int:
53+
ans = -inf
54+
for i in range(1, 1 << len(nums)):
55+
t = 1
56+
for j, x in enumerate(nums):
57+
if i >> j & 1:
58+
t *= x
59+
ans = max(ans, t)
60+
return ans
61+
```
62+
63+
```java
64+
class Solution {
65+
public long maxStrength(int[] nums) {
66+
long ans = (long) -1e14;
67+
int n = nums.length;
68+
for (int i = 1; i < 1 << n; ++i) {
69+
long t = 1;
70+
for (int j = 0; j < n; ++j) {
71+
if ((i >> j & 1) == 1) {
72+
t *= nums[j];
73+
}
74+
}
75+
ans = Math.max(ans, t);
76+
}
77+
return ans;
78+
}
79+
}
80+
```
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
long long maxStrength(vector<int>& nums) {
86+
long long ans = -1e14;
87+
int n = nums.size();
88+
for (int i = 1; i < 1 << n; ++i) {
89+
long long t = 1;
90+
for (int j = 0; j < n; ++j) {
91+
if (i >> j & 1) {
92+
t *= nums[j];
93+
}
94+
}
95+
ans = max(ans, t);
96+
}
97+
return ans;
98+
}
99+
};
100+
```
101+
102+
```go
103+
func maxStrength(nums []int) int64 {
104+
ans := int64(-1e14)
105+
for i := 1; i < 1<<len(nums); i++ {
106+
var t int64 = 1
107+
for j, x := range nums {
108+
if i>>j&1 == 1 {
109+
t *= int64(x)
110+
}
111+
}
112+
ans = max(ans, t)
113+
}
114+
return ans
115+
}
116+
```
117+
118+
```ts
119+
function maxStrength(nums: number[]): number {
120+
let ans = -Infinity;
121+
const n = nums.length;
122+
for (let i = 1; i < 1 << n; ++i) {
123+
let t = 1;
124+
for (let j = 0; j < n; ++j) {
125+
if ((i >> j) & 1) {
126+
t *= nums[j];
127+
}
128+
}
129+
ans = Math.max(ans, t);
130+
}
131+
return ans;
132+
}
133+
```
134+
135+
<!-- tabs:end -->
136+
137+
### Solution 2: Sorting + Greedy
138+
139+
First, we can sort the array. Based on the characteristics of the array, we can draw the following conclusions:
140+
141+
- If there is only one element in the array, then the maximum strength value is this element.
142+
- If there are two or more elements in the array, and $nums[1] = nums[n - 1] = 0$, then the maximum strength value is $0$.
143+
- Otherwise, we traverse the array from small to large. If the current element is less than $0$ and the next element is also less than $0$, then we multiply these two elements and accumulate the product into the answer. Otherwise, if the current element is less than or equal to $0$, we skip it directly. If the current element is greater than $0$, we multiply this element into the answer. Finally, we return the answer.
144+
145+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the length of the array.
41146

42147
<!-- tabs:start -->
43148

Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
class Solution {
2-
public:
3-
long long maxStrength(vector<int>& nums) {
4-
sort(nums.begin(), nums.end());
5-
int n = nums.size();
6-
if (n == 1) {
7-
return nums[0];
8-
}
9-
if (nums[1] == 0 && nums[n - 1] == 0) {
10-
return 0;
11-
}
12-
long long ans = 1;
13-
int i = 0;
14-
while (i < n) {
15-
if (nums[i] < 0 && i + 1 < n && nums[i + 1] < 0) {
16-
ans *= nums[i] * nums[i + 1];
17-
i += 2;
18-
} else if (nums[i] <= 0) {
19-
i += 1;
20-
} else {
21-
ans *= nums[i];
22-
i += 1;
23-
}
24-
}
25-
return ans;
26-
}
1+
class Solution {
2+
public:
3+
long long maxStrength(vector<int>& nums) {
4+
long long ans = -1e14;
5+
int n = nums.size();
6+
for (int i = 1; i < 1 << n; ++i) {
7+
long long t = 1;
8+
for (int j = 0; j < n; ++j) {
9+
if (i >> j & 1) {
10+
t *= nums[j];
11+
}
12+
}
13+
ans = max(ans, t);
14+
}
15+
return ans;
16+
}
2717
};
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
func maxStrength(nums []int) int64 {
2-
sort.Ints(nums)
3-
n := len(nums)
4-
if n == 1 {
5-
return int64(nums[0])
6-
}
7-
if nums[1] == 0 && nums[n-1] == 0 {
8-
return 0
9-
}
10-
ans := int64(1)
11-
for i := 0; i < n; i++ {
12-
if nums[i] < 0 && i+1 < n && nums[i+1] < 0 {
13-
ans *= int64(nums[i] * nums[i+1])
14-
i++
15-
} else if nums[i] > 0 {
16-
ans *= int64(nums[i])
17-
}
18-
}
19-
return ans
1+
func maxStrength(nums []int) int64 {
2+
ans := int64(-1e14)
3+
for i := 1; i < 1<<len(nums); i++ {
4+
var t int64 = 1
5+
for j, x := range nums {
6+
if i>>j&1 == 1 {
7+
t *= int64(x)
8+
}
9+
}
10+
ans = max(ans, t)
11+
}
12+
return ans
2013
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
1-
class Solution {
2-
public long maxStrength(int[] nums) {
3-
Arrays.sort(nums);
4-
int n = nums.length;
5-
if (n == 1) {
6-
return nums[0];
7-
}
8-
if (nums[1] == 0 && nums[n - 1] == 0) {
9-
return 0;
10-
}
11-
long ans = 1;
12-
int i = 0;
13-
while (i < n) {
14-
if (nums[i] < 0 && i + 1 < n && nums[i + 1] < 0) {
15-
ans *= nums[i] * nums[i + 1];
16-
i += 2;
17-
} else if (nums[i] <= 0) {
18-
i += 1;
19-
} else {
20-
ans *= nums[i];
21-
i += 1;
22-
}
23-
}
24-
return ans;
25-
}
1+
class Solution {
2+
public long maxStrength(int[] nums) {
3+
long ans = (long) -1e14;
4+
int n = nums.length;
5+
for (int i = 1; i < 1 << n; ++i) {
6+
long t = 1;
7+
for (int j = 0; j < n; ++j) {
8+
if ((i >> j & 1) == 1) {
9+
t *= nums[j];
10+
}
11+
}
12+
ans = Math.max(ans, t);
13+
}
14+
return ans;
15+
}
2616
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
class Solution:
2-
def maxStrength(self, nums: List[int]) -> int:
3-
nums.sort()
4-
n = len(nums)
5-
if n == 1:
6-
return nums[0]
7-
if nums[1] == nums[-1] == 0:
8-
return 0
9-
ans, i = 1, 0
10-
while i < n:
11-
if nums[i] < 0 and i + 1 < n and nums[i + 1] < 0:
12-
ans *= nums[i] * nums[i + 1]
13-
i += 2
14-
elif nums[i] <= 0:
15-
i += 1
16-
else:
17-
ans *= nums[i]
18-
i += 1
19-
return ans
1+
class Solution:
2+
def maxStrength(self, nums: List[int]) -> int:
3+
ans = -inf
4+
for i in range(1, 1 << len(nums)):
5+
t = 1
6+
for j, x in enumerate(nums):
7+
if i >> j & 1:
8+
t *= x
9+
ans = max(ans, t)
10+
return ans

0 commit comments

Comments
 (0)