diff --git a/README.md b/README.md index 596bf2f..0c36254 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 500. Keyboard Row | [Link](https://leetcode.com/problems/keyboard-row/) | [Link](./lib/easy/500_keyboard_row.dart) | | 3280. Convert Date to Binary | [Link](https://leetcode.com/problems/convert-date-to-binary/) | [Link](./lib/easy/3280_convert_date_to_binary.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/3536_maximum_product_of_two_digits.dart b/lib/easy/3536_maximum_product_of_two_digits.dart new file mode 100644 index 0000000..736840e --- /dev/null +++ b/lib/easy/3536_maximum_product_of_two_digits.dart @@ -0,0 +1,15 @@ +class Solution { + int maxProduct(int n) { + final digits = []; + + while (n != 0) { + digits.add(n % 10); + + n = n ~/ 10; + } + + digits.sort(); + + return digits[digits.length - 1] * digits[digits.length - 2]; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 2d391ad..e7c29b0 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.5 +version: 1.0.6 homepage: https://github.com/fartem/leetcode-dart environment: diff --git a/test/easy/3536_maximum_product_of_two_digits_test.dart b/test/easy/3536_maximum_product_of_two_digits_test.dart new file mode 100644 index 0000000..9115916 --- /dev/null +++ b/test/easy/3536_maximum_product_of_two_digits_test.dart @@ -0,0 +1,33 @@ +import 'package:leetcode_dart/easy/3536_maximum_product_of_two_digits.dart'; +import 'package:test/test.dart'; + +void main() { + group( + 'Example tests', + () { + final solution = Solution(); + + test( + '3', + () => expect( + 3, + solution.maxProduct(31), + ), + ); + test( + '4', + () => expect( + 4, + solution.maxProduct(22), + ), + ); + test( + '8', + () => expect( + 8, + solution.maxProduct(124), + ), + ); + }, + ); +}