Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76b22e0

Browse files
feat: add 1531,1879,1897
1 parent 60815fd commit 76b22e0

File tree

3 files changed

+262
-0
lines changed

3 files changed

+262
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# 1531. String Compression II
2+
3+
- Difficulty: Hard.
4+
- Related Topics: String, Dynamic Programming.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string `"aabccc"` we replace `"aa"` by `"a2"` and replace `"ccc"` by `"c3"`. Thus the compressed string becomes `"a2bc3"`.
10+
11+
Notice that in this problem, we are not adding `'1'` after single characters.
12+
13+
Given a string `s` and an integer `k`. You need to delete **at most** `k` characters from `s` such that the run-length encoded version of `s` has minimum length.
14+
15+
Find the **minimum length of the run-length encoded version of **`s`** after deleting at most **`k`** characters**.
16+
17+
18+
Example 1:
19+
20+
```
21+
Input: s = "aaabcccd", k = 2
22+
Output: 4
23+
Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.
24+
```
25+
26+
Example 2:
27+
28+
```
29+
Input: s = "aabbaa", k = 2
30+
Output: 2
31+
Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.
32+
```
33+
34+
Example 3:
35+
36+
```
37+
Input: s = "aaaaaaaaaaa", k = 0
38+
Output: 3
39+
Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.
40+
```
41+
42+
43+
**Constraints:**
44+
45+
46+
47+
- `1 <= s.length <= 100`
48+
49+
- `0 <= k <= s.length`
50+
51+
- `s` contains only lowercase English letters.
52+
53+
54+
55+
## Solution
56+
57+
```javascript
58+
/**
59+
* @param {string} s
60+
* @param {number} k
61+
* @return {number}
62+
*/
63+
var getLengthOfOptimalCompression = function(s, k) {
64+
var cache = {};
65+
var helper = function(i, k, prev, prevCount) {
66+
if (k < 0) return Number.MAX_SAFE_INTEGER;
67+
if (i === s.length - k) return 0;
68+
69+
var cacheKey = `${i}-${k}-${prev}-${prevCount}`;
70+
if (cache[cacheKey] !== undefined) return cache[cacheKey];
71+
72+
var res = 0;
73+
if (s[i] === prev) {
74+
// keep
75+
var diff = [1, 9, 99].includes(prevCount) ? 1 : 0;
76+
res = diff + helper(i + 1, k, prev, prevCount + 1);
77+
} else {
78+
res = Math.min(
79+
// delete
80+
helper(i + 1, k - 1, prev, prevCount),
81+
// keep
82+
1 + helper(i + 1, k, s[i], 1),
83+
);
84+
}
85+
cache[cacheKey] = res;
86+
return res;
87+
};
88+
return helper(0, k, '', 0);
89+
};
90+
```
91+
92+
**Explain:**
93+
94+
nope.
95+
96+
**Complexity:**
97+
98+
* Time complexity : O(n * k * n).
99+
* Space complexity : O(n * k * n).
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 1879. Minimum XOR Sum of Two Arrays
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Dynamic Programming, Bit Manipulation, Bitmask.
5+
- Similar Questions: Fair Distribution of Cookies, Choose Numbers From Two Arrays in Range, Maximum AND Sum of Array.
6+
7+
## Problem
8+
9+
You are given two integer arrays `nums1` and `nums2` of length `n`.
10+
11+
The **XOR sum** of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])` (**0-indexed**).
12+
13+
14+
15+
- For example, the **XOR sum** of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4`.
16+
17+
18+
Rearrange the elements of `nums2` such that the resulting **XOR sum** is **minimized**.
19+
20+
Return **the **XOR sum** after the rearrangement**.
21+
22+
23+
Example 1:
24+
25+
```
26+
Input: nums1 = [1,2], nums2 = [2,3]
27+
Output: 2
28+
Explanation: Rearrange nums2 so that it becomes [3,2].
29+
The XOR sum is (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2.
30+
```
31+
32+
Example 2:
33+
34+
```
35+
Input: nums1 = [1,0,3], nums2 = [5,3,4]
36+
Output: 8
37+
Explanation: Rearrange nums2 so that it becomes [5,4,3].
38+
The XOR sum is (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8.
39+
```
40+
41+
42+
**Constraints:**
43+
44+
45+
46+
- `n == nums1.length`
47+
48+
- `n == nums2.length`
49+
50+
- `1 <= n <= 14`
51+
52+
- `0 <= nums1[i], nums2[i] <= 107`
53+
54+
55+
56+
## Solution
57+
58+
```javascript
59+
/**
60+
* @param {number[]} nums1
61+
* @param {number[]} nums2
62+
* @return {number}
63+
*/
64+
var minimumXORSum = function(nums1, nums2) {
65+
return helper(nums1, nums2, 0, 0, {});
66+
};
67+
68+
var helper = function(nums1, nums2, i, bitmask, dp) {
69+
if (i === nums1.length) return 0;
70+
var key = `${i}-${bitmask}`;
71+
if (dp[key] !== undefined) return dp[key];
72+
var min = Number.MAX_SAFE_INTEGER;
73+
for (var j = 0; j < nums2.length; j++) {
74+
var mask = 1 << j;
75+
if (bitmask & mask) continue;
76+
min = Math.min(min, (nums1[i] ^ nums2[j]) + helper(nums1, nums2, i + 1, bitmask | mask, dp));
77+
}
78+
dp[key] = min;
79+
return min;
80+
};
81+
```
82+
83+
**Explain:**
84+
85+
nope.
86+
87+
**Complexity:**
88+
89+
* Time complexity : O(n * 2^n).
90+
* Space complexity : O(n * 2^n).
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 1897. Redistribute Characters to Make All Strings Equal
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Hash Table, String, Counting.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given an array of strings `words` (**0-indexed**).
10+
11+
In one operation, pick two **distinct** indices `i` and `j`, where `words[i]` is a non-empty string, and move **any** character from `words[i]` to **any** position in `words[j]`.
12+
13+
Return `true` **if you can make** every** string in **`words`** **equal **using **any** number of operations**,** and **`false` **otherwise**.
14+
15+
16+
Example 1:
17+
18+
```
19+
Input: words = ["abc","aabc","bc"]
20+
Output: true
21+
Explanation: Move the first 'a' in words[1] to the front of words[2],
22+
to make words[1] = "abc" and words[2] = "abc".
23+
All the strings are now equal to "abc", so return true.
24+
```
25+
26+
Example 2:
27+
28+
```
29+
Input: words = ["ab","a"]
30+
Output: false
31+
Explanation: It is impossible to make all the strings equal using the operation.
32+
```
33+
34+
35+
**Constraints:**
36+
37+
38+
39+
- `1 <= words.length <= 100`
40+
41+
- `1 <= words[i].length <= 100`
42+
43+
- `words[i]` consists of lowercase English letters.
44+
45+
46+
47+
## Solution
48+
49+
```javascript
50+
/**
51+
* @param {string[]} words
52+
* @return {boolean}
53+
*/
54+
var makeEqual = function(words) {
55+
var map = Array(26).fill(0);
56+
var a = 'a'.charCodeAt(0);
57+
for (var i = 0; i < words.length; i++) {
58+
for (var j = 0; j < words[i].length; j++) {
59+
map[words[i][j].charCodeAt(0) - a]++;
60+
}
61+
}
62+
return map.every(item => item % words.length === 0);
63+
};
64+
```
65+
66+
**Explain:**
67+
68+
nope.
69+
70+
**Complexity:**
71+
72+
* Time complexity : O(n * m).
73+
* Space complexity : O(1).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /