|
37 | 37 |
|
38 | 38 | ## Solutions
|
39 | 39 |
|
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. |
41 | 146 |
|
42 | 147 | <!-- tabs:start -->
|
43 | 148 |
|
|
0 commit comments