diff --git a/README.md b/README.md index d6283eebf..67d9817be 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,6 @@ implementation 'com.github.javadev:leetcode-in-java:1.34' > ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews) ## -* [Level 1](#level-1) * [Level 2](#level-2) * [Udemy](#udemy) * [Data Structure I](#data-structure-i) @@ -48,113 +47,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.34' * [Programming Skills II](#programming-skills-ii) * [Graph Theory I](#graph-theory-i) * [SQL I](#sql-i) - -### Level 1 - -#### Day 1 Prefix Sum - -| | | | | | -|-|-|-|-|-|- -| 1480 |[Running Sum of 1d Array](src/main/java/g1401_1500/s1480_running_sum_of_1d_array/Solution.java)| Easy | Array, Prefix_Sum | 0 | 100.00 -| 0724 |[Find Pivot Index](src/main/java/g0701_0800/s0724_find_pivot_index/Solution.java)| Easy | Array, Prefix_Sum | 2 | 69.67 - -#### Day 2 String - -| | | | | | -|-|-|-|-|-|- -| 0205 |[Isomorphic Strings](src/main/java/g0201_0300/s0205_isomorphic_strings/Solution.java)| Easy | String, Hash_Table | 2 | 99.97 -| 0392 |[Is Subsequence](src/main/java/g0301_0400/s0392_is_subsequence/Solution.java)| Easy | String, Dynamic_Programming, Two_Pointers | 1 | 93.01 - -#### Day 3 Linked List - -| | | | | | -|-|-|-|-|-|- -| 0021 |[Merge Two Sorted Lists](src/main/java/g0001_0100/s0021_merge_two_sorted_lists/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00 -| 0206 |[Reverse Linked List](src/main/java/g0201_0300/s0206_reverse_linked_list/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00 - -#### Day 4 Linked List - -| | | | | | -|-|-|-|-|-|- -| 0876 |[Middle of the Linked List](src/main/java/g0801_0900/s0876_middle_of_the_linked_list/Solution.java)| Easy | Two_Pointers, Linked_List | 0 | 100.00 -| 0142 |[Linked List Cycle II](src/main/java/g0101_0200/s0142_linked_list_cycle_ii/Solution.java)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00 - -#### Day 5 Greedy - -| | | | | | -|-|-|-|-|-|- -| 0121 |[Best Time to Buy and Sell Stock](src/main/java/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 100.00 -| 0409 |[Longest Palindrome](src/main/java/g0401_0500/s0409_longest_palindrome/Solution.java)| Easy | String, Hash_Table, Greedy | 2 | 92.90 - -#### Day 6 Tree - -| | | | | | -|-|-|-|-|-|- -| 0589 |[N-ary Tree Preorder Traversal](src/main/java/g0501_0600/s0589_n_ary_tree_preorder_traversal/Solution.java)| Easy | Depth_First_Search, Tree, Stack | 1 | 90.98 -| 0102 |[Binary Tree Level Order Traversal](src/main/java/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 1 | 91.09 - -#### Day 7 Binary Search - -| | | | | | -|-|-|-|-|-|- -| 0704 |[Binary Search](src/main/java/g0701_0800/s0704_binary_search/Solution.java)| Easy | Top_100_Liked_Questions, Array, Binary_Search | 0 | 100.00 -| 0278 |[First Bad Version](src/main/java/g0201_0300/s0278_first_bad_version/Solution.java)| Easy | Binary_Search, Interactive | 15 | 87.89 - -#### Day 8 Binary Search Tree - -| | | | | | -|-|-|-|-|-|- -| 0098 |[Validate Binary Search Tree](src/main/java/g0001_0100/s0098_validate_binary_search_tree/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00 -| 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/java/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/Solution.java)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 4 | 100.00 - -#### Day 9 Graph/BFS/DFS - -| | | | | | -|-|-|-|-|-|- -| 0733 |[Flood Fill](src/main/java/g0701_0800/s0733_flood_fill/Solution.java)| Easy | Array, Depth_First_Search, Breadth_First_Search, Matrix | 1 | 85.36 -| 0200 |[Number of Islands](src/main/java/g0101_0200/s0200_number_of_islands/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 3 | 97.76 - -#### Day 10 Dynamic Programming - -| | | | | | -|-|-|-|-|-|- -| 0509 |[Fibonacci Number](src/main/java/g0501_0600/s0509_fibonacci_number/Solution.java)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 0 | 100.00 -| 0070 |[Climbing Stairs](src/main/java/g0001_0100/s0070_climbing_stairs/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 - -#### Day 11 Dynamic Programming - -| | | | | | -|-|-|-|-|-|- -| 0746 |[Min Cost Climbing Stairs](src/main/java/g0701_0800/s0746_min_cost_climbing_stairs/Solution.java)| Easy | Array, Dynamic_Programming | 1 | 86.38 -| 0062 |[Unique Paths](src/main/java/g0001_0100/s0062_unique_paths/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00 - -#### Day 12 Sliding Window/Two Pointer - -| | | | | | -|-|-|-|-|-|- -| 0438 |[Find All Anagrams in a String](src/main/java/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.java)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 6 | 99.03 -| 0424 |[Longest Repeating Character Replacement](src/main/java/g0401_0500/s0424_longest_repeating_character_replacement/Solution.java)| Medium | String, Hash_Table, Sliding_Window | 5 | 95.15 - -#### Day 13 Hashmap - -| | | | | | -|-|-|-|-|-|- -| 0001 |[Two Sum](src/main/java/g0001_0100/s0001_two_sum/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 2 | 85.97 -| 0299 |[Bulls and Cows](src/main/java/g0201_0300/s0299_bulls_and_cows/Solution.java)| Medium | String, Hash_Table, Counting | 6 | 86.69 - -#### Day 14 Stack - -| | | | | | -|-|-|-|-|-|- -| 0844 |[Backspace String Compare](src/main/java/g0801_0900/s0844_backspace_string_compare/Solution.java)| Easy | String, Two_Pointers, Stack, Simulation | 0 | 100.00 -| 0394 |[Decode String](src/main/java/g0301_0400/s0394_decode_string/Solution.java)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 1 | 87.68 - -#### Day 15 Heap - -| | | | | | -|-|-|-|-|-|- -| 1046 |[Last Stone Weight](src/main/java/g1001_1100/s1046_last_stone_weight/Solution.java)| Easy | Array, Heap_Priority_Queue | 2 | 73.81 -| 0692 |[Top K Frequent Words](src/main/java/g0601_0700/s0692_top_k_frequent_words/Solution.java)| Medium | String, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Trie, Bucket_Sort | 11 | 38.54 +* [Level 1](#level-1) ### Level 2 @@ -1844,6 +1737,113 @@ implementation 'com.github.javadev:leetcode-in-java:1.34' | 1587 |[Bank Account Summary II](src/main/java/g1501_1600/s1587_bank_account_summary_ii/script.sql)| Easy | Database | 630 | 60.32 | 1084 |[Sales Analysis III](src/main/java/g1001_1100/s1084_sales_analysis_iii/script.sql)| Easy | LeetCode_Curated_SQL_70, Database | 1066 | 69.71 +### Level 1 + +#### Day 1 Prefix Sum + +| | | | | | +|-|-|-|-|-|- +| 1480 |[Running Sum of 1d Array](src/main/java/g1401_1500/s1480_running_sum_of_1d_array/Solution.java)| Easy | Array, Prefix_Sum | 0 | 100.00 +| 0724 |[Find Pivot Index](src/main/java/g0701_0800/s0724_find_pivot_index/Solution.java)| Easy | Array, Prefix_Sum | 2 | 69.67 + +#### Day 2 String + +| | | | | | +|-|-|-|-|-|- +| 0205 |[Isomorphic Strings](src/main/java/g0201_0300/s0205_isomorphic_strings/Solution.java)| Easy | String, Hash_Table | 2 | 99.97 +| 0392 |[Is Subsequence](src/main/java/g0301_0400/s0392_is_subsequence/Solution.java)| Easy | String, Dynamic_Programming, Two_Pointers | 1 | 93.01 + +#### Day 3 Linked List + +| | | | | | +|-|-|-|-|-|- +| 0021 |[Merge Two Sorted Lists](src/main/java/g0001_0100/s0021_merge_two_sorted_lists/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00 +| 0206 |[Reverse Linked List](src/main/java/g0201_0300/s0206_reverse_linked_list/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00 + +#### Day 4 Linked List + +| | | | | | +|-|-|-|-|-|- +| 0876 |[Middle of the Linked List](src/main/java/g0801_0900/s0876_middle_of_the_linked_list/Solution.java)| Easy | Two_Pointers, Linked_List | 0 | 100.00 +| 0142 |[Linked List Cycle II](src/main/java/g0101_0200/s0142_linked_list_cycle_ii/Solution.java)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00 + +#### Day 5 Greedy + +| | | | | | +|-|-|-|-|-|- +| 0121 |[Best Time to Buy and Sell Stock](src/main/java/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 100.00 +| 0409 |[Longest Palindrome](src/main/java/g0401_0500/s0409_longest_palindrome/Solution.java)| Easy | String, Hash_Table, Greedy | 2 | 92.90 + +#### Day 6 Tree + +| | | | | | +|-|-|-|-|-|- +| 0589 |[N-ary Tree Preorder Traversal](src/main/java/g0501_0600/s0589_n_ary_tree_preorder_traversal/Solution.java)| Easy | Depth_First_Search, Tree, Stack | 1 | 90.98 +| 0102 |[Binary Tree Level Order Traversal](src/main/java/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 1 | 91.09 + +#### Day 7 Binary Search + +| | | | | | +|-|-|-|-|-|- +| 0704 |[Binary Search](src/main/java/g0701_0800/s0704_binary_search/Solution.java)| Easy | Top_100_Liked_Questions, Array, Binary_Search | 0 | 100.00 +| 0278 |[First Bad Version](src/main/java/g0201_0300/s0278_first_bad_version/Solution.java)| Easy | Binary_Search, Interactive | 15 | 87.89 + +#### Day 8 Binary Search Tree + +| | | | | | +|-|-|-|-|-|- +| 0098 |[Validate Binary Search Tree](src/main/java/g0001_0100/s0098_validate_binary_search_tree/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00 +| 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/java/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/Solution.java)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 4 | 100.00 + +#### Day 9 Graph/BFS/DFS + +| | | | | | +|-|-|-|-|-|- +| 0733 |[Flood Fill](src/main/java/g0701_0800/s0733_flood_fill/Solution.java)| Easy | Array, Depth_First_Search, Breadth_First_Search, Matrix | 1 | 85.36 +| 0200 |[Number of Islands](src/main/java/g0101_0200/s0200_number_of_islands/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 3 | 97.76 + +#### Day 10 Dynamic Programming + +| | | | | | +|-|-|-|-|-|- +| 0509 |[Fibonacci Number](src/main/java/g0501_0600/s0509_fibonacci_number/Solution.java)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 0 | 100.00 +| 0070 |[Climbing Stairs](src/main/java/g0001_0100/s0070_climbing_stairs/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 + +#### Day 11 Dynamic Programming + +| | | | | | +|-|-|-|-|-|- +| 0746 |[Min Cost Climbing Stairs](src/main/java/g0701_0800/s0746_min_cost_climbing_stairs/Solution.java)| Easy | Array, Dynamic_Programming | 1 | 86.38 +| 0062 |[Unique Paths](src/main/java/g0001_0100/s0062_unique_paths/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00 + +#### Day 12 Sliding Window/Two Pointer + +| | | | | | +|-|-|-|-|-|- +| 0438 |[Find All Anagrams in a String](src/main/java/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.java)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 6 | 99.03 +| 0424 |[Longest Repeating Character Replacement](src/main/java/g0401_0500/s0424_longest_repeating_character_replacement/Solution.java)| Medium | String, Hash_Table, Sliding_Window | 5 | 95.15 + +#### Day 13 Hashmap + +| | | | | | +|-|-|-|-|-|- +| 0001 |[Two Sum](src/main/java/g0001_0100/s0001_two_sum/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 2 | 85.97 +| 0299 |[Bulls and Cows](src/main/java/g0201_0300/s0299_bulls_and_cows/Solution.java)| Medium | String, Hash_Table, Counting | 6 | 86.69 + +#### Day 14 Stack + +| | | | | | +|-|-|-|-|-|- +| 0844 |[Backspace String Compare](src/main/java/g0801_0900/s0844_backspace_string_compare/Solution.java)| Easy | String, Two_Pointers, Stack, Simulation | 0 | 100.00 +| 0394 |[Decode String](src/main/java/g0301_0400/s0394_decode_string/Solution.java)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 1 | 87.68 + +#### Day 15 Heap + +| | | | | | +|-|-|-|-|-|- +| 1046 |[Last Stone Weight](src/main/java/g1001_1100/s1046_last_stone_weight/Solution.java)| Easy | Array, Heap_Priority_Queue | 2 | 73.81 +| 0692 |[Top K Frequent Words](src/main/java/g0601_0700/s0692_top_k_frequent_words/Solution.java)| Medium | String, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Trie, Bucket_Sort | 11 | 38.54 + ## Contributing Your ideas/fixes/algorithms are more than welcome! diff --git a/src/main/java/g3201_3300/s3232_find_if_digit_game_can_be_won/Solution.java b/src/main/java/g3201_3300/s3232_find_if_digit_game_can_be_won/Solution.java new file mode 100644 index 000000000..f1489dc33 --- /dev/null +++ b/src/main/java/g3201_3300/s3232_find_if_digit_game_can_be_won/Solution.java @@ -0,0 +1,18 @@ +package g3201_3300.s3232_find_if_digit_game_can_be_won; + +// #Easy #Array #Math #2024_08_02_Time_0_ms_(100.00%)_Space_43.5_MB_(99.62%) + +public class Solution { + public boolean canAliceWin(int[] nums) { + int sdSum = 0; + int ddSum = 0; + for (int num : nums) { + if (num / 10 == 0) { + sdSum += num; + } else { + ddSum += num; + } + } + return sdSum != ddSum; + } +} diff --git a/src/main/java/g3201_3300/s3232_find_if_digit_game_can_be_won/readme.md b/src/main/java/g3201_3300/s3232_find_if_digit_game_can_be_won/readme.md new file mode 100644 index 000000000..491cf3a63 --- /dev/null +++ b/src/main/java/g3201_3300/s3232_find_if_digit_game_can_be_won/readme.md @@ -0,0 +1,44 @@ +3232\. Find if Digit Game Can Be Won + +Easy + +You are given an array of **positive** integers `nums`. + +Alice and Bob are playing a game. In the game, Alice can choose **either** all single-digit numbers or all double-digit numbers from `nums`, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is **strictly greater** than the sum of Bob's numbers. + +Return `true` if Alice can win this game, otherwise, return `false`. + +**Example 1:** + +**Input:** nums = [1,2,3,4,10] + +**Output:** false + +**Explanation:** + +Alice cannot win by choosing either single-digit or double-digit numbers. + +**Example 2:** + +**Input:** nums = [1,2,3,4,5,14] + +**Output:** true + +**Explanation:** + +Alice can win by choosing single-digit numbers which have a sum equal to 15. + +**Example 3:** + +**Input:** nums = [5,5,5,25] + +**Output:** true + +**Explanation:** + +Alice can win by choosing double-digit numbers which have a sum equal to 25. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 99` \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special/Solution.java b/src/main/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special/Solution.java new file mode 100644 index 000000000..618852c60 --- /dev/null +++ b/src/main/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special/Solution.java @@ -0,0 +1,46 @@ +package g3201_3300.s3233_find_the_count_of_numbers_which_are_not_special; + +// #Medium #Array #Math #Number_Theory #2024_08_02_Time_20_ms_(70.12%)_Space_44.6_MB_(17.28%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + public int nonSpecialCount(int l, int r) { + List primes = sieveOfEratosthenes((int) Math.sqrt(r)); + int specialCount = 0; + + for (Integer prime : primes) { + long primeSquare = (long) prime * prime; + if (primeSquare >= l && primeSquare <= r) { + specialCount++; + } + } + + int totalNumbersInRange = r - l + 1; + return totalNumbersInRange - specialCount; + } + + private List sieveOfEratosthenes(int n) { + boolean[] isPrime = new boolean[n + 1]; + for (int i = 2; i <= n; i++) { + isPrime[i] = true; + } + + for (int p = 2; p * p <= n; p++) { + if (isPrime[p]) { + for (int i = p * p; i <= n; i += p) { + isPrime[i] = false; + } + } + } + + List primes = new ArrayList<>(); + for (int i = 2; i <= n; i++) { + if (isPrime[i]) { + primes.add(i); + } + } + return primes; + } +} diff --git a/src/main/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special/readme.md b/src/main/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special/readme.md new file mode 100644 index 000000000..f7fe205aa --- /dev/null +++ b/src/main/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special/readme.md @@ -0,0 +1,36 @@ +3233\. Find the Count of Numbers Which Are Not Special + +Medium + +You are given 2 **positive** integers `l` and `r`. For any number `x`, all positive divisors of `x` _except_ `x` are called the **proper divisors** of `x`. + +A number is called **special** if it has exactly 2 **proper divisors**. For example: + +* The number 4 is _special_ because it has proper divisors 1 and 2. +* The number 6 is _not special_ because it has proper divisors 1, 2, and 3. + +Return the count of numbers in the range `[l, r]` that are **not** **special**. + +**Example 1:** + +**Input:** l = 5, r = 7 + +**Output:** 3 + +**Explanation:** + +There are no special numbers in the range `[5, 7]`. + +**Example 2:** + +**Input:** l = 4, r = 16 + +**Output:** 11 + +**Explanation:** + +The special numbers in the range `[4, 16]` are 4 and 9. + +**Constraints:** + +* 1 <= l <= r <= 109 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones/Solution.java b/src/main/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones/Solution.java new file mode 100644 index 000000000..3265f06e4 --- /dev/null +++ b/src/main/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones/Solution.java @@ -0,0 +1,34 @@ +package g3201_3300.s3234_count_the_number_of_substrings_with_dominant_ones; + +// #Medium #String #Sliding_Window #Enumeration +// #2024_08_02_Time_163_ms_(93.90%)_Space_45.1_MB_(81.71%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + public int numberOfSubstrings(String s) { + List zero = new ArrayList<>(); + zero.add(-1); + int result = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == '0') { + zero.add(i); + } else { + result += i - zero.get(zero.size() - 1); + } + for (int j = 1; j < zero.size(); j++) { + int len = j * (j + 1); + if (len > i + 1) { + break; + } + int prev = zero.get(zero.size() - j - 1); + int from = Math.min(i - len + 1, zero.get(zero.size() - j)); + if (from > prev) { + result += from - prev; + } + } + } + return result; + } +} diff --git a/src/main/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones/readme.md b/src/main/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones/readme.md new file mode 100644 index 000000000..815b7e720 --- /dev/null +++ b/src/main/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones/readme.md @@ -0,0 +1,52 @@ +3234\. Count the Number of Substrings With Dominant Ones + +Medium + +You are given a binary string `s`. + +Return the number of substrings with **dominant** ones. + +A string has **dominant** ones if the number of ones in the string is **greater than or equal to** the **square** of the number of zeros in the string. + +**Example 1:** + +**Input:** s = "00011" + +**Output:** 5 + +**Explanation:** + +The substrings with dominant ones are shown in the table below. + +| i | j | s[i..j] | Number of Zeros | Number of Ones | +|---|---|---------|-----------------|----------------| +| 3 | 3 | 1 | 0 | 1 | +| 4 | 4 | 1 | 0 | 1 | +| 2 | 3 | 01 | 1 | 1 | +| 3 | 4 | 11 | 0 | 2 | +| 2 | 4 | 011 | 1 | 2 | + +**Example 2:** + +**Input:** s = "101101" + +**Output:** 16 + +**Explanation:** + +The substrings with **non-dominant** ones are shown in the table below. + +Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones. + +| i | j | s[i..j] | Number of Zeros | Number of Ones | +|---|---|---------|-----------------|----------------| +| 1 | 1 | 0 | 1 | 0 | +| 4 | 4 | 0 | 1 | 0 | +| 1 | 4 | 0110 | 2 | 2 | +| 0 | 4 | 10110 | 2 | 3 | +| 1 | 5 | 01101 | 2 | 3 | + +**Constraints:** + +* 1 <= s.length <= 4 * 104 +* `s` consists only of characters `'0'` and `'1'`. \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable/Solution.java b/src/main/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable/Solution.java new file mode 100644 index 000000000..6de5bd0be --- /dev/null +++ b/src/main/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable/Solution.java @@ -0,0 +1,116 @@ +package g3201_3300.s3235_check_if_the_rectangle_corner_is_reachable; + +// #Hard #Array #Math #Union_Find #Geometry #Depth_First_Search #Breadth_First_Search +// #2024_08_02_Time_95_ms_(59.46%)_Space_44.8_MB_(94.44%) + +import java.util.Arrays; + +@SuppressWarnings("java:S135") +public class Solution { + public boolean canReachCorner(int x, int y, int[][] circles) { + int n = circles.length; + DisjointSet ds = new DisjointSet(n + 5); + + // Special nodes for boundaries + int leftBoundary = n + 3; + int topBoundary = n; + int rightBoundary = n + 1; + int bottomBoundary = n + 2; + + int i = 0; + for (int[] it : circles) { + int xi = it[0]; + int yi = it[1]; + int ri = it[2]; + if (yi - ri >= y || xi - ri >= x) { + continue; + } + if (((xi > (x + y) || yi > y) && (xi > x || yi > x + y))) { + continue; + } + if (xi <= ri) { + ds.dsu(i, leftBoundary); + } + if (yi <= ri) { + ds.dsu(i, topBoundary); + } + if (x - xi <= ri) { + ds.dsu(i, rightBoundary); + } + if (y - yi <= ri) { + ds.dsu(i, bottomBoundary); + } + i++; + } + + // Union circles that overlap + for (i = 0; i < n; i++) { + int x1 = circles[i][0]; + int y1 = circles[i][1]; + int r1 = circles[i][2]; + if (y1 - r1 >= y || x1 - r1 >= x) { + continue; + } + if (((x1 > (x + y) || y1 > y) && (x1 > x || y1 > x + y))) { + continue; + } + + for (int j = i + 1; j < n; j++) { + int x2 = circles[j][0]; + int y2 = circles[j][1]; + int r2 = circles[j][2]; + double dist = + Math.sqrt( + Math.pow(x1 - (double) x2, 2.0) + Math.pow(y1 - (double) y2, 2.0)); + if (dist <= (r1 + r2)) { + ds.dsu(i, j); + } + } + } + + // Check if left is connected to right or top is connected to bottom + if (ds.findUpar(leftBoundary) == ds.findUpar(rightBoundary) + || ds.findUpar(leftBoundary) == ds.findUpar(topBoundary)) { + return false; + } + return ds.findUpar(bottomBoundary) != ds.findUpar(rightBoundary) + && ds.findUpar(bottomBoundary) != ds.findUpar(topBoundary); + } + + private static class DisjointSet { + private final int[] parent; + private final int[] size; + + public DisjointSet(int n) { + size = new int[n + 1]; + Arrays.fill(size, 1); + parent = new int[n + 1]; + for (int i = 0; i <= n; i++) { + parent[i] = i; + } + } + + public int findUpar(int u) { + if (u == parent[u]) { + return u; + } + parent[u] = findUpar(parent[u]); + return parent[u]; + } + + public void dsu(int u, int v) { + int ulpu = findUpar(u); + int ulpv = findUpar(v); + if (ulpv == ulpu) { + return; + } + if (size[ulpu] < size[ulpv]) { + parent[ulpu] = ulpv; + size[ulpv] += size[ulpu]; + } else { + parent[ulpv] = ulpu; + size[ulpu] += size[ulpv]; + } + } + } +} diff --git a/src/main/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable/readme.md b/src/main/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable/readme.md new file mode 100644 index 000000000..d9c000a24 --- /dev/null +++ b/src/main/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable/readme.md @@ -0,0 +1,62 @@ +3235\. Check if the Rectangle Corner Is Reachable + +Hard + +You are given two positive integers `X` and `Y`, and a 2D array `circles`, where circles[i] = [xi, yi, ri] denotes a circle with center at (xi, yi) and radius ri. + +There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate `(X, Y)`. You need to check whether there is a path from the bottom left corner to the top right corner such that the **entire path** lies inside the rectangle, **does not** touch or lie inside **any** circle, and touches the rectangle **only** at the two corners. + +Return `true` if such a path exists, and `false` otherwise. + +**Example 1:** + +**Input:** X = 3, Y = 4, circles = [[2,1,1]] + +**Output:** true + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/18/example2circle1.png) + +The black curve shows a possible path between `(0, 0)` and `(3, 4)`. + +**Example 2:** + +**Input:** X = 3, Y = 3, circles = [[1,1,2]] + +**Output:** false + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/18/example1circle.png) + +No path exists from `(0, 0)` to `(3, 3)`. + +**Example 3:** + +**Input:** X = 3, Y = 3, circles = [[2,1,1],[1,2,1]] + +**Output:** false + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/18/example0circle.png) + +No path exists from `(0, 0)` to `(3, 3)`. + +**Example 4:** + +**Input:** X = 4, Y = 4, circles = [[5,5,1]] + +**Output:** true + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/07/31/rectangleexample5.png) + +**Constraints:** + +* 3 <= X, Y <= 109 +* `1 <= circles.length <= 1000` +* `circles[i].length == 3` +* 1 <= xi, yi, ri <= 109 \ No newline at end of file diff --git a/src/test/java/g3201_3300/s3232_find_if_digit_game_can_be_won/SolutionTest.java b/src/test/java/g3201_3300/s3232_find_if_digit_game_can_be_won/SolutionTest.java new file mode 100644 index 000000000..025f60f79 --- /dev/null +++ b/src/test/java/g3201_3300/s3232_find_if_digit_game_can_be_won/SolutionTest.java @@ -0,0 +1,23 @@ +package g3201_3300.s3232_find_if_digit_game_can_be_won; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void canAliceWin() { + assertThat(new Solution().canAliceWin(new int[] {1, 2, 3, 4, 10}), equalTo(false)); + } + + @Test + void canAliceWin2() { + assertThat(new Solution().canAliceWin(new int[] {1, 2, 3, 4, 5, 14}), equalTo(true)); + } + + @Test + void canAliceWin3() { + assertThat(new Solution().canAliceWin(new int[] {5, 5, 5, 25}), equalTo(true)); + } +} diff --git a/src/test/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special/SolutionTest.java b/src/test/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special/SolutionTest.java new file mode 100644 index 000000000..77fea3d67 --- /dev/null +++ b/src/test/java/g3201_3300/s3233_find_the_count_of_numbers_which_are_not_special/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3233_find_the_count_of_numbers_which_are_not_special; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void nonSpecialCount() { + assertThat(new Solution().nonSpecialCount(5, 7), equalTo(3)); + } + + @Test + void nonSpecialCount2() { + assertThat(new Solution().nonSpecialCount(4, 16), equalTo(11)); + } +} diff --git a/src/test/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones/SolutionTest.java b/src/test/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones/SolutionTest.java new file mode 100644 index 000000000..15945e39f --- /dev/null +++ b/src/test/java/g3201_3300/s3234_count_the_number_of_substrings_with_dominant_ones/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3234_count_the_number_of_substrings_with_dominant_ones; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfSubstrings() { + assertThat(new Solution().numberOfSubstrings("00011"), equalTo(5)); + } + + @Test + void numberOfSubstrings2() { + assertThat(new Solution().numberOfSubstrings("101101"), equalTo(16)); + } +} diff --git a/src/test/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable/SolutionTest.java b/src/test/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable/SolutionTest.java new file mode 100644 index 000000000..ef530b15c --- /dev/null +++ b/src/test/java/g3201_3300/s3235_check_if_the_rectangle_corner_is_reachable/SolutionTest.java @@ -0,0 +1,30 @@ +package g3201_3300.s3235_check_if_the_rectangle_corner_is_reachable; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void canReachCorner() { + assertThat(new Solution().canReachCorner(3, 4, new int[][] {{2, 1, 1}}), equalTo(true)); + } + + @Test + void canReachCorner2() { + assertThat(new Solution().canReachCorner(3, 3, new int[][] {{1, 1, 2}}), equalTo(false)); + } + + @Test + void canReachCorner3() { + assertThat( + new Solution().canReachCorner(3, 3, new int[][] {{2, 1, 1}, {1, 2, 1}}), + equalTo(false)); + } + + @Test + void canReachCorner4() { + assertThat(new Solution().canReachCorner(4, 4, new int[][] {{5, 5, 1}}), equalTo(true)); + } +}