Skip to content

feat: add solutions to lc problem: No.2708 #2740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 106 additions & 1 deletion solution/2700-2799/2708.Maximum Strength of a Group/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,112 @@

## 解法

### 方法一
### 方法一:二进制枚举

题目实际上是求所有子集的乘积的最大值,由于数组长度不超过 $13$,我们可以考虑使用二进制枚举的方法。

我们在 $[1, 2^n)$ 的范围内枚举所有的子集,对于每个子集,我们计算其乘积,最后返回最大值即可。

时间复杂度 $O(2^n \times n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def maxStrength(self, nums: List[int]) -> int:
ans = -inf
for i in range(1, 1 << len(nums)):
t = 1
for j, x in enumerate(nums):
if i >> j & 1:
t *= x
ans = max(ans, t)
return ans
```

```java
class Solution {
public long maxStrength(int[] nums) {
long ans = (long) -1e14;
int n = nums.length;
for (int i = 1; i < 1 << n; ++i) {
long t = 1;
for (int j = 0; j < n; ++j) {
if ((i >> j & 1) == 1) {
t *= nums[j];
}
}
ans = Math.max(ans, t);
}
return ans;
}
}
```

```cpp
class Solution {
public:
long long maxStrength(vector<int>& nums) {
long long ans = -1e14;
int n = nums.size();
for (int i = 1; i < 1 << n; ++i) {
long long t = 1;
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
t *= nums[j];
}
}
ans = max(ans, t);
}
return ans;
}
};
```

```go
func maxStrength(nums []int) int64 {
ans := int64(-1e14)
for i := 1; i < 1<<len(nums); i++ {
var t int64 = 1
for j, x := range nums {
if i>>j&1 == 1 {
t *= int64(x)
}
}
ans = max(ans, t)
}
return ans
}
```

```ts
function maxStrength(nums: number[]): number {
let ans = -Infinity;
const n = nums.length;
for (let i = 1; i < 1 << n; ++i) {
let t = 1;
for (let j = 0; j < n; ++j) {
if ((i >> j) & 1) {
t *= nums[j];
}
}
ans = Math.max(ans, t);
}
return ans;
}
```

<!-- tabs:end -->

### 方法二:排序 + 贪心

我们可以先对数组进行排序,然后根据数组的特点,我们可以得到以下结论:

- 如果数组中只有一个元素,那么最大实力值就是这个元素;
- 如果数组中有两个及以上的元素,且数组中 $nums[1] = nums[n - 1] = 0$,那么最大实力值就是 $0$;
- 否则,我们从小到大遍历数组,如果当前元素小于 $0$,且下一个元素也小于 $0$,那么我们将这两个元素相乘,累乘到答案中;否则,如果当前元素小于等于 $0$,我们直接跳过;如果当前元素大于 $0$,我们将这个元素累乘到答案中,最后返回答案。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组的长度。

<!-- tabs:start -->

Expand Down
107 changes: 106 additions & 1 deletion solution/2700-2799/2708.Maximum Strength of a Group/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,112 @@

## Solutions

### Solution 1
### Solution 1: Binary Enumeration

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.

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.

The time complexity is $O(2^n \times n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

```python
class Solution:
def maxStrength(self, nums: List[int]) -> int:
ans = -inf
for i in range(1, 1 << len(nums)):
t = 1
for j, x in enumerate(nums):
if i >> j & 1:
t *= x
ans = max(ans, t)
return ans
```

```java
class Solution {
public long maxStrength(int[] nums) {
long ans = (long) -1e14;
int n = nums.length;
for (int i = 1; i < 1 << n; ++i) {
long t = 1;
for (int j = 0; j < n; ++j) {
if ((i >> j & 1) == 1) {
t *= nums[j];
}
}
ans = Math.max(ans, t);
}
return ans;
}
}
```

```cpp
class Solution {
public:
long long maxStrength(vector<int>& nums) {
long long ans = -1e14;
int n = nums.size();
for (int i = 1; i < 1 << n; ++i) {
long long t = 1;
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
t *= nums[j];
}
}
ans = max(ans, t);
}
return ans;
}
};
```

```go
func maxStrength(nums []int) int64 {
ans := int64(-1e14)
for i := 1; i < 1<<len(nums); i++ {
var t int64 = 1
for j, x := range nums {
if i>>j&1 == 1 {
t *= int64(x)
}
}
ans = max(ans, t)
}
return ans
}
```

```ts
function maxStrength(nums: number[]): number {
let ans = -Infinity;
const n = nums.length;
for (let i = 1; i < 1 << n; ++i) {
let t = 1;
for (let j = 0; j < n; ++j) {
if ((i >> j) & 1) {
t *= nums[j];
}
}
ans = Math.max(ans, t);
}
return ans;
}
```

<!-- tabs:end -->

### Solution 2: Sorting + Greedy

First, we can sort the array. Based on the characteristics of the array, we can draw the following conclusions:

- If there is only one element in the array, then the maximum strength value is this element.
- If there are two or more elements in the array, and $nums[1] = nums[n - 1] = 0$, then the maximum strength value is $0$.
- 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.

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.

<!-- tabs:start -->

Expand Down
42 changes: 16 additions & 26 deletions solution/2700-2799/2708.Maximum Strength of a Group/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
class Solution {
public:
long long maxStrength(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
if (n == 1) {
return nums[0];
}
if (nums[1] == 0 && nums[n - 1] == 0) {
return 0;
}
long long ans = 1;
int i = 0;
while (i < n) {
if (nums[i] < 0 && i + 1 < n && nums[i + 1] < 0) {
ans *= nums[i] * nums[i + 1];
i += 2;
} else if (nums[i] <= 0) {
i += 1;
} else {
ans *= nums[i];
i += 1;
}
}
return ans;
}
class Solution {
public:
long long maxStrength(vector<int>& nums) {
long long ans = -1e14;
int n = nums.size();
for (int i = 1; i < 1 << n; ++i) {
long long t = 1;
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
t *= nums[j];
}
}
ans = max(ans, t);
}
return ans;
}
};
31 changes: 12 additions & 19 deletions solution/2700-2799/2708.Maximum Strength of a Group/Solution.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
func maxStrength(nums []int) int64 {
sort.Ints(nums)
n := len(nums)
if n == 1 {
return int64(nums[0])
}
if nums[1] == 0 && nums[n-1] == 0 {
return 0
}
ans := int64(1)
for i := 0; i < n; i++ {
if nums[i] < 0 && i+1 < n && nums[i+1] < 0 {
ans *= int64(nums[i] * nums[i+1])
i++
} else if nums[i] > 0 {
ans *= int64(nums[i])
}
}
return ans
func maxStrength(nums []int) int64 {
ans := int64(-1e14)
for i := 1; i < 1<<len(nums); i++ {
var t int64 = 1
for j, x := range nums {
if i>>j&1 == 1 {
t *= int64(x)
}
}
ans = max(ans, t)
}
return ans
}
40 changes: 15 additions & 25 deletions solution/2700-2799/2708.Maximum Strength of a Group/Solution.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
class Solution {
public long maxStrength(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
if (n == 1) {
return nums[0];
}
if (nums[1] == 0 && nums[n - 1] == 0) {
return 0;
}
long ans = 1;
int i = 0;
while (i < n) {
if (nums[i] < 0 && i + 1 < n && nums[i + 1] < 0) {
ans *= nums[i] * nums[i + 1];
i += 2;
} else if (nums[i] <= 0) {
i += 1;
} else {
ans *= nums[i];
i += 1;
}
}
return ans;
}
class Solution {
public long maxStrength(int[] nums) {
long ans = (long) -1e14;
int n = nums.length;
for (int i = 1; i < 1 << n; ++i) {
long t = 1;
for (int j = 0; j < n; ++j) {
if ((i >> j & 1) == 1) {
t *= nums[j];
}
}
ans = Math.max(ans, t);
}
return ans;
}
}
29 changes: 10 additions & 19 deletions solution/2700-2799/2708.Maximum Strength of a Group/Solution.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
class Solution:
def maxStrength(self, nums: List[int]) -> int:
nums.sort()
n = len(nums)
if n == 1:
return nums[0]
if nums[1] == nums[-1] == 0:
return 0
ans, i = 1, 0
while i < n:
if nums[i] < 0 and i + 1 < n and nums[i + 1] < 0:
ans *= nums[i] * nums[i + 1]
i += 2
elif nums[i] <= 0:
i += 1
else:
ans *= nums[i]
i += 1
return ans
class Solution:
def maxStrength(self, nums: List[int]) -> int:
ans = -inf
for i in range(1, 1 << len(nums)):
t = 1
for j, x in enumerate(nums):
if i >> j & 1:
t *= x
ans = max(ans, t)
return ans
Loading