Skip to content

Commit e0566b6

Browse files
committed
+ problem 1250
1 parent fd9d1f1 commit e0566b6

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 1250. Check If It Is a Good Array
2+
Given an array `nums` of positive integers. Your task is to select some subset of `nums`, multiply each element by an integer and add all these numbers. The array is said to be **good** if you can obtain a sum of `1` from the array by any possible subset and multiplicand.
3+
4+
Return `True` if the array is **good** otherwise return `False`.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> nums = [12,5,7,23]
9+
<strong>Output:</strong> true
10+
<strong>Explanation:</strong> Pick numbers 5 and 7.
11+
5*3 + 7*(-2) = 1
12+
</pre>
13+
14+
#### Example 2:
15+
<pre>
16+
<strong>Input:</strong> nums = [29,6,10]
17+
<strong>Output:</strong> true
18+
<strong>Explanation:</strong> Pick numbers 29, 6 and 10.
19+
29*1 + 6*(-3) + 10*(-1) = 1
20+
</pre>
21+
22+
#### Example 3:
23+
<pre>
24+
<strong>Input:</strong> nums = [3,6]
25+
<strong>Output:</strong> false
26+
</pre>
27+
28+
#### Constraints:
29+
* `1 <= nums.length <= 10^5`
30+
* `1 <= nums[i] <= 10^9`
31+
32+
## Solutions (Python)
33+
34+
### 1. Solution
35+
```Python
36+
class Solution:
37+
def isGoodArray(self, nums: List[int]) -> bool:
38+
return math.gcd(*nums) == 1
39+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 1250. 检查「好数组」
2+
给你一个正整数数组 `nums`,你需要从中任选一些子集,然后将子集中每一个数乘以一个 **任意整数**,并求出他们的和。
3+
4+
假如该和结果为 `1`,那么原数组就是一个「**好数组**」,则返回 `True`;否则请返回 `False`
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> nums = [12,5,7,23]
9+
<strong>输出:</strong> true
10+
<strong>解释:</strong> 挑选数字 5 和 7。
11+
5*3 + 7*(-2) = 1
12+
</pre>
13+
14+
#### 示例 2:
15+
<pre>
16+
<strong>输入:</strong> nums = [29,6,10]
17+
<strong>输出:</strong> true
18+
<strong>解释:</strong> 挑选数字 29, 6 和 10。
19+
29*1 + 6*(-3) + 10*(-1) = 1
20+
</pre>
21+
22+
#### 示例 3:
23+
<pre>
24+
<strong>输入:</strong> nums = [3,6]
25+
<strong>输出:</strong> false
26+
</pre>
27+
28+
#### 提示:
29+
* `1 <= nums.length <= 10^5`
30+
* `1 <= nums[i] <= 10^9`
31+
32+
## 题解 (Python)
33+
34+
### 1. 题解
35+
```Python
36+
class Solution:
37+
def isGoodArray(self, nums: List[int]) -> bool:
38+
return math.gcd(*nums) == 1
39+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def isGoodArray(self, nums: List[int]) -> bool:
3+
return math.gcd(*nums) == 1

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@
679679
[1247][1247l]|[Minimum Swaps to Make Strings Equal][1247] |![py]
680680
[1248][1248l]|[Count Number of Nice Subarrays][1248] |![rb]&nbsp;&nbsp;![rs]
681681
[1249][1249l]|[Minimum Remove to Make Valid Parentheses][1249] |![rs]
682+
[1250][1250l]|[Check If It Is a Good Array][1250] |![py]
682683
[1252][1252l]|[Cells with Odd Values in a Matrix][1252] |![rs]
683684
[1253][1253l]|[Reconstruct a 2-Row Binary Matrix][1253] |![rb]&nbsp;&nbsp;![rs]
684685
[1255][1255l]|[Maximum Score Words Formed by Letters][1255] |![rs]
@@ -1996,6 +1997,7 @@
19961997
[1247]:Problemset/1247-Minimum%20Swaps%20to%20Make%20Strings%20Equal/README.md#1247-minimum-swaps-to-make-strings-equal
19971998
[1248]:Problemset/1248-Count%20Number%20of%20Nice%20Subarrays/README.md#1248-count-number-of-nice-subarrays
19981999
[1249]:Problemset/1249-Minimum%20Remove%20to%20Make%20Valid%20Parentheses/README.md#1249-minimum-remove-to-make-valid-parentheses
2000+
[1250]:Problemset/1250-Check%20If%20It%20Is%20a%20Good%20Array/README.md#1250-check-if-it-is-a-good-array
19992001
[1252]:Problemset/1252-Cells%20with%20Odd%20Values%20in%20a%20Matrix/README.md#1252-cells-with-odd-values-in-a-matrix
20002002
[1253]:Problemset/1253-Reconstruct%20a%202-Row%20Binary%20Matrix/README.md#1253-reconstruct-a-2-row-binary-matrix
20012003
[1255]:Problemset/1255-Maximum%20Score%20Words%20Formed%20by%20Letters/README.md#1255-maximum-score-words-formed-by-letters
@@ -3316,6 +3318,7 @@
33163318
[1247l]:https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/
33173319
[1248l]:https://leetcode.com/problems/count-number-of-nice-subarrays/
33183320
[1249l]:https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/
3321+
[1250l]:https://leetcode.com/problems/check-if-it-is-a-good-array/
33193322
[1252l]:https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/
33203323
[1253l]:https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/
33213324
[1255l]:https://leetcode.com/problems/maximum-score-words-formed-by-letters/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@
679679
[1247][1247l]|[交换字符使得字符串相同][1247] |![py]
680680
[1248][1248l]|[统计「优美子数组」][1248] |![rb]&nbsp;&nbsp;![rs]
681681
[1249][1249l]|[移除无效的括号][1249] |![rs]
682+
[1250][1250l]|[检查「好数组」][1250] |![py]
682683
[1252][1252l]|[奇数值单元格的数目][1252] |![rs]
683684
[1253][1253l]|[重构 2 行二进制矩阵][1253] |![rb]&nbsp;&nbsp;![rs]
684685
[1255][1255l]|[得分最高的单词集合][1255] |![rs]
@@ -1996,6 +1997,7 @@
19961997
[1247]:Problemset/1247-Minimum%20Swaps%20to%20Make%20Strings%20Equal/README_CN.md#1247-交换字符使得字符串相同
19971998
[1248]:Problemset/1248-Count%20Number%20of%20Nice%20Subarrays/README_CN.md#1248-统计优美子数组
19981999
[1249]:Problemset/1249-Minimum%20Remove%20to%20Make%20Valid%20Parentheses/README_CN.md#1249-移除无效的括号
2000+
[1250]:Problemset/1250-Check%20If%20It%20Is%20a%20Good%20Array/README_CN.md#1250-检查「好数组」
19992001
[1252]:Problemset/1252-Cells%20with%20Odd%20Values%20in%20a%20Matrix/README_CN.md#1252-奇数值单元格的数目
20002002
[1253]:Problemset/1253-Reconstruct%20a%202-Row%20Binary%20Matrix/README_CN.md#1253-重构-2-行二进制矩阵
20012003
[1255]:Problemset/1255-Maximum%20Score%20Words%20Formed%20by%20Letters/README_CN.md#1255-得分最高的单词集合
@@ -3316,6 +3318,7 @@
33163318
[1247l]:https://leetcode.cn/problems/minimum-swaps-to-make-strings-equal/
33173319
[1248l]:https://leetcode.cn/problems/count-number-of-nice-subarrays/
33183320
[1249l]:https://leetcode.cn/problems/minimum-remove-to-make-valid-parentheses/
3321+
[1250l]:https://leetcode.cn/problems/check-if-it-is-a-good-array/
33193322
[1252l]:https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/
33203323
[1253l]:https://leetcode.cn/problems/reconstruct-a-2-row-binary-matrix/
33213324
[1255l]:https://leetcode.cn/problems/maximum-score-words-formed-by-letters/

0 commit comments

Comments
 (0)