Skip to content

Commit a323752

Browse files
authored
Added tasks 1863, 1864, 1865, 1866, 1869.
1 parent b239073 commit a323752

File tree

15 files changed

+529
-0
lines changed

15 files changed

+529
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g1801_1900.s1863_sum_of_all_subset_xor_totals;
2+
3+
// #Easy #Array #Math #Bit_Manipulation #Backtracking #Combinatorics
4+
// #2022_05_10_Time_22_ms_(11.36%)_Space_52.2_MB_(6.49%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
@SuppressWarnings("java:S5413")
10+
public class Solution {
11+
public int subsetXORSum(int[] nums) {
12+
int sum = 0;
13+
List<List<Integer>> subsets = subsets(nums);
14+
for (List<Integer> subset : subsets) {
15+
int xor = 0;
16+
for (int i : subset) {
17+
xor ^= i;
18+
}
19+
sum += xor;
20+
}
21+
return sum;
22+
}
23+
24+
private List<List<Integer>> subsets(int[] nums) {
25+
List<List<Integer>> result = new ArrayList<>();
26+
backtracking(result, new ArrayList<>(), nums, 0);
27+
return result;
28+
}
29+
30+
private void backtracking(
31+
List<List<Integer>> result, List<Integer> list, int[] nums, int start) {
32+
result.add(new ArrayList<>(list));
33+
for (int i = start; i < nums.length; i++) {
34+
list.add(nums[i]);
35+
backtracking(result, list, nums, i + 1);
36+
list.remove(list.size() - 1);
37+
}
38+
}
39+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
1863\. Sum of All Subset XOR Totals
2+
3+
Easy
4+
5+
The **XOR total** of an array is defined as the bitwise `XOR` of **all its elements**, or `0` if the array is **empty**.
6+
7+
* For example, the **XOR total** of the array `[2,5,6]` is `2 XOR 5 XOR 6 = 1`.
8+
9+
Given an array `nums`, return _the **sum** of all **XOR totals** for every **subset** of_ `nums`.
10+
11+
**Note:** Subsets with the **same** elements should be counted **multiple** times.
12+
13+
An array `a` is a **subset** of an array `b` if `a` can be obtained from `b` by deleting some (possibly zero) elements of `b`.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,3]
18+
19+
**Output:** 6
20+
21+
**Explanation:** The 4 subsets of [1,3] are:
22+
23+
- The empty subset has an XOR total of 0.
24+
25+
- [1] has an XOR total of 1.
26+
27+
- [3] has an XOR total of 3.
28+
29+
- [1,3] has an XOR total of 1 XOR 3 = 2.
30+
31+
0 + 1 + 3 + 2 = 6
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [5,1,6]
36+
37+
**Output:** 28
38+
39+
**Explanation:** The 8 subsets of [5,1,6] are:
40+
41+
- The empty subset has an XOR total of 0.
42+
43+
- [5] has an XOR total of 5.
44+
45+
- [1] has an XOR total of 1.
46+
47+
- [6] has an XOR total of 6.
48+
49+
- [5,1] has an XOR total of 5 XOR 1 = 4.
50+
51+
- [5,6] has an XOR total of 5 XOR 6 = 3.
52+
53+
- [1,6] has an XOR total of 1 XOR 6 = 7.
54+
55+
- [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.
56+
57+
0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28
58+
59+
**Example 3:**
60+
61+
**Input:** nums = [3,4,5,6,7,8]
62+
63+
**Output:** 480
64+
65+
**Explanation:** The sum of all XOR totals for every subset is 480.
66+
67+
**Constraints:**
68+
69+
* `1 <= nums.length <= 12`
70+
* `1 <= nums[i] <= 20`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1801_1900.s1864_minimum_number_of_swaps_to_make_the_binary_string_alternating;
2+
3+
// #Medium #String #Greedy #2022_05_10_Time_3_ms_(43.20%)_Space_41.6_MB_(72.00%)
4+
5+
public class Solution {
6+
public int minSwaps(String s) {
7+
int[][] count = new int[2][2];
8+
9+
for (int i = 0; i < s.length(); i++) {
10+
char c = s.charAt(i);
11+
if (i % 2 == 0) {
12+
count[0][c - '0']++;
13+
} else {
14+
count[1][c - '0']++;
15+
}
16+
}
17+
18+
if ((count[0][0] == 0 && count[1][1] == 0) || (count[0][1] == 0 && count[1][0] == 0)) {
19+
return 0;
20+
}
21+
22+
if (count[0][0] != count[1][1] && count[0][1] != count[1][0]) {
23+
return -1;
24+
}
25+
26+
int ans1 = count[0][0] == count[1][1] ? count[0][0] : Integer.MAX_VALUE;
27+
int ans2 = count[0][1] == count[1][0] ? count[0][1] : Integer.MAX_VALUE;
28+
29+
return Math.min(ans1, ans2);
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1864\. Minimum Number of Swaps to Make the Binary String Alternating
2+
3+
Medium
4+
5+
Given a binary string `s`, return _the **minimum** number of character swaps to make it **alternating**, or_ `-1` _if it is impossible._
6+
7+
The string is called **alternating** if no two adjacent characters are equal. For example, the strings `"010"` and `"1010"` are alternating, while the string `"0100"` is not.
8+
9+
Any two characters may be swapped, even if they are **not adjacent**.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "111000"
14+
15+
**Output:** 1
16+
17+
**Explanation:** Swap positions 1 and 4: "111000" -> "101010" The string is now alternating.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "010"
22+
23+
**Output:** 0
24+
25+
**Explanation:** The string is already alternating, no swaps are needed.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "1110"
30+
31+
**Output:** -1
32+
33+
**Constraints:**
34+
35+
* `1 <= s.length <= 1000`
36+
* `s[i]` is either `'0'` or `'1'`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g1801_1900.s1865_finding_pairs_with_a_certain_sum;
2+
3+
// #Medium #Array #Hash_Table #Design #2022_05_10_Time_195_ms_(83.97%)_Space_74.2_MB_(92.11%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class FindSumPairs {
9+
10+
Map<Integer, Integer> numFreq = new HashMap<>();
11+
int[] nums1;
12+
int[] nums2;
13+
14+
public FindSumPairs(int[] nums1, int[] nums2) {
15+
this.nums1 = nums1;
16+
this.nums2 = nums2;
17+
for (int num : nums2) {
18+
numFreq.put(num, numFreq.getOrDefault(num, 0) + 1);
19+
}
20+
}
21+
22+
public void add(int index, int val) {
23+
numFreq.put(nums2[index], numFreq.getOrDefault(nums2[index], 0) - 1);
24+
nums2[index] += val;
25+
numFreq.put(nums2[index], numFreq.getOrDefault(nums2[index], 0) + 1);
26+
}
27+
28+
public int count(int tot) {
29+
int res = 0;
30+
for (int num : nums1) {
31+
res += numFreq.getOrDefault(tot - num, 0);
32+
}
33+
34+
return res;
35+
}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
1865\. Finding Pairs With a Certain Sum
2+
3+
Medium
4+
5+
You are given two integer arrays `nums1` and `nums2`. You are tasked to implement a data structure that supports queries of two types:
6+
7+
1. **Add** a positive integer to an element of a given index in the array `nums2`.
8+
2. **Count** the number of pairs `(i, j)` such that `nums1[i] + nums2[j]` equals a given value (`0 <= i < nums1.length` and `0 <= j < nums2.length`).
9+
10+
Implement the `FindSumPairs` class:
11+
12+
* `FindSumPairs(int[] nums1, int[] nums2)` Initializes the `FindSumPairs` object with two integer arrays `nums1` and `nums2`.
13+
* `void add(int index, int val)` Adds `val` to `nums2[index]`, i.e., apply `nums2[index] += val`.
14+
* `int count(int tot)` Returns the number of pairs `(i, j)` such that `nums1[i] + nums2[j] == tot`.
15+
16+
**Example 1:**
17+
18+
**Input** ["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"] [[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]
19+
20+
**Output:** [null, 8, null, 2, 1, null, null, 11]
21+
22+
**Explanation:**
23+
24+
FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
25+
26+
findSumPairs.count(7); // return 8; pairs (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) make 2 + 5 and pairs (5,1), (5,5) make 3 + 4
27+
28+
findSumPairs.add(3, 2); // now nums2 = [1,4,5,**4**`,5,4`]
29+
30+
findSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5
31+
32+
findSumPairs.count(4); // return 1; pair (5,0) makes 3 + 1
33+
34+
findSumPairs.add(0, 1); // now nums2 = [**`2`**,4,5,4`,5,4`]
35+
36+
findSumPairs.add(1, 1); // now nums2 = [`2`,**5**,5,4`,5,4`]
37+
38+
findSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) make 2 + 5 and pairs (5,3), (5,5) make 3 + 4
39+
40+
**Constraints:**
41+
42+
* `1 <= nums1.length <= 1000`
43+
* <code>1 <= nums2.length <= 10<sup>5</sup></code>
44+
* <code>1 <= nums1[i] <= 10<sup>9</sup></code>
45+
* <code>1 <= nums2[i] <= 10<sup>5</sup></code>
46+
* `0 <= index < nums2.length`
47+
* <code>1 <= val <= 10<sup>5</sup></code>
48+
* <code>1 <= tot <= 10<sup>9</sup></code>
49+
* At most `1000` calls are made to `add` and `count` **each**.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g1801_1900.s1866_number_of_ways_to_rearrange_sticks_with_k_sticks_visible;
2+
3+
// #Hard #Dynamic_Programming #Math #Combinatorics
4+
// #2022_05_10_Time_67_ms_(96.33%)_Space_55.1_MB_(80.73%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
private static final int MOD = 1_000_000_007;
10+
11+
public int rearrangeSticks(int n, int k) {
12+
if (k > n || k < 1) {
13+
return 0;
14+
}
15+
if (k == n) {
16+
return 1;
17+
}
18+
19+
long[] dp = new long[k + 1];
20+
Arrays.fill(dp, 1);
21+
22+
for (int i = 1; i + k <= n; i++) {
23+
long[] dp2 = new long[k + 1];
24+
for (int j = 1; j <= k; j++) {
25+
dp2[j] = (dp2[j - 1] + (i + j - 1) * dp[j]) % MOD;
26+
}
27+
28+
dp = dp2;
29+
}
30+
31+
return (int) dp[k];
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1866\. Number of Ways to Rearrange Sticks With K Sticks Visible
2+
3+
Hard
4+
5+
There are `n` uniquely-sized sticks whose lengths are integers from `1` to `n`. You want to arrange the sticks such that **exactly** `k` sticks are **visible** from the left. A stick is **visible** from the left if there are no **longer** sticks to the **left** of it.
6+
7+
* For example, if the sticks are arranged `[1,3,2,5,4]`, then the sticks with lengths `1`, `3`, and `5` are visible from the left.
8+
9+
Given `n` and `k`, return _the **number** of such arrangements_. Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 3, k = 2
14+
15+
**Output:** 3
16+
17+
**Explanation:** [1,3,2], [2,3,1], and [2,1,3] are the only arrangements such that exactly 2 sticks are visible. The visible sticks are underlined.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 5, k = 5
22+
23+
**Output:** 1
24+
25+
**Explanation:** [1,2,3,4,5] is the only arrangement such that all 5 sticks are visible. The visible sticks are underlined.
26+
27+
**Example 3:**
28+
29+
**Input:** n = 20, k = 11
30+
31+
**Output:** 647427950
32+
33+
**Explanation:** There are 647427950 (mod 10<sup>9</sup> \+ 7) ways to rearrange the sticks such that exactly 11 sticks are visible.
34+
35+
**Constraints:**
36+
37+
* `1 <= n <= 1000`
38+
* `1 <= k <= n`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g1801_1900.s1869_longer_contiguous_segments_of_ones_than_zeros;
2+
3+
// #Easy #String #2022_05_10_Time_1_ms_(88.10%)_Space_41.8_MB_(66.14%)
4+
5+
public class Solution {
6+
public boolean checkZeroOnes(String s) {
7+
int zeroes = 0;
8+
int ones = 0;
9+
int i = 0;
10+
while (i < s.length()) {
11+
int start = i;
12+
while (i < s.length() && s.charAt(i) == '0') {
13+
i++;
14+
}
15+
if (i > start) {
16+
zeroes = Math.max(zeroes, i - start);
17+
}
18+
start = i;
19+
while (i < s.length() && s.charAt(i) == '1') {
20+
i++;
21+
}
22+
if (i > start) {
23+
ones = Math.max(ones, i - start);
24+
}
25+
}
26+
return ones > zeroes;
27+
}
28+
}

0 commit comments

Comments
 (0)