Skip to content

Commit 6a15b13

Browse files
authored
Added tasks 1784, 1785.
1 parent 71ffe78 commit 6a15b13

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g1701_1800.s1784_check_if_binary_string_has_at_most_one_segment_of_ones;
2+
3+
// #Easy #String #2022_04_30_Time_1_ms_(65.60%)_Space_42.6_MB_(11.20%)
4+
5+
public class Solution {
6+
public boolean checkOnesSegment(String s) {
7+
boolean metOne = false;
8+
for (int i = 0; i < s.length(); i++) {
9+
if (s.charAt(i) == '1' && metOne) {
10+
return false;
11+
}
12+
if (s.charAt(i) == '1') {
13+
metOne = true;
14+
while (i < s.length() && s.charAt(i) == '1') {
15+
i++;
16+
}
17+
}
18+
}
19+
return true;
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
1784\. Check if Binary String Has at Most One Segment of Ones
2+
3+
Easy
4+
5+
Given a binary string `s` **without leading zeros**, return `true` _if_ `s` _contains **at most one contiguous segment of ones**_. Otherwise, return `false`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "1001"
10+
11+
**Output:** false
12+
13+
**Explanation:** The ones do not form a contiguous segment.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "110"
18+
19+
**Output:** true
20+
21+
**Constraints:**
22+
23+
* `1 <= s.length <= 100`
24+
* `s[i]` is either `'0'` or `'1'`.
25+
* `s[0]` is `'1'`.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g1701_1800.s1785_minimum_elements_to_add_to_form_a_given_sum;
2+
3+
// #Medium #Array #Greedy #2022_04_30_Time_2_ms_(70.75%)_Space_82.5_MB_(57.55%)
4+
5+
public class Solution {
6+
public int minElements(int[] nums, int limit, int goal) {
7+
long sum = 0;
8+
for (int num : nums) {
9+
sum += num;
10+
}
11+
long diff = Math.abs(goal - sum);
12+
return diff % limit == 0 ? (int) (diff / limit) : (int) ((diff / limit) + 1);
13+
}
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
1784\. Check if Binary String Has at Most One Segment of Ones
2+
3+
Easy
4+
5+
Given a binary string `s` **without leading zeros**, return `true` _if_ `s` _contains **at most one contiguous segment of ones**_. Otherwise, return `false`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "1001"
10+
11+
**Output:** false
12+
13+
**Explanation:** The ones do not form a contiguous segment.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "110"
18+
19+
**Output:** true
20+
21+
**Constraints:**
22+
23+
* `1 <= s.length <= 100`
24+
* `s[i]` is either `'0'` or `'1'`.
25+
* `s[0]` is `'1'`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1701_1800.s1784_check_if_binary_string_has_at_most_one_segment_of_ones;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void checkOnesSegment() {
11+
assertThat(new Solution().checkOnesSegment("1001"), equalTo(false));
12+
}
13+
14+
@Test
15+
void checkOnesSegment2() {
16+
assertThat(new Solution().checkOnesSegment("110"), equalTo(true));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1701_1800.s1785_minimum_elements_to_add_to_form_a_given_sum;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minElements() {
11+
assertThat(new Solution().minElements(new int[] {1, -1, 1}, 3, -4), equalTo(2));
12+
}
13+
14+
@Test
15+
void minElements2() {
16+
assertThat(new Solution().minElements(new int[] {1, -10, 9, 1}, 100, 0), equalTo(1));
17+
}
18+
}

0 commit comments

Comments
 (0)