Skip to content

Commit 4ba35ee

Browse files
committed
May-19
1 parent 595e34b commit 4ba35ee

10 files changed

+534
-28
lines changed

README.md

Lines changed: 15 additions & 8 deletions
Large diffs are not rendered by default.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Tag: Dynamic Programming
2+
// Time: O(N * 48 ^ 2)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/mVdxVHIq4hQ
7+
8+
// You are given two integers m and n. Consider an m x n grid where each cell is initially white. You can paint each cell red, green, or blue. All cells must be painted.
9+
// Return the number of ways to color the grid with no two adjacent cells having the same color. Since the answer can be very large, return it modulo 109 + 7.
10+
//  
11+
// Example 1:
12+
//
13+
//
14+
// Input: m = 1, n = 1
15+
// Output: 3
16+
// Explanation: The three possible colorings are shown in the image above.
17+
//
18+
// Example 2:
19+
//
20+
//
21+
// Input: m = 1, n = 2
22+
// Output: 6
23+
// Explanation: The six possible colorings are shown in the image above.
24+
//
25+
// Example 3:
26+
//
27+
// Input: m = 5, n = 5
28+
// Output: 580986
29+
//
30+
//  
31+
// Constraints:
32+
//
33+
// 1 <= m <= 5
34+
// 1 <= n <= 1000
35+
//
36+
//
37+
38+
class Solution {
39+
public:
40+
int dp[1001][1024] = {};
41+
int mod = 1e9 + 7;
42+
int colorTheGrid(int m, int n) {
43+
return dfs(m, n, 0, 0, 0, 0);
44+
}
45+
46+
int dfs(int m, int n, int i, int j, int cur, int pre) {
47+
if (i == m) {
48+
return dfs(m, n, 0, j + 1, 0, cur);
49+
}
50+
51+
if (j == n) {
52+
return 1;
53+
}
54+
55+
if (i == 0 && dp[j][pre] > 0) {
56+
return dp[j][pre];
57+
}
58+
59+
int up = i == 0 ? 0 : (cur >> (i - 1) * 2) & 3;
60+
int left = (pre >> i * 2) & 3;
61+
int res = 0;
62+
for (int k = 1; k <=3; k++) {
63+
if (k != up && k != left) {
64+
res = (res * 1LL + dfs(m, n, i + 1, j, k << i * 2 | cur, pre)) % mod;
65+
}
66+
}
67+
68+
if (i == 0) {
69+
dp[j][pre] = res;
70+
}
71+
72+
return res;
73+
}
74+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Tag: Dynamic Programming
2+
# Time: O(N * 48 ^ 2)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/mVdxVHIq4hQ
7+
8+
# You are given two integers m and n. Consider an m x n grid where each cell is initially white. You can paint each cell red, green, or blue. All cells must be painted.
9+
# Return the number of ways to color the grid with no two adjacent cells having the same color. Since the answer can be very large, return it modulo 109 + 7.
10+
#  
11+
# Example 1:
12+
#
13+
#
14+
# Input: m = 1, n = 1
15+
# Output: 3
16+
# Explanation: The three possible colorings are shown in the image above.
17+
#
18+
# Example 2:
19+
#
20+
#
21+
# Input: m = 1, n = 2
22+
# Output: 6
23+
# Explanation: The six possible colorings are shown in the image above.
24+
#
25+
# Example 3:
26+
#
27+
# Input: m = 5, n = 5
28+
# Output: 580986
29+
#
30+
#  
31+
# Constraints:
32+
#
33+
# 1 <= m <= 5
34+
# 1 <= n <= 1000
35+
#
36+
#
37+
38+
class Solution:
39+
def colorTheGrid(self, m: int, n: int) -> int:
40+
dp = [[0] * 1024 for i in range(n + 1)]
41+
mod = 10 ** 9 + 7
42+
43+
def dfs(i: int, j: int, cur: int, pre: int) -> int:
44+
if i == m:
45+
return dfs(0, j + 1, 0, cur)
46+
47+
if j == n:
48+
return 1
49+
50+
if (i == 0 and dp[j][pre] > 0):
51+
return dp[j][pre]
52+
53+
res = 0
54+
up = 0 if i == 0 else (cur >> ((i - 1) * 2)) & 3
55+
left = (pre >> (i * 2)) & 3
56+
for k in range(1, 4):
57+
if k != up and k != left:
58+
res += dfs(i + 1, j, k << (i * 2) | cur, pre)
59+
res %= mod
60+
61+
if i == 0:
62+
dp[j][pre] = res
63+
64+
return res
65+
66+
return dfs(0, 0, 0, 0)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Tag: Array, Hash Table, Two Pointers, Sorting, Enumeration
2+
// Time: O(N^2)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Alice had a 0-indexed array arr consisting of n positive integers. She chose an arbitrary positive integer k and created two new 0-indexed integer arrays lower and higher in the following manner:
8+
//
9+
// lower[i] = arr[i] - k, for every index i where 0 <= i < n
10+
// higher[i] = arr[i] + k, for every index i where 0 <= i < n
11+
//
12+
// Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays lower and higher, but not the array each integer belonged to. Help Alice and recover the original array.
13+
// Given an array nums consisting of 2n integers, where exactly n of the integers were present in lower and the remaining in higher, return the original array arr. In case the answer is not unique, return any valid array.
14+
// Note: The test cases are generated such that there exists at least one valid array arr.
15+
//  
16+
// Example 1:
17+
//
18+
// Input: nums = [2,10,6,4,8,12]
19+
// Output: [3,7,11]
20+
// Explanation:
21+
// If arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12].
22+
// Combining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums.
23+
// Another valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12].
24+
//
25+
// Example 2:
26+
//
27+
// Input: nums = [1,1,3,3]
28+
// Output: [2,2]
29+
// Explanation:
30+
// If arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3].
31+
// Combining lower and higher gives us [1,1,3,3], which is equal to nums.
32+
// Note that arr cannot be [1,3] because in that case, the only possible way to obtain [1,1,3,3] is with k = 0.
33+
// This is invalid since k must be positive.
34+
//
35+
// Example 3:
36+
//
37+
// Input: nums = [5,435]
38+
// Output: [220]
39+
// Explanation:
40+
// The only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435].
41+
//
42+
//  
43+
// Constraints:
44+
//
45+
// 2 * n == nums.length
46+
// 1 <= n <= 1000
47+
// 1 <= nums[i] <= 109
48+
// The test cases are generated such that there exists at least one valid array arr.
49+
//
50+
//
51+
52+
class Solution {
53+
public:
54+
vector<int> recoverArray(vector<int>& nums) {
55+
int n = nums.size();
56+
sort(nums.begin(), nums.end());
57+
vector<int> res(n / 2, 0);
58+
59+
for (int i = 1; i < n; i++) {
60+
int k = nums[i] - nums[0];
61+
if (k == 0 || k % 2 == 1) {
62+
continue;
63+
}
64+
65+
k = k / 2;
66+
int low = 0;
67+
int high = 0;
68+
res[low] = nums[0] + k;
69+
low += 1;
70+
71+
for (int j = 1; j < n; j++) {
72+
if (high < low && nums[j] - k == res[high]) {
73+
high += 1;
74+
} else if (low < res.size()) {
75+
res[low] = nums[j] + k;
76+
low += 1;
77+
} else {
78+
break;
79+
}
80+
}
81+
82+
if (low == res.size() && high == res.size()) {
83+
break;
84+
}
85+
}
86+
87+
return res;
88+
}
89+
};
90+
91+
class Solution {
92+
public:
93+
vector<int> recoverArray(vector<int>& nums) {
94+
int n = nums.size();
95+
sort(nums.begin(), nums.end());
96+
97+
for (int i = 1; i < n; i++) {
98+
int k = nums[i] - nums[0];
99+
if (k == 0 || k % 2 == 1) {
100+
continue;
101+
}
102+
103+
unordered_map<int, int> counter;
104+
for (auto &x: nums) {
105+
counter[x] += 1;
106+
}
107+
108+
vector<int> res;
109+
for (int j = 0; j < n; j++) {
110+
if (counter[nums[j]] == 0) {
111+
continue;
112+
}
113+
114+
counter[nums[j]] -= 1;
115+
int target = nums[j] + k;
116+
if (counter[target] == 0) {
117+
break;
118+
}
119+
120+
counter[target] -= 1;
121+
res.push_back(nums[j] + k / 2);
122+
}
123+
124+
if (res.size() == n / 2) {
125+
return res;
126+
}
127+
}
128+
129+
return vector<int>{};
130+
}
131+
};
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Tag: Array, Hash Table, Two Pointers, Sorting, Enumeration
2+
# Time: O(N^2)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# Alice had a 0-indexed array arr consisting of n positive integers. She chose an arbitrary positive integer k and created two new 0-indexed integer arrays lower and higher in the following manner:
8+
#
9+
# lower[i] = arr[i] - k, for every index i where 0 <= i < n
10+
# higher[i] = arr[i] + k, for every index i where 0 <= i < n
11+
#
12+
# Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays lower and higher, but not the array each integer belonged to. Help Alice and recover the original array.
13+
# Given an array nums consisting of 2n integers, where exactly n of the integers were present in lower and the remaining in higher, return the original array arr. In case the answer is not unique, return any valid array.
14+
# Note: The test cases are generated such that there exists at least one valid array arr.
15+
#  
16+
# Example 1:
17+
#
18+
# Input: nums = [2,10,6,4,8,12]
19+
# Output: [3,7,11]
20+
# Explanation:
21+
# If arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12].
22+
# Combining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums.
23+
# Another valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12].
24+
#
25+
# Example 2:
26+
#
27+
# Input: nums = [1,1,3,3]
28+
# Output: [2,2]
29+
# Explanation:
30+
# If arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3].
31+
# Combining lower and higher gives us [1,1,3,3], which is equal to nums.
32+
# Note that arr cannot be [1,3] because in that case, the only possible way to obtain [1,1,3,3] is with k = 0.
33+
# This is invalid since k must be positive.
34+
#
35+
# Example 3:
36+
#
37+
# Input: nums = [5,435]
38+
# Output: [220]
39+
# Explanation:
40+
# The only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435].
41+
#
42+
#  
43+
# Constraints:
44+
#
45+
# 2 * n == nums.length
46+
# 1 <= n <= 1000
47+
# 1 <= nums[i] <= 109
48+
# The test cases are generated such that there exists at least one valid array arr.
49+
#
50+
#
51+
52+
class Solution:
53+
def recoverArray(self, nums: List[int]) -> List[int]:
54+
n = len(nums)
55+
nums.sort()
56+
res = [0 for i in range(n // 2)]
57+
58+
for i in range(1, n):
59+
k = nums[i] - nums[0]
60+
if k == 0 or k % 2 == 1:
61+
continue
62+
63+
k = k // 2
64+
low = 0
65+
high = 0
66+
67+
res[low] = nums[0] + k
68+
low += 1
69+
70+
for j in range(1, n):
71+
if high < low and nums[j] - k == res[high]:
72+
high += 1
73+
elif low < len(res):
74+
res[low] = nums[j] + k
75+
low += 1
76+
else:
77+
break
78+
79+
if low == len(res) and high == len(res):
80+
break
81+
82+
return res
83+
84+
from collections import Counter
85+
class Solution:
86+
def recoverArray(self, nums: List[int]) -> List[int]:
87+
n = len(nums)
88+
nums.sort()
89+
90+
for i in range(1, n):
91+
k = nums[i] - nums[0]
92+
if k == 0 or k % 2 == 1:
93+
continue
94+
95+
res = []
96+
counter = Counter(nums)
97+
for x in nums:
98+
if counter[x] == 0:
99+
continue
100+
101+
if counter[x + k] == 0:
102+
break
103+
104+
counter[x] -= 1
105+
counter[x + k] -= 1
106+
107+
res.append(x + k // 2)
108+
109+
if len(res) == n // 2:
110+
return res
111+
112+
return []

0 commit comments

Comments
 (0)