From 9aede6eace0e8fc483825785b8c9f7616ff21235 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 13 Oct 2024 05:02:41 +0300 Subject: [PATCH] Improved dart tasks --- .../Solution.dart | 3 ++- .../s0004_median_of_two_sorted_arrays/Solution.dart | 6 ++++-- .../g0001_0100/s0008_string_to_integer_atoi/Solution.dart | 4 +--- .../s0011_container_with_most_water/Solution.dart | 6 ++---- src/main/dart/g0001_0100/s0045_jump_game_ii/Solution.dart | 7 ++----- .../s0121_best_time_to_buy_and_sell_stock/Solution.dart | 4 ++-- .../s0124_binary_tree_maximum_path_sum/Solution.dart | 2 +- 7 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/dart/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.dart b/src/main/dart/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.dart index c62823f..2b7da21 100644 --- a/src/main/dart/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.dart +++ b/src/main/dart/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.dart @@ -10,7 +10,8 @@ class Solution { int start = 0; for (int i = 0; i < s.length; i++) { - int cur = s.codeUnitAt(i); // Getting ASCII value of the character + // Getting ASCII value of the character + int cur = s.codeUnitAt(i); if (lastIndices[cur] < start) { lastIndices[cur] = i; curLen++; diff --git a/src/main/dart/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.dart b/src/main/dart/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.dart index 8a9f684..01063f0 100644 --- a/src/main/dart/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.dart +++ b/src/main/dart/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.dart @@ -15,8 +15,10 @@ class Solution { int low = 0; int high = n1; - int minValue = -pow(2, 31).toInt(); // substitute for Integer.MIN_VALUE - int maxValue = pow(2, 31).toInt() - 1; // substitute for Integer.MAX_VALUE + // substitute for Integer.MIN_VALUE + int minValue = -pow(2, 31).toInt(); + // substitute for Integer.MAX_VALUE + int maxValue = pow(2, 31).toInt() - 1; while (low <= high) { int cut1 = (low + high) ~/ 2; diff --git a/src/main/dart/g0001_0100/s0008_string_to_integer_atoi/Solution.dart b/src/main/dart/g0001_0100/s0008_string_to_integer_atoi/Solution.dart index 9922866..92790b3 100644 --- a/src/main/dart/g0001_0100/s0008_string_to_integer_atoi/Solution.dart +++ b/src/main/dart/g0001_0100/s0008_string_to_integer_atoi/Solution.dart @@ -31,16 +31,14 @@ class Solution { // read digits while (current < s.length && digits.containsKey(s[current])) { int digit = digits[s[current++]]!; - // check owerflow + // check overflow if (sign == -1 && res < (MIN + digit) / 10) { return MIN; } else if (res > (MAX - digit) / 10) { return MAX; } - res = res * 10 + sign * digit; } - return res; } } diff --git a/src/main/dart/g0001_0100/s0011_container_with_most_water/Solution.dart b/src/main/dart/g0001_0100/s0011_container_with_most_water/Solution.dart index f287757..1ee8fba 100644 --- a/src/main/dart/g0001_0100/s0011_container_with_most_water/Solution.dart +++ b/src/main/dart/g0001_0100/s0011_container_with_most_water/Solution.dart @@ -2,6 +2,8 @@ // #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1) // #2024_09_30_Time_337_ms_(96.77%)_Space_172.5_MB_(64.52%) +import 'dart:math'; + class Solution { int maxArea(List height) { int maxArea = -1; @@ -20,8 +22,4 @@ class Solution { return maxArea; } - - int max(int a, int b) { - return (a > b) ? a : b; - } } diff --git a/src/main/dart/g0001_0100/s0045_jump_game_ii/Solution.dart b/src/main/dart/g0001_0100/s0045_jump_game_ii/Solution.dart index 400813b..3324b90 100644 --- a/src/main/dart/g0001_0100/s0045_jump_game_ii/Solution.dart +++ b/src/main/dart/g0001_0100/s0045_jump_game_ii/Solution.dart @@ -2,6 +2,8 @@ // #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_4 // #Big_O_Time_O(n)_Space_O(1) #2024_10_04_Time_335_ms_(81.58%)_Space_148.6_MB_(76.32%) +import 'dart:math'; + class Solution { int jump(List nums) { int length = 0; @@ -25,9 +27,4 @@ class Solution { return minJump; } - - // Dart's equivalent for Java's Math.max() is the built-in max() function - int max(int a, int b) { - return a > b ? a : b; - } } diff --git a/src/main/dart/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.dart b/src/main/dart/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.dart index f5db69a..9c2d9b5 100644 --- a/src/main/dart/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.dart +++ b/src/main/dart/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.dart @@ -2,6 +2,8 @@ // #Data_Structure_I_Day_3_Array #Dynamic_Programming_I_Day_7 #Level_1_Day_5_Greedy #Udemy_Arrays // #Big_O_Time_O(N)_Space_O(1) #2024_10_07_Time_374_ms_(89.33%)_Space_190.1_MB_(48.33%) +import 'dart:math'; + class Solution { int maxProfit(List prices) { int maxProfit = 0; @@ -17,6 +19,4 @@ class Solution { return maxProfit; } - - int max(int a, int b) => a > b ? a : b; // Utility function for max } diff --git a/src/main/dart/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.dart b/src/main/dart/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.dart index 1ceb6dd..3e57167 100644 --- a/src/main/dart/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.dart +++ b/src/main/dart/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.dart @@ -12,7 +12,7 @@ * } */ class Solution { - int _max = -999999999; // Use a large negative value instead of -double.infinity.toInt() + int _max = -999999999; int _helper(TreeNode? root) { if (root == null) {