Skip to content

Commit 1106d76

Browse files
authored
Added tasks 1753, 1754, 1755, 1758, 1759.
1 parent eb3523a commit 1106d76

File tree

15 files changed

+533
-0
lines changed

15 files changed

+533
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g1701_1800.s1753_maximum_score_from_removing_stones;
2+
3+
// #Medium #Math #Greedy #Heap_Priority_Queue #2022_04_30_Time_0_ms_(100.00%)_Space_41.5_MB_(70.96%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maximumScore(int a, int b, int c) {
9+
int[] nums = new int[] {a, b, c};
10+
Arrays.sort(nums);
11+
if (nums[0] + nums[1] < nums[2]) {
12+
return nums[0] + nums[1];
13+
} else {
14+
return (nums[0] + nums[1] + nums[2]) / 2;
15+
}
16+
}
17+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
1753\. Maximum Score From Removing Stones
2+
3+
Medium
4+
5+
You are playing a solitaire game with **three piles** of stones of sizes `a`, `b`, and `c` respectively. Each turn you choose two **different non-empty** piles, take one stone from each, and add `1` point to your score. The game stops when there are **fewer than two non-empty** piles (meaning there are no more available moves).
6+
7+
Given three integers `a`, `b`, and `c`, return _the_ **_maximum_** _**score** you can get._
8+
9+
**Example 1:**
10+
11+
**Input:** a = 2, b = 4, c = 6
12+
13+
**Output:** 6
14+
15+
**Explanation:** The starting state is (2, 4, 6). One optimal set of moves is:
16+
17+
- Take from 1st and 3rd piles, state is now (1, 4, 5)
18+
19+
- Take from 1st and 3rd piles, state is now (0, 4, 4)
20+
21+
- Take from 2nd and 3rd piles, state is now (0, 3, 3)
22+
23+
- Take from 2nd and 3rd piles, state is now (0, 2, 2)
24+
25+
- Take from 2nd and 3rd piles, state is now (0, 1, 1)
26+
27+
- Take from 2nd and 3rd piles, state is now (0, 0, 0)
28+
29+
There are fewer than two non-empty piles, so the game ends. Total: 6 points.
30+
31+
**Example 2:**
32+
33+
**Input:** a = 4, b = 4, c = 6
34+
35+
**Output:** 7
36+
37+
**Explanation:** The starting state is (4, 4, 6). One optimal set of moves is:
38+
39+
- Take from 1st and 2nd piles, state is now (3, 3, 6)
40+
41+
- Take from 1st and 3rd piles, state is now (2, 3, 5)
42+
43+
- Take from 1st and 3rd piles, state is now (1, 3, 4)
44+
45+
- Take from 1st and 3rd piles, state is now (0, 3, 3)
46+
47+
- Take from 2nd and 3rd piles, state is now (0, 2, 2)
48+
49+
- Take from 2nd and 3rd piles, state is now (0, 1, 1)
50+
51+
- Take from 2nd and 3rd piles, state is now (0, 0, 0)
52+
53+
There are fewer than two non-empty piles, so the game ends. Total: 7 points.
54+
55+
**Example 3:**
56+
57+
**Input:** a = 1, b = 8, c = 8
58+
59+
**Output:** 8
60+
61+
**Explanation:** One optimal set of moves is to take from the 2nd and 3rd piles for 8 turns until they are empty. After that, there are fewer than two non-empty piles, so the game ends.
62+
63+
**Constraints:**
64+
65+
* <code>1 <= a, b, c <= 10<sup>5</sup></code>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g1701_1800.s1754_largest_merge_of_two_strings;
2+
3+
// #Medium #String #Greedy #Two_Pointers #2022_04_30_Time_37_ms_(89.23%)_Space_53.8_MB_(53.08%)
4+
5+
public class Solution {
6+
public String largestMerge(String word1, String word2) {
7+
char[] a = word1.toCharArray();
8+
char[] b = word2.toCharArray();
9+
StringBuilder sb = new StringBuilder();
10+
int i = 0;
11+
int j = 0;
12+
while (i < a.length && j < b.length) {
13+
if (a[i] == b[j]) {
14+
boolean first = go(a, i, b, j);
15+
if (first) {
16+
sb.append(a[i]);
17+
i++;
18+
} else {
19+
sb.append(b[j]);
20+
j++;
21+
}
22+
} else {
23+
if (a[i] > b[j]) {
24+
sb.append(a[i]);
25+
i++;
26+
} else {
27+
sb.append(b[j]);
28+
j++;
29+
}
30+
}
31+
}
32+
33+
while (i < a.length) {
34+
sb.append(a[i++]);
35+
}
36+
while (j < b.length) {
37+
sb.append(b[j++]);
38+
}
39+
40+
return sb.toString();
41+
}
42+
43+
private boolean go(char[] a, int i, char[] b, int j) {
44+
while (i < a.length && j < b.length && a[i] == b[j]) {
45+
i++;
46+
j++;
47+
}
48+
if (i == a.length) {
49+
return false;
50+
}
51+
if (j == b.length) {
52+
return true;
53+
}
54+
return a[i] > b[j];
55+
}
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
1754\. Largest Merge Of Two Strings
2+
3+
Medium
4+
5+
You are given two strings `word1` and `word2`. You want to construct a string `merge` in the following way: while either `word1` or `word2` are non-empty, choose **one** of the following options:
6+
7+
* If `word1` is non-empty, append the **first** character in `word1` to `merge` and delete it from `word1`.
8+
* For example, if `word1 = "abc"` and `merge = "dv"`, then after choosing this operation, `word1 = "bc"` and `merge = "dva"`.
9+
* If `word2` is non-empty, append the **first** character in `word2` to `merge` and delete it from `word2`.
10+
* For example, if `word2 = "abc"` and `merge = ""`, then after choosing this operation, `word2 = "bc"` and `merge = "a"`.
11+
12+
Return _the lexicographically **largest**_ `merge` _you can construct_.
13+
14+
A string `a` is lexicographically larger than a string `b` (of the same length) if in the first position where `a` and `b` differ, `a` has a character strictly larger than the corresponding character in `b`. For example, `"abcd"` is lexicographically larger than `"abcc"` because the first position they differ is at the fourth character, and `d` is greater than `c`.
15+
16+
**Example 1:**
17+
18+
**Input:** word1 = "cabaa", word2 = "bcaaa"
19+
20+
**Output:** "cbcabaaaaa"
21+
22+
**Explanation:** One way to get the lexicographically largest merge is:
23+
24+
- Take from word1: merge = "c", word1 = "abaa", word2 = "bcaaa"
25+
26+
- Take from word2: merge = "cb", word1 = "abaa", word2 = "caaa"
27+
28+
- Take from word2: merge = "cbc", word1 = "abaa", word2 = "aaa"
29+
30+
- Take from word1: merge = "cbca", word1 = "baa", word2 = "aaa"
31+
32+
- Take from word1: merge = "cbcab", word1 = "aa", word2 = "aaa"
33+
34+
- Append the remaining 5 a's from word1 and word2 at the end of merge.
35+
36+
**Example 2:**
37+
38+
**Input:** word1 = "abcabc", word2 = "abdcaba"
39+
40+
**Output:** "abdcabcabcaba"
41+
42+
**Constraints:**
43+
44+
* `1 <= word1.length, word2.length <= 3000`
45+
* `word1` and `word2` consist only of lowercase English letters.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g1701_1800.s1755_closest_subsequence_sum;
2+
3+
// #Hard #Array #Dynamic_Programming #Two_Pointers #Bit_Manipulation #Bitmask
4+
// #2022_04_30_Time_383_ms_(87.60%)_Space_65.5_MB_(78.51%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
private int idx;
10+
private int sum;
11+
12+
public int minAbsDifference(int[] nums, int goal) {
13+
int n = nums.length;
14+
15+
int nFirst = (int) Math.pow(2, (double) n / 2);
16+
int nSecond = (int) Math.pow(2, n - n / 2);
17+
18+
int[] first = new int[nFirst];
19+
int[] second = new int[nSecond];
20+
21+
helper(nums, first, 0, n / 2 - 1);
22+
idx = sum = 0;
23+
helper(nums, second, n / 2, n - 1);
24+
25+
Arrays.sort(first);
26+
Arrays.sort(second);
27+
28+
int low = 0;
29+
int high = nSecond - 1;
30+
31+
int ans = Integer.MAX_VALUE;
32+
while (low < nFirst && high >= 0) {
33+
int sum = first[low] + second[high];
34+
ans = Math.min(ans, Math.abs(sum - goal));
35+
36+
if (ans == 0) {
37+
break;
38+
}
39+
40+
if (sum < goal) {
41+
low++;
42+
} else {
43+
high--;
44+
}
45+
}
46+
47+
return ans;
48+
}
49+
50+
private void helper(int[] nums, int[] arr, int start, int end) {
51+
for (int i = start; i <= end; i++) {
52+
sum += nums[i];
53+
arr[idx++] = sum;
54+
helper(nums, arr, i + 1, end);
55+
sum -= nums[i];
56+
}
57+
}
58+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1755\. Closest Subsequence Sum
2+
3+
Hard
4+
5+
You are given an integer array `nums` and an integer `goal`.
6+
7+
You want to choose a subsequence of `nums` such that the sum of its elements is the closest possible to `goal`. That is, if the sum of the subsequence's elements is `sum`, then you want to **minimize the absolute difference** `abs(sum - goal)`.
8+
9+
Return _the **minimum** possible value of_ `abs(sum - goal)`.
10+
11+
Note that a subsequence of an array is an array formed by removing some elements **(possibly all or none)** of the original array.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [5,-7,3,5], goal = 6
16+
17+
**Output:** 0
18+
19+
**Explanation:** Choose the whole array as a subsequence, with a sum of 6. This is equal to the goal, so the absolute difference is 0.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [7,-9,15,-2], goal = -5
24+
25+
**Output:** 1
26+
27+
**Explanation:** Choose the subsequence [7,-9,-2], with a sum of -4. The absolute difference is abs(-4 - (-5)) = abs(1) = 1, which is the minimum.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [1,2,3], goal = -7
32+
33+
**Output:** 7
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 40`
38+
* <code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code>
39+
* <code>-10<sup>9</sup> <= goal <= 10<sup>9</sup></code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g1701_1800.s1758_minimum_changes_to_make_alternating_binary_string;
2+
3+
// #Easy #String #2022_04_30_Time_10_ms_(9.93%)_Space_42.9_MB_(56.74%)
4+
5+
public class Solution {
6+
public int minOperations(String s) {
7+
int ops1 = 0;
8+
for (int i = 0; i < s.length(); i++) {
9+
if (i % 2 == 0) {
10+
if (s.charAt(i) != '0') {
11+
ops1++;
12+
}
13+
} else {
14+
if (s.charAt(i) != '1') {
15+
ops1++;
16+
}
17+
}
18+
}
19+
20+
int ops2 = 0;
21+
for (int i = 0; i < s.length(); i++) {
22+
if (i % 2 == 0) {
23+
if (s.charAt(i) != '1') {
24+
ops2++;
25+
}
26+
} else {
27+
if (s.charAt(i) != '0') {
28+
ops2++;
29+
}
30+
}
31+
}
32+
return Math.min(ops1, ops2);
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1758\. Minimum Changes To Make Alternating Binary String
2+
3+
Easy
4+
5+
You are given a string `s` consisting only of the characters `'0'` and `'1'`. In one operation, you can change any `'0'` to `'1'` or vice versa.
6+
7+
The string is called alternating if no two adjacent characters are equal. For example, the string `"010"` is alternating, while the string `"0100"` is not.
8+
9+
Return _the **minimum** number of operations needed to make_ `s` _alternating_.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "0100"
14+
15+
**Output:** 1
16+
17+
**Explanation:** If you change the last character to '1', s will be "0101", which is alternating.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "10"
22+
23+
**Output:** 0
24+
25+
**Explanation:** s is already alternating.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "1111"
30+
31+
**Output:** 2
32+
33+
**Explanation:** You need two operations to reach "0101" or "1010".
34+
35+
**Constraints:**
36+
37+
* <code>1 <= s.length <= 10<sup>4</sup></code>
38+
* `s[i]` is either `'0'` or `'1'`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g1701_1800.s1759_count_number_of_homogenous_substrings;
2+
3+
// #Medium #String #Math #2022_04_30_Time_19_ms_(42.40%)_Space_51.3_MB_(28.80%)
4+
5+
public class Solution {
6+
public int countHomogenous(String s) {
7+
int total = 0;
8+
int count = 0;
9+
for (int i = 0; i < s.length(); i++) {
10+
if (i > 0 && s.charAt(i) == s.charAt(i - 1)) {
11+
count++;
12+
} else {
13+
count = 1;
14+
}
15+
total = (total + count) % 1000000007;
16+
}
17+
return total;
18+
}
19+
}

0 commit comments

Comments
 (0)