diff --git a/README.md b/README.md index f22445d..f6367ba 100644 --- a/README.md +++ b/README.md @@ -43,5 +43,6 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 3120. Count the Number of Special Characters I | [Link](https://leetcode.com/problems/count-the-number-of-special-characters-i/) | [Link](./lib/easy/3120_count_the_number_of_special_characters_i.dart) | | 3280. Convert Date to Binary | [Link](https://leetcode.com/problems/convert-date-to-binary/) | [Link](./lib/easy/3280_convert_date_to_binary.dart) | | 3340. Check Balanced String | [Link](https://leetcode.com/problems/check-balanced-string/) | [Link](./lib/easy/3340_check_balanced_string.dart) | +| 3471. Find the Largest Almost Missing Integer | [Link](https://leetcode.com/problems/check-balanced-string/) | [Link](./lib/easy/3471_find_the_largest_almost_missing_integer.dart) | | 3516. Find Closest Person | [Link](https://leetcode.com/problems/find-closest-person/) | [Link](./lib/easy/3516_find_closest_person.dart) | | 3536. Maximum Product of Two Digits | [Link](https://leetcode.com/problems/maximum-product-of-two-digits/) | [Link](./lib/easy/3536_maximum_product_of_two_digits.dart) | diff --git a/lib/easy/3471_find_the_largest_almost_missing_integer.dart b/lib/easy/3471_find_the_largest_almost_missing_integer.dart new file mode 100644 index 0000000..c5cf6e3 --- /dev/null +++ b/lib/easy/3471_find_the_largest_almost_missing_integer.dart @@ -0,0 +1,27 @@ +class Solution { + int largestInteger(List nums, int k) { + if (nums.length == k) { + return nums.reduce((a, b) => a > b ? a : b); + } + + final count = {}; + + for (int i = 0; i <= nums.length - k; i++) { + final sub = nums.sublist(i, i + k); + + for (final num in sub) { + count[num] = (count[num] ?? 0) + 1; + } + } + + var result = -1; + + for (final entry in count.entries) { + if (entry.value == 1 && entry.key > result) { + result = entry.key; + } + } + + return result; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 6baffca..c03c4fe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: leetcode_dart description: Some solved problems from https://leetcode.com on Dart -version: 1.0.9 +version: 1.1.0 homepage: https://github.com/fartem/leetcode-dart environment: diff --git a/test/easy/3471_find_the_largest_almost_missing_integer_test.dart b/test/easy/3471_find_the_largest_almost_missing_integer_test.dart new file mode 100644 index 0000000..86f7f4f --- /dev/null +++ b/test/easy/3471_find_the_largest_almost_missing_integer_test.dart @@ -0,0 +1,42 @@ +import 'package:leetcode_dart/easy/3471_find_the_largest_almost_missing_integer.dart'; +import 'package:test/test.dart'; + +void main() { + group( + 'Example tests', + () { + final solution = Solution(); + + test( + 'Test case 1', + () => expect( + 7, + solution.largestInteger( + [3, 9, 2, 1, 7], + 3, + ), + ), + ); + test( + 'Test case 2', + () => expect( + 3, + solution.largestInteger( + [3, 9, 7, 2, 1, 7], + 4, + ), + ), + ); + test( + 'Test case 3', + () => expect( + -1, + solution.largestInteger( + [0, 0], + 1, + ), + ), + ); + }, + ); +}