Skip to content

Commit 0441ef3

Browse files
authored
Improved task 1785.
1 parent 6320232 commit 0441ef3

File tree

1 file changed

+16
-11
lines changed
  • src/main/java/g1701_1800/s1785_minimum_elements_to_add_to_form_a_given_sum

1 file changed

+16
-11
lines changed
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
1784\. Check if Binary String Has at Most One Segment of Ones
1+
1785\. Minimum Elements to Add to Form a Given Sum
22

3-
Easy
3+
Medium
44

5-
Given a binary string `s` **without leading zeros**, return `true` _if_ `s` _contains **at most one contiguous segment of ones**_. Otherwise, return `false`.
5+
You are given an integer array `nums` and two integers `limit` and `goal`. The array `nums` has an interesting property that `abs(nums[i]) <= limit`.
6+
7+
Return _the minimum number of elements you need to add to make the sum of the array equal to_ `goal`. The array must maintain its property that `abs(nums[i]) <= limit`.
8+
9+
Note that `abs(x)` equals `x` if `x >= 0`, and `-x` otherwise.
610

711
**Example 1:**
812

9-
**Input:** s = "1001"
13+
**Input:** nums = [1,-1,1], limit = 3, goal = -4
1014

11-
**Output:** false
15+
**Output:** 2
1216

13-
**Explanation:** The ones do not form a contiguous segment.
17+
**Explanation:** You can add -2 and -3, then the sum of the array will be 1 - 1 + 1 - 2 - 3 = -4.
1418

1519
**Example 2:**
1620

17-
**Input:** s = "110"
21+
**Input:** nums = [1,-10,9,1], limit = 100, goal = 0
1822

19-
**Output:** true
23+
**Output:** 1
2024

2125
**Constraints:**
2226

23-
* `1 <= s.length <= 100`
24-
* `s[i]` is either `'0'` or `'1'`.
25-
* `s[0]` is `'1'`.
27+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
28+
* <code>1 <= limit <= 10<sup>6</sup></code>
29+
* `-limit <= nums[i] <= limit`
30+
* <code>-10<sup>9</sup> <= goal <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)