Skip to content

Commit fa092b1

Browse files
committed
feat: updates mar 3 2025
1 parent ef41d6b commit fa092b1

28 files changed

+1299
-0
lines changed

.DS_Store

4 KB
Binary file not shown.

1028-recover-tree-preorder.dart

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* int val;
5+
* TreeNode? left;
6+
* TreeNode? right;
7+
* TreeNode([this.val = 0, this.left, this.right]);
8+
* }
9+
*/
10+
11+
class TreeNode {
12+
int val;
13+
TreeNode? left;
14+
TreeNode? right;
15+
TreeNode([this.val = 0, this.left, this.right]);
16+
}
17+
18+
class Solution {
19+
TreeNode? recoverFromPreorder(String traversal) {
20+
if (traversal.isEmpty) return null;
21+
22+
List<TreeNode> stack = [];
23+
int index = 0;
24+
25+
while (index < traversal.length) {
26+
int depth = 0;
27+
while (index < traversal.length && traversal[index] == '-') {
28+
depth++;
29+
index++;
30+
}
31+
32+
int value = 0;
33+
while (index < traversal.length && traversal[index] != '-') {
34+
value =
35+
value * 10 + (traversal[index].codeUnitAt(0) - '0'.codeUnitAt(0));
36+
index++;
37+
}
38+
39+
TreeNode currentNode = TreeNode(value);
40+
41+
if (depth == 0) {
42+
stack.add(currentNode);
43+
continue;
44+
}
45+
46+
while (stack.length > depth) {
47+
stack.removeLast();
48+
}
49+
50+
TreeNode parent = stack.last;
51+
if (parent.left == null) {
52+
parent.left = currentNode;
53+
} else {
54+
parent.right = currentNode;
55+
}
56+
57+
stack.add(currentNode);
58+
}
59+
60+
return stack.first;
61+
}
62+
}
63+
64+
void main() {
65+
final solution = Solution();
66+
67+
// Test Case 1
68+
TreeNode? result1 = solution.recoverFromPreorder("1-2--3--4-5--6--7");
69+
print(result1?.val); // 1
70+
71+
// Test Case 2
72+
TreeNode? result2 = solution.recoverFromPreorder("1-2--3---4-5--6---7");
73+
print(result2?.val); // 1
74+
75+
// Test Case 3
76+
TreeNode? result3 = solution.recoverFromPreorder("1-401--349---90--88");
77+
print(result3?.val); // 1
78+
}

