Skip to content

Commit bbce022

Browse files
committed
feat: add solutions to lc problem: No.1785
No.1785.Minimum Elements to Add to Form a Given Sum
1 parent 61afad6 commit bbce022

File tree

8 files changed

+146
-23
lines changed

8 files changed

+146
-23
lines changed

solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,20 @@
4242

4343
**方法一:0 后面不能有 1**
4444

45-
注意到字符串 $s$ 不含前导零,说明 $s$ 以 "1" 开头,若字符串后面出现 "01",则不满足题意。
45+
注意到字符串 $s$ 不含前导零,说明 $s$ 以 '1' 开头。
46+
47+
若字符串 $s$ 存在 "01" 串,那么 $s$ 就变成形如 "1...01..." 的字符串,此时 $s$ 出现了至少两个连续的 '1' 片段,不满足题意,返回 `false`
48+
49+
若字符串 $s$ 不存在 "01" 串,那么 $s$ 只能是形如 "1..1000..." 的字符串,此时 $s$ 只有一个连续的 '1' 片段,满足题意,返回 `true`
50+
51+
因此,只需要判断字符串 $s$ 是否存在 "01" 串即可。
4652

4753
<!-- tabs:start -->
4854

4955
### **Python3**
5056

5157
<!-- 这里可写当前语言的特殊实现逻辑 -->
5258

53-
```python
54-
class Solution:
55-
def checkOnesSegment(self, s: str) -> bool:
56-
for i, c in enumerate(s):
57-
if c == '0':
58-
if s[:i].count('1') and s[i + 1 :].count('1'):
59-
return False
60-
return True
61-
```
62-
6359
```python
6460
class Solution:
6561
def checkOnesSegment(self, s: str) -> bool:

solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README_EN.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,6 @@
3636

3737
### **Python3**
3838

39-
```python
40-
class Solution:
41-
def checkOnesSegment(self, s: str) -> bool:
42-
for i, c in enumerate(s):
43-
if c == '0':
44-
if s[:i].count('1') and s[i + 1 :].count('1'):
45-
return False
46-
return True
47-
```
48-
4939
```python
5040
class Solution:
5141
def checkOnesSegment(self, s: str) -> bool:

solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,77 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:贪心**
48+
49+
先计算数组元素总和 $s$,然后计算 $s$ 与 $goal$ 的差值 $d$。
50+
51+
那么需要添加的元素数量为 $d$ 的绝对值除以 $limit$ 向上取整,即 $\lceil \frac{|d|}{limit} \rceil$。
52+
53+
注意,本题中数组元素的数据范围为 $[-10^6, 10^6]$,元素个数最大为 $10^5$,总和 $s$ 以及差值 $d$ 可能会超过 $32$ 位整数的表示范围,因此需要使用 $64$ 位整数。
54+
55+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
56+
4757
<!-- tabs:start -->
4858

4959
### **Python3**
5060

5161
<!-- 这里可写当前语言的特殊实现逻辑 -->
5262

5363
```python
54-
64+
class Solution:
65+
def minElements(self, nums: List[int], limit: int, goal: int) -> int:
66+
d = abs(sum(nums) - goal)
67+
return (d + limit - 1) // limit
5568
```
5669

5770
### **Java**
5871

5972
<!-- 这里可写当前语言的特殊实现逻辑 -->
6073

6174
```java
75+
class Solution {
76+
public int minElements(int[] nums, int limit, int goal) {
77+
long s = 0;
78+
for (int v : nums) {
79+
s += v;
80+
}
81+
long d = Math.abs(s - goal);
82+
return (int) ((d + limit - 1) / limit);
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int minElements(vector<int>& nums, int limit, int goal) {
93+
long long s = accumulate(nums.begin(), nums.end(), 0ll);
94+
long long d = abs(s - goal);
95+
return (d + limit - 1) / limit;
96+
}
97+
};
98+
```
6299
100+
### **Go**
101+
102+
```go
103+
func minElements(nums []int, limit int, goal int) int {
104+
s := 0
105+
for _, v := range nums {
106+
s += v
107+
}
108+
d := abs(s - goal)
109+
return (d + limit - 1) / limit
110+
}
111+
112+
func abs(x int) int {
113+
if x < 0 {
114+
return -x
115+
}
116+
return x
117+
}
63118
```
64119

65120
### **...**

solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README_EN.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,58 @@
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def minElements(self, nums: List[int], limit: int, goal: int) -> int:
48+
d = abs(sum(nums) - goal)
49+
return (d + limit - 1) // limit
4750
```
4851

4952
### **Java**
5053

5154
```java
55+
class Solution {
56+
public int minElements(int[] nums, int limit, int goal) {
57+
long s = 0;
58+
for (int v : nums) {
59+
s += v;
60+
}
61+
long d = Math.abs(s - goal);
62+
return (int) ((d + limit - 1) / limit);
63+
}
64+
}
65+
```
66+
67+
### **C++**
68+
69+
```cpp
70+
class Solution {
71+
public:
72+
int minElements(vector<int>& nums, int limit, int goal) {
73+
long long s = accumulate(nums.begin(), nums.end(), 0ll);
74+
long long d = abs(s - goal);
75+
return (d + limit - 1) / limit;
76+
}
77+
};
78+
```
5279
80+
### **Go**
81+
82+
```go
83+
func minElements(nums []int, limit int, goal int) int {
84+
s := 0
85+
for _, v := range nums {
86+
s += v
87+
}
88+
d := abs(s - goal)
89+
return (d + limit - 1) / limit
90+
}
91+
92+
func abs(x int) int {
93+
if x < 0 {
94+
return -x
95+
}
96+
return x
97+
}
5398
```
5499

55100
### **...**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int minElements(vector<int>& nums, int limit, int goal) {
4+
long long s = accumulate(nums.begin(), nums.end(), 0ll);
5+
long long d = abs(s - goal);
6+
return (d + limit - 1) / limit;
7+
}
8+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func minElements(nums []int, limit int, goal int) int {
2+
s := 0
3+
for _, v := range nums {
4+
s += v
5+
}
6+
d := abs(s - goal)
7+
return (d + limit - 1) / limit
8+
}
9+
10+
func abs(x int) int {
11+
if x < 0 {
12+
return -x
13+
}
14+
return x
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int minElements(int[] nums, int limit, int goal) {
3+
long s = 0;
4+
for (int v : nums) {
5+
s += v;
6+
}
7+
long d = Math.abs(s - goal);
8+
return (int) ((d + limit - 1) / limit);
9+
}
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def minElements(self, nums: List[int], limit: int, goal: int) -> int:
3+
d = abs(sum(nums) - goal)
4+
return (d + limit - 1) // limit

0 commit comments

Comments
 (0)