Skip to content

Commit 0508e6c

Browse files
committed
fix test
add daily test
1 parent 468b6ba commit 0508e6c

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
lines changed

.idea/misc.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/// https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values
2+
/// You are given two 2D integer arrays nums1 and nums2.
3+
///
4+
/// nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.
5+
/// nums2[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.
6+
/// Each array contains unique ids and is sorted in ascending order by id.
7+
///
8+
/// Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:
9+
///
10+
/// Only ids that appear in at least one of the two arrays should be included in the resulting array.
11+
/// Each id should be included only once and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays, then assume its value in that array to be 0.
12+
/// Return the resulting array. The returned array must be sorted in ascending order by id.
13+
///
14+
///
15+
///
16+
/// Example 1:
17+
///
18+
/// Input: nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]
19+
/// Output: [[1,6],[2,3],[3,2],[4,6]]
20+
/// Explanation: The resulting array contains the following:
21+
/// - id = 1, the value of this id is 2 + 4 = 6.
22+
/// - id = 2, the value of this id is 3.
23+
/// - id = 3, the value of this id is 2.
24+
/// - id = 4, the value of this id is 5 + 1 = 6.
25+
/// Example 2:
26+
///
27+
/// Input: nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]
28+
/// Output: [[1,3],[2,4],[3,6],[4,3],[5,5]]
29+
/// Explanation: There are no common ids, so we just include each id with its value in the resulting list.
30+
///
31+
///
32+
/// Constraints:
33+
///
34+
/// 1 <= nums1.length, nums2.length <= 200
35+
/// nums1[i].length == nums2[j].length == 2
36+
/// 1 <= idi, vali <= 1000
37+
/// Both arrays contain unique ids.
38+
/// Both arrays are in strictly ascending order by id.
39+
class Daily20250302 {
40+
List<List<int>> mergeArrays(List<List<int>> nums1, List<List<int>> nums2) {
41+
List<List<int>> result = [];
42+
int i = 0;
43+
int j = 0;
44+
while (i < nums1.length || j < nums2.length) {
45+
if (i >= nums1.length) {
46+
result.add(nums2[j++]);
47+
continue;
48+
}
49+
if (j >= nums2.length) {
50+
result.add(nums1[i++]);
51+
continue;
52+
}
53+
// if both ids are the same, sum both numbers
54+
if (nums1[i][0] == nums2[j][0]) {
55+
result.add([nums1[i][0], nums1[i][1] + nums2[j][1]]);
56+
i++;
57+
j++;
58+
} else if (nums1[i][0] < nums2[j][0]) {
59+
result.add(nums1[i]);
60+
i++;
61+
} else {
62+
result.add(nums2[j]);
63+
j++;
64+
}
65+
}
66+
return result;
67+
}
68+
}

test/leetcode_daily_test.dart

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:leetcode/exercises/add_two_numbers.dart';
2+
import 'package:leetcode/exercises/daily/2025/03/daily20250301.dart';
3+
import 'package:leetcode/exercises/daily/2025/03/daily20250302.dart';
4+
import 'package:leetcode/models/list_node.dart';
5+
import 'package:test/test.dart';
6+
7+
void main() {
8+
group("Daily Exercises", (){
9+
dailyExercises();
10+
});
11+
}
12+
13+
void dailyExercises() {
14+
test('Daily 2025-03-01', () {
15+
final daily = Daily20250301();
16+
expect(daily.applyOperations([1, 2, 2, 1, 1, 0]), [1, 4, 2, 0, 0, 0]);
17+
expect(daily.applyOperations([0, 1]), [1, 0]);
18+
});
19+
test('Daily 2025-03-02', (){
20+
final daily = Daily20250302();
21+
expect(daily.mergeArrays([[1,2],[2,3],[4,5]], [[1,4],[3,2],[4,1]]), [[1,6],[2,3],[3,2],[4,6]]);
22+
expect(daily.mergeArrays([[2,4],[3,6],[5,5]], [[1,3],[4,3]]), [[1,3],[2,4],[3,6],[4,3],[5,5]]);
23+
});
24+
}

test/leetcode_test.dart

-12
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
import 'package:leetcode/exercises/add_two_numbers.dart';
2-
import 'package:leetcode/exercises/daily/2025/03/daily20250301.dart';
32
import 'package:leetcode/models/list_node.dart';
43
import 'package:test/test.dart';
54

65
void main() {
7-
group("Daily Exercises", (){
8-
dailyExercises();
9-
});
106
group("Other Exercises", (){
117
exercise();
128
});
139
}
1410

15-
void dailyExercises() {
16-
test('Daily 2025-03-01', () {
17-
final daily = Daily20250301();
18-
expect(daily.applyOperations([1, 2, 2, 1, 1, 0]), [1, 4, 2, 0, 0, 0]);
19-
expect(daily.applyOperations([0, 0]), [1, 0]);
20-
});
21-
}
22-
2311
void exercise() {
2412
test("add two numbers", () {
2513
final addTwoNumbers = AddTwoNumbers();

0 commit comments

Comments
 (0)