Skip to content

Commit 90f55e9

Browse files
authored
feat: add solutions to lc problems: No.3566,3567 (#4454)
* No.3566.Partition Array into Two Equal Product Subsets * No.3567.Minimum Absolute Difference in Sliding Submatrix
1 parent 15f7077 commit 90f55e9

File tree

14 files changed

+712
-16
lines changed

14 files changed

+712
-16
lines changed

solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README.md

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,134 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3566.Pa
6161

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

64-
### 方法一
64+
### 方法一:二进制枚举
65+
66+
我们可以使用二进制枚举的方式来检查所有可能的子集划分。对于每个子集划分,我们可以计算两个子集的乘积,并检查它们是否都等于目标值。
67+
68+
具体地,我们可以使用一个整数 $i$ 来表示子集划分的状态,其中 $i$ 的二进制位表示每个元素是否属于第一个子集。对于每个可能的 $i$,我们可以计算两个子集的乘积,并检查它们是否都等于目标值。
69+
70+
时间复杂度 $O(2^n \times n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
6571

6672
<!-- tabs:start -->
6773

6874
#### Python3
6975

7076
```python
71-
77+
class Solution:
78+
def checkEqualPartitions(self, nums: List[int], target: int) -> bool:
79+
n = len(nums)
80+
for i in range(1 << n):
81+
x = y = 1
82+
for j in range(n):
83+
if i >> j & 1:
84+
x *= nums[j]
85+
else:
86+
y *= nums[j]
87+
if x == target and y == target:
88+
return True
89+
return False
7290
```
7391

7492
#### Java
7593

7694
```java
77-
95+
class Solution {
96+
public boolean checkEqualPartitions(int[] nums, long target) {
97+
int n = nums.length;
98+
for (int i = 0; i < 1 << n; ++i) {
99+
long x = 1, y = 1;
100+
for (int j = 0; j < n; ++j) {
101+
if ((i >> j & 1) == 1) {
102+
x *= nums[j];
103+
} else {
104+
y *= nums[j];
105+
}
106+
}
107+
if (x == target && y == target) {
108+
return true;
109+
}
110+
}
111+
return false;
112+
}
113+
}
78114
```
79115

80116
#### C++
81117

82118
```cpp
83-
119+
class Solution {
120+
public:
121+
bool checkEqualPartitions(vector<int>& nums, long long target) {
122+
int n = nums.size();
123+
for (int i = 0; i < 1 << n; ++i) {
124+
long long x = 1, y = 1;
125+
for (int j = 0; j < n; ++j) {
126+
if ((i >> j & 1) == 1) {
127+
x *= nums[j];
128+
} else {
129+
y *= nums[j];
130+
}
131+
if (x > target || y > target) {
132+
break;
133+
}
134+
}
135+
if (x == target && y == target) {
136+
return true;
137+
}
138+
}
139+
return false;
140+
}
141+
};
84142
```
85143
86144
#### Go
87145
88146
```go
147+
func checkEqualPartitions(nums []int, target int64) bool {
148+
n := len(nums)
149+
for i := 0; i < 1<<n; i++ {
150+
x, y := int64(1), int64(1)
151+
for j, v := range nums {
152+
if i>>j&1 == 1 {
153+
x *= int64(v)
154+
} else {
155+
y *= int64(v)
156+
}
157+
if x > target || y > target {
158+
break
159+
}
160+
}
161+
if x == target && y == target {
162+
return true
163+
}
164+
}
165+
return false
166+
}
167+
```
89168

169+
#### TypeScript
170+
171+
```ts
172+
function checkEqualPartitions(nums: number[], target: number): boolean {
173+
const n = nums.length;
174+
for (let i = 0; i < 1 << n; ++i) {
175+
let [x, y] = [1, 1];
176+
for (let j = 0; j < n; ++j) {
177+
if (((i >> j) & 1) === 1) {
178+
x *= nums[j];
179+
} else {
180+
y *= nums[j];
181+
}
182+
if (x > target || y > target) {
183+
break;
184+
}
185+
}
186+
if (x === target && y === target) {
187+
return true;
188+
}
189+
}
190+
return false;
191+
}
90192
```
91193

92194
<!-- tabs:end -->

solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README_EN.md

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,134 @@ A <strong>subset</strong> of an array is a selection of elements of the array.
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Binary Enumeration
61+
62+
We can use binary enumeration to check all possible subset partitions. For each subset partition, we can calculate the product of the two subsets and check whether both are equal to the target value.
63+
64+
Specifically, we can use an integer $i$ to represent the state of the subset partition, where the binary bits of $i$ indicate whether each element belongs to the first subset. For each possible $i$, we calculate the product of the two subsets and check whether both are equal to the target value.
65+
66+
The time complexity is $O(2^n \times n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
6167

6268
<!-- tabs:start -->
6369

6470
#### Python3
6571

6672
```python
67-
73+
class Solution:
74+
def checkEqualPartitions(self, nums: List[int], target: int) -> bool:
75+
n = len(nums)
76+
for i in range(1 << n):
77+
x = y = 1
78+
for j in range(n):
79+
if i >> j & 1:
80+
x *= nums[j]
81+
else:
82+
y *= nums[j]
83+
if x == target and y == target:
84+
return True
85+
return False
6886
```
6987

7088
#### Java
7189

7290
```java
73-
91+
class Solution {
92+
public boolean checkEqualPartitions(int[] nums, long target) {
93+
int n = nums.length;
94+
for (int i = 0; i < 1 << n; ++i) {
95+
long x = 1, y = 1;
96+
for (int j = 0; j < n; ++j) {
97+
if ((i >> j & 1) == 1) {
98+
x *= nums[j];
99+
} else {
100+
y *= nums[j];
101+
}
102+
}
103+
if (x == target && y == target) {
104+
return true;
105+
}
106+
}
107+
return false;
108+
}
109+
}
74110
```
75111

76112
#### C++
77113

78114
```cpp
79-
115+
class Solution {
116+
public:
117+
bool checkEqualPartitions(vector<int>& nums, long long target) {
118+
int n = nums.size();
119+
for (int i = 0; i < 1 << n; ++i) {
120+
long long x = 1, y = 1;
121+
for (int j = 0; j < n; ++j) {
122+
if ((i >> j & 1) == 1) {
123+
x *= nums[j];
124+
} else {
125+
y *= nums[j];
126+
}
127+
if (x > target || y > target) {
128+
break;
129+
}
130+
}
131+
if (x == target && y == target) {
132+
return true;
133+
}
134+
}
135+
return false;
136+
}
137+
};
80138
```
81139
82140
#### Go
83141
84142
```go
143+
func checkEqualPartitions(nums []int, target int64) bool {
144+
n := len(nums)
145+
for i := 0; i < 1<<n; i++ {
146+
x, y := int64(1), int64(1)
147+
for j, v := range nums {
148+
if i>>j&1 == 1 {
149+
x *= int64(v)
150+
} else {
151+
y *= int64(v)
152+
}
153+
if x > target || y > target {
154+
break
155+
}
156+
}
157+
if x == target && y == target {
158+
return true
159+
}
160+
}
161+
return false
162+
}
163+
```
85164

165+
#### TypeScript
166+
167+
```ts
168+
function checkEqualPartitions(nums: number[], target: number): boolean {
169+
const n = nums.length;
170+
for (let i = 0; i < 1 << n; ++i) {
171+
let [x, y] = [1, 1];
172+
for (let j = 0; j < n; ++j) {
173+
if (((i >> j) & 1) === 1) {
174+
x *= nums[j];
175+
} else {
176+
y *= nums[j];
177+
}
178+
if (x > target || y > target) {
179+
break;
180+
}
181+
}
182+
if (x === target && y === target) {
183+
return true;
184+
}
185+
}
186+
return false;
187+
}
86188
```
87189

88190
<!-- tabs:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool checkEqualPartitions(vector<int>& nums, long long target) {
4+
int n = nums.size();
5+
for (int i = 0; i < 1 << n; ++i) {
6+
long long x = 1, y = 1;
7+
for (int j = 0; j < n; ++j) {
8+
if ((i >> j & 1) == 1) {
9+
x *= nums[j];
10+
} else {
11+
y *= nums[j];
12+
}
13+
if (x > target || y > target) {
14+
break;
15+
}
16+
}
17+
if (x == target && y == target) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func checkEqualPartitions(nums []int, target int64) bool {
2+
n := len(nums)
3+
for i := 0; i < 1<<n; i++ {
4+
x, y := int64(1), int64(1)
5+
for j, v := range nums {
6+
if i>>j&1 == 1 {
7+
x *= int64(v)
8+
} else {
9+
y *= int64(v)
10+
}
11+
if x > target || y > target {
12+
break
13+
}
14+
}
15+
if x == target && y == target {
16+
return true
17+
}
18+
}
19+
return false
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean checkEqualPartitions(int[] nums, long target) {
3+
int n = nums.length;
4+
for (int i = 0; i < 1 << n; ++i) {
5+
long x = 1, y = 1;
6+
for (int j = 0; j < n; ++j) {
7+
if ((i >> j & 1) == 1) {
8+
x *= nums[j];
9+
} else {
10+
y *= nums[j];
11+
}
12+
}
13+
if (x == target && y == target) {
14+
return true;
15+
}
16+
}
17+
return false;
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def checkEqualPartitions(self, nums: List[int], target: int) -> bool:
3+
n = len(nums)
4+
for i in range(1 << n):
5+
x = y = 1
6+
for j in range(n):
7+
if i >> j & 1:
8+
x *= nums[j]
9+
else:
10+
y *= nums[j]
11+
if x == target and y == target:
12+
return True
13+
return False
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function checkEqualPartitions(nums: number[], target: number): boolean {
2+
const n = nums.length;
3+
for (let i = 0; i < 1 << n; ++i) {
4+
let [x, y] = [1, 1];
5+
for (let j = 0; j < n; ++j) {
6+
if (((i >> j) & 1) === 1) {
7+
x *= nums[j];
8+
} else {
9+
y *= nums[j];
10+
}
11+
if (x > target || y > target) {
12+
break;
13+
}
14+
}
15+
if (x === target && y === target) {
16+
return true;
17+
}
18+
}
19+
return false;
20+
}

0 commit comments

Comments
 (0)