Skip to content

Commit cf9339d

Browse files
committed
weekly-241
Signed-off-by: ashKIK <[email protected]>
1 parent d9a69c5 commit cf9339d

5 files changed

+135
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Default ignored files
2+
/shelf/
3+
/.idea/
4+
# Datasource local storage ignored files
5+
/dataSources/
6+
/dataSources.local.xml
7+
# Editor-based HTTP Client requests
8+
/httpRequests/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
// https://leetcode.com/contest/weekly-contest-241/problems/finding-pairs-with-a-certain-sum/
5+
6+
public class FindingPairsWithACertainSum {
7+
8+
class FindSumPairs {
9+
10+
private int[] nums1;
11+
private int[] nums2;
12+
private Map<Integer, Integer> map;
13+
14+
public FindSumPairs(int[] nums1, int[] nums2) {
15+
this.map = new HashMap<>();
16+
this.nums1 = nums1;
17+
this.nums2 = nums2;
18+
for (int num2 : nums2) {
19+
map.merge(num2, 1, Integer::sum);
20+
}
21+
}
22+
23+
public void add(int index, int val) {
24+
map.merge(nums2[index], -1, Integer::sum);
25+
nums2[index] += val;
26+
map.merge(nums2[index], 1, Integer::sum);
27+
}
28+
29+
public int count(int tot) {
30+
int count = 0;
31+
for (int num : nums1) {
32+
count += map.getOrDefault(tot - num, 0);
33+
}
34+
return count;
35+
}
36+
}
37+
38+
/**
39+
* Your FindSumPairs object will be instantiated and called as such:
40+
* FindSumPairs obj = new FindSumPairs(nums1, nums2);
41+
* obj.add(index,val);
42+
* int param_2 = obj.count(tot);
43+
*/
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-241/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating/
3+
4+
public class MinimumNumberOfSwapsToMakeTheBinaryStringAlternating {
5+
6+
public int minSwaps(String s) {
7+
int numOnes = 0;
8+
int numZeros = 0;
9+
int countOnes = 0;
10+
int countZeros = 0;
11+
12+
// two patters; either 10101 or 01010
13+
char one = '1';
14+
char zero = '0';
15+
16+
for (int i = 0; i < s.length(); i++) {
17+
char c = s.charAt(i);
18+
if (c == '1') {
19+
numOnes++;
20+
}
21+
if (c == '0') {
22+
numZeros++;
23+
}
24+
if (c != one) {
25+
countOnes++;
26+
}
27+
if (c != zero) {
28+
countZeros++;
29+
}
30+
// alternate the 10101 or 01010 pattern
31+
one = one == '1' ? '0' : '1';
32+
zero = zero == '1' ? '0' : '1';
33+
}
34+
if (Math.abs(numOnes - numZeros) > 1) {
35+
return -1;
36+
}
37+
// if the difference is odd, then impossible to make 10101 pattern, make 01010 instead
38+
// vice versa
39+
if (countOnes % 2 == 1) {
40+
return countZeros / 2;
41+
}
42+
if (countZeros % 2 == 1) {
43+
return countOnes / 2;
44+
}
45+
// if able to alter to both patters then return minimum
46+
return Math.min(countOnes / 2, countZeros / 2);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-241/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible/
3+
4+
public class NumberOfWaysToRearrangeSticksWithKSticksVisible {
5+
6+
private final int[][] dp = new int[1001][1001];
7+
private final int MOD = 1000000007;
8+
9+
public int rearrangeSticks(int n, int k) {
10+
if (n == k) {
11+
return 1;
12+
}
13+
if (k == 0) {
14+
return 0;
15+
}
16+
if (dp[n][k] == 0) {
17+
dp[n][k] = (int) (((long) rearrangeSticks(n - 1, k - 1)
18+
+ (long) rearrangeSticks(n - 1, k) * (n - 1)) % MOD);
19+
}
20+
return dp[n][k];
21+
}
22+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-241/problems/sum-of-all-subset-xor-totals/
3+
4+
public class SumOfAllSubsetXORTotals {
5+
6+
public int subsetXORSum(int[] nums) {
7+
int res = 0;
8+
for (int num : nums) {
9+
res |= num;
10+
}
11+
return res * (1 << nums.length - 1);
12+
}
13+
}

0 commit comments

Comments
 (0)