1079-letter-tile-possibilities.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
int numTilePossibilities(String tiles) {
3+
Map<String, int> frequency = {};
4+
for (int i = 0; i < tiles.length; i++) {
5+
String char = tiles[i];
6+
frequency[char] = (frequency[char] ?? 0) + 1;
7+
}
8+
9+
int result = 0;
10+
11+
void backtrack() {
12+
for (var entry in frequency.entries) {
13+
String char = entry.key;
14+
int count = entry.value;
15+
16+
if (count > 0) {
17+
result++;
18+
frequency[char] = count - 1;
19+
20+
backtrack();
21+
22+
frequency[char] = count;
23+
}
24+
}
25+
}
26+
27+
backtrack();
28+
return result;
29+
}
30+
}
31+
32+
void main() {
33+
final solution = Solution();
34+
35+
// Test Case 1
36+
print(solution.numTilePossibilities("AAB")); // 8
37+
38+
// Test Case 2
39+
print(solution.numTilePossibilities("AAABBC")); // 188
40+
41+
// Test Case 3
42+
print(solution.numTilePossibilities("V")); // 1
43+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
String shortestCommonSupersequence(String str1, String str2) {
3+
int m = str1.length;
4+
int n = str2.length;
5+
6+
List<List<int>> dp = List.generate(m + 1, (_) => List.filled(n + 1, 0));
7+
8+
for (int i = 1; i <= m; i++) {
9+
for (int j = 1; j <= n; j++) {
10+
if (str1[i - 1] == str2[j - 1]) {
11+
dp[i][j] = dp[i - 1][j - 1] + 1;
12+
} else {
13+
dp[i][j] = dp[i - 1][j] > dp[i][j - 1] ? dp[i - 1][j] : dp[i][j - 1];
14+
}
15+
}
16+
}
17+
18+
int i = m, j = n;
19+
StringBuffer scs = StringBuffer();
20+
21+
while (i > 0 && j > 0) {
22+
if (str1[i - 1] == str2[j - 1]) {
23+
scs.write(str1[i - 1]);
24+
i--;
25+
j--;
26+
} else if (dp[i - 1][j] > dp[i][j - 1]) {
27+
scs.write(str1[i - 1]);
28+
i--;
29+
} else {
30+
scs.write(str2[j - 1]);
31+
j--;
32+
}
33+
}
34+
35+
while (i > 0) {
36+
scs.write(str1[i - 1]);
37+
i--;
38+
}
39+
while (j > 0) {
40+
scs.write(str2[j - 1]);
41+
j--;
42+
}
43+
44+
return scs.toString().split('').reversed.join();
45+
}
46+
}
47+
48+
void main() {
49+
final solution = Solution();
50+
51+
// Test Case 1
52+
print(solution.shortestCommonSupersequence("abac", "cab")); // "cabac"
53+
54+
// Test Case 2
55+
print(solution.shortestCommonSupersequence(
56+
"aaaaaaaa", "aaaaaaaa")); // "aaaaaaaa"
57+
58+
// Test Case 3
59+
print(solution.shortestCommonSupersequence("abc", "def")); // "abcdef"
60+
}

1261-elements-contaminated-tree.dart

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* int val;
5+
* TreeNode? left;
6+
* TreeNode? right;
7+
* TreeNode([this.val = 0, this.left, this.right]);
8+
* }
9+
*/
10+
11+
class TreeNode {
12+
int val;
13+
TreeNode? left;
14+
TreeNode? right;
15+
TreeNode([this.val = 0, this.left, this.right]);
16+
}
17+
18+
class FindElements {
19+
Set<int> _values = {};
20+
21+
FindElements(TreeNode? root) {
22+
_recoverTree(root, 0);
23+
}
24+
25+
void _recoverTree(TreeNode? node, int val) {
26+
if (node == null) return;
27+
28+
node.val = val;
29+
_values.add(val);
30+
31+
_recoverTree(node.left, 2 * val + 1);
32+
_recoverTree(node.right, 2 * val + 2);
33+
}
34+
35+
bool find(int target) {
36+
return _values.contains(target);
37+
}
38+
}
39+
40+
/**
41+
* Your FindElements object will be instantiated and called as such:
42+
* FindElements obj = FindElements(root);
43+
* bool param1 = obj.find(target);
44+
*/
45+
46+
void main() {
47+
// Test Case 1
48+
TreeNode root1 = TreeNode(-1, TreeNode(-1), TreeNode(-1));
49+
FindElements findElements1 = FindElements(root1);
50+
print(findElements1.find(1)); // true
51+
print(findElements1.find(2)); // true
52+
print(findElements1.find(3)); // false
53+
54+
// Test Case 2
55+
TreeNode root2 = TreeNode(-1, null, TreeNode(-1));
56+
FindElements findElements2 = FindElements(root2);
57+
print(findElements2.find(1)); // false
58+
print(findElements2.find(2)); // true
59+
60+
// Test Case 3
61+
TreeNode root3 = TreeNode(-1, TreeNode(-1, TreeNode(-1), null), TreeNode(-1));
62+
FindElements findElements3 = FindElements(root3);
63+
print(findElements3.find(1)); // true
64+
print(findElements3.find(3)); // true
65+
print(findElements3.find(5)); // false
66+
}

1352-product-last-k.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class ProductOfNumbers {
2+
List<int> prefixProducts;
3+
4+
ProductOfNumbers() : prefixProducts = [1];
5+
6+
void add(int num) {
7+
if (num == 0) {
8+
prefixProducts = [1];
9+
} else {
10+
prefixProducts.add(prefixProducts.last * num);
11+
}
12+
}
13+
14+
int getProduct(int k) {
15+
if (k >= prefixProducts.length) {
16+
return 0;
17+
}
18+
return prefixProducts[prefixProducts.length - 1] ~/
19+
prefixProducts[prefixProducts.length - 1 - k];
20+
}
21+
}
22+
23+
/**
24+
* Your ProductOfNumbers object will be instantiated and called as such:
25+
* ProductOfNumbers obj = ProductOfNumbers();
26+
* obj.add(num);
27+
* int param2 = obj.getProduct(k);
28+
*/
29+
30+
void main() {
31+
// Test Case 1
32+
final product1 = ProductOfNumbers();
33+
product1.add(3);
34+
product1.add(0);
35+
product1.add(2);
36+
product1.add(5);
37+
product1.add(4);
38+
print(product1.getProduct(2)); // 20
39+
print(product1.getProduct(3)); // 40
40+
print(product1.getProduct(4)); // 0
41+
42+
// Test Case 2
43+
final product2 = ProductOfNumbers();
44+
product2.add(1);
45+
product2.add(2);
46+
product2.add(3);
47+
print(product2.getProduct(1)); // 3
48+
print(product2.getProduct(2)); // 6
49+
print(product2.getProduct(3)); // 6
50+
51+
// Test Case 3
52+
final product3 = ProductOfNumbers();
53+
product3.add(5);
54+
product3.add(2);
55+
product3.add(0);
56+
product3.add(4);
57+
print(product3.getProduct(1)); // 4
58+
print(product3.getProduct(2)); // 0
59+
print(product3.getProduct(3)); // 0
60+
}

1415-kth-lexicographical-happy.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
String getHappyString(int n, int k) {
3+
String result = "";
4+
int count = 0;
5+
6+
void backtrack(String current) {
7+
if (current.length == n) {
8+
count++;
9+
if (count == k) {
10+
result = current;
11+
}
12+
return;
13+
}
14+
15+
for (String char in ['a', 'b', 'c']) {
16+
if (current.isEmpty || current[current.length - 1] != char) {
17+
backtrack(current + char);
18+
if (result.isNotEmpty) {
19+
return;
20+
}
21+
}
22+
}
23+
}
24+
25+
backtrack("");
26+
return result;
27+
}
28+
}
29+
30+
void main() {
31+
final solution = Solution();
32+
33+
// Test Case 1
34+
print(solution.getHappyString(1, 3)); // "c"
35+
36+
// Test Case 2
37+
print(solution.getHappyString(3, 9)); // "cab"
38+
39+
// Test Case 3
40+
print(solution.getHappyString(2, 7)); // ""
41+
}

1524-subarrays-odd-sum.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
int numOfSubarrays(List<int> arr) {
3+
const int MOD = 1000000007;
4+
int prefixSum = 0;
5+
int oddCount = 0;
6+
int evenCount = 1;
7+
int result = 0;
8+
9+
for (int num in arr) {
10+
prefixSum += num;
11+
if (prefixSum % 2 == 1) {
12+
result = (result + evenCount) % MOD;
13+
oddCount++;
14+
} else {
15+
result = (result + oddCount) % MOD;
16+
evenCount++;
17+
}
18+
}
19+
20+
return result;
21+
}
22+
}
23+
24+
void main() {
25+
final solution = Solution();
26+
27+
// Test Case 1
28+
print(solution.numOfSubarrays([1, 3, 5])); // 4
29+
30+
// Test Case 2
31+
print(solution.numOfSubarrays([2, 4, 6])); // 0
32+
33+
// Test Case 3
34+
print(solution.numOfSubarrays([1, 2, 3, 4, 5])); // 9
35+
}

0 commit comments

Comments
 (0)