From df459b92d469a159fb6af739aaafe95415315c73 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Mon, 26 Feb 2024 11:04:03 +0700 Subject: [PATCH 1/9] Added tasks 3002-3007 --- .../Solution.java | 36 ++++++ .../readme.md | 40 ++++++ .../Solution.java | 90 ++++++++++++++ .../readme.md | 66 ++++++++++ .../Solution.java | 44 +++++++ .../readme.md | 30 +++++ .../Solution.java | 116 ++++++++++++++++++ .../readme.md | 41 +++++++ .../Solution.java | 25 ++++ .../readme.md | 55 +++++++++ .../SolutionTest.java | 31 +++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 18 +++ 15 files changed, 656 insertions(+) create mode 100644 src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/Solution.java create mode 100644 src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/readme.md create mode 100644 src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java create mode 100644 src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/readme.md create mode 100644 src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/Solution.java create mode 100644 src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/readme.md create mode 100644 src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java create mode 100644 src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/readme.md create mode 100644 src/main/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/Solution.java create mode 100644 src/main/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/readme.md create mode 100644 src/test/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3005_count_elements_with_maximum_frequency/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/SolutionTest.java diff --git a/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/Solution.java b/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/Solution.java new file mode 100644 index 000000000..e09fd4ccb --- /dev/null +++ b/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/Solution.java @@ -0,0 +1,36 @@ +package g3001_3100.s3002_maximum_size_of_a_set_after_removals; + +// #Medium #Array #Hash_Table #Greedy #2024_02_26_Time_26_ms_(94.01%)_Space_53.3_MB_(80.90%) + +import java.util.HashSet; + +public class Solution { + public int maximumSetSize(int[] nums1, int[] nums2) { + HashSet uniq1 = new HashSet<>(); + HashSet uniq2 = new HashSet<>(); + for (int i = 0; i < nums1.length; i++) { + uniq1.add(nums1[i]); + uniq2.add(nums2[i]); + } + int common = 0; + if (uniq1.size() <= uniq2.size()) { + for (int u : uniq1) { + if (uniq2.contains(u)) { + common++; + } + } + } else { + for (int u : uniq2) { + if (uniq1.contains(u)) { + common++; + } + } + } + int half = nums1.length / 2; + int from1 = Math.min(uniq1.size() - common, half); + int from2 = Math.min(uniq2.size() - common, half); + int takeFromCommon1 = half - from1; + int takeFromCommon2 = half - from2; + return from1 + from2 + Math.min(takeFromCommon1 + takeFromCommon2, common); + } +} diff --git a/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/readme.md b/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/readme.md new file mode 100644 index 000000000..4e779978d --- /dev/null +++ b/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/readme.md @@ -0,0 +1,40 @@ +3002\. Maximum Size of a Set After Removals + +Medium + +You are given two **0-indexed** integer arrays `nums1` and `nums2` of even length `n`. + +You must remove `n / 2` elements from `nums1` and `n / 2` elements from `nums2`. After the removals, you insert the remaining elements of `nums1` and `nums2` into a set `s`. + +Return _the **maximum** possible size of the set_ `s`. + +**Example 1:** + +**Input:** nums1 = [1,2,1,2], nums2 = [1,1,1,1] + +**Output:** 2 + +**Explanation:** We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}. It can be shown that 2 is the maximum possible size of the set s after the removals. + +**Example 2:** + +**Input:** nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3] + +**Output:** 5 + +**Explanation:** We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}. It can be shown that 5 is the maximum possible size of the set s after the removals. + +**Example 3:** + +**Input:** nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6] + +**Output:** 6 + +**Explanation:** We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}. It can be shown that 6 is the maximum possible size of the set s after the removals. + +**Constraints:** + +* `n == nums1.length == nums2.length` +* 1 <= n <= 2 * 104 +* `n` is even. +* 1 <= nums1[i], nums2[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java new file mode 100644 index 000000000..d5616479d --- /dev/null +++ b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java @@ -0,0 +1,90 @@ +package g3001_3100.s3003_maximize_the_number_of_partitions_after_operations; + +// #Hard #String #Dynamic_Programming #Bit_Manipulation #Bitmask +// #2024_02_26_Time_1_ms_(100.00%)_Space_42.1_MB_(99.44%) + +public class Solution { + private static final int ALPHABET_SIZE = 'z' - 'a' + 1; + + public int maxPartitionsAfterOperations(String s, int k) { + if (k == ALPHABET_SIZE) { + return 1; + } + int n = s.length(); + int[] ansr = new int[n]; + int[] usedr = new int[n]; + int used = 0; + int cntUsed = 0; + int ans = 1; + for (int i = n - 1; i >= 0; --i) { + int ch = s.charAt(i) - 'a'; + if ((used & (1 << ch)) == 0) { + if (cntUsed == k) { + cntUsed = 0; + used = 0; + ans++; + } + used |= (1 << ch); + cntUsed++; + } + ansr[i] = ans; + usedr[i] = used; + } + + int ansl = 0; + ans = ansr[0]; + for (int l = 0; l < n; ) { + used = 0; + cntUsed = 0; + int usedBeforeLast = 0; + int usedTwiceBeforeLast = 0; + int last = -1; + int r = l; + while (r < n) { + int ch = s.charAt(r) - 'a'; + if ((used & (1 << ch)) == 0) { + if (cntUsed == k) { + break; + } + usedBeforeLast = used; + last = r; + used |= (1 << ch); + cntUsed++; + } else if (cntUsed < k) { + usedTwiceBeforeLast |= (1 << ch); + } + r++; + } + + if (cntUsed == k) { + if (last - l > Integer.bitCount(usedBeforeLast)) { + ans = Math.max(ans, ansl + 1 + ansr[last]); + } + if (last + 1 < r) { + if (last + 2 >= n) { + ans = Math.max(ans, ansl + 1 + 1); + } else { + if (Integer.bitCount(usedr[last + 2]) == k) { + int canUse = ((1 << ALPHABET_SIZE) - 1) & ~used & ~usedr[last + 2]; + if (canUse > 0) { + ans = Math.max(ans, ansl + 1 + 1 + ansr[last + 2]); + } else { + ans = Math.max(ans, ansl + 1 + ansr[last + 2]); + } + int l1 = s.charAt(last + 1) - 'a'; + if ((usedTwiceBeforeLast & (1 << l1)) == 0) { + ans = Math.max(ans, ansl + 1 + ansr[last + 1]); + } + } else { + ans = Math.max(ans, ansl + 1 + ansr[last + 2]); + } + } + } + } + l = r; + ansl++; + } + + return ans; + } +} diff --git a/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/readme.md b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/readme.md new file mode 100644 index 000000000..8c9fec781 --- /dev/null +++ b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/readme.md @@ -0,0 +1,66 @@ +3003\. Maximize the Number of Partitions After Operations + +Hard + +You are given a **0-indexed** string `s` and an integer `k`. + +You are to perform the following partitioning operations until `s` is **empty**: + +* Choose the **longest** **prefix** of `s` containing at most `k` **distinct** characters. +* **Delete** the prefix from `s` and increase the number of partitions by one. The remaining characters (if any) in `s` maintain their initial order. + +**Before** the operations, you are allowed to change **at most** **one** index in `s` to another lowercase English letter. + +Return _an integer denoting the **maximum** number of resulting partitions after the operations by optimally choosing at most one index to change._ + +**Example 1:** + +**Input:** s = "accca", k = 2 + +**Output:** 3 + +**Explanation:** In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'. s becomes "acbca". The operations can now be performed as follows until s becomes empty: +- Choose the longest prefix containing at most 2 distinct characters, "acbca". +- Delete the prefix, and s becomes "bca". The number of partitions is now 1. +- Choose the longest prefix containing at most 2 distinct characters, "bca". +- Delete the prefix, and s becomes "a". The number of partitions is now 2. +- Choose the longest prefix containing at most 2 distinct characters, "a". +- Delete the prefix, and s becomes empty. The number of partitions is now 3. + +Hence, the answer is 3. It can be shown that it is not possible to obtain more than 3 partitions. + +**Example 2:** + +**Input:** s = "aabaab", k = 3 + +**Output:** 1 + +**Explanation:** In this example, to maximize the number of resulting partitions we can leave s as it is. The operations can now be performed as follows until s becomes empty: +- Choose the longest prefix containing at most 3 distinct characters, "aabaab". +- Delete the prefix, and s becomes empty. The number of partitions becomes 1. + +Hence, the answer is 1. It can be shown that it is not possible to obtain more than 1 partition. + +**Example 3:** + +**Input:** s = "xxyz", k = 1 + +**Output:** 4 + +**Explanation:** In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'. s becomes "xayz". The operations can now be performed as follows until s becomes empty: +- Choose the longest prefix containing at most 1 distinct character, "xayz". +- Delete the prefix, and s becomes "ayz". The number of partitions is now 1. +- Choose the longest prefix containing at most 1 distinct character, "ayz". +- Delete the prefix, and s becomes "yz". The number of partitions is now 2. +- Choose the longest prefix containing at most 1 distinct character, "yz". +- Delete the prefix, and s becomes "z". The number of partitions is now 3. +- Choose the longest prefix containing at most 1 distinct character, "z". +- Delete the prefix, and s becomes empty. The number of partitions is now 4. + +Hence, the answer is 4. It can be shown that it is not possible to obtain more than 4 partitions. + +**Constraints:** + +* 1 <= s.length <= 104 +* `s` consists only of lowercase English letters. +* `1 <= k <= 26` \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/Solution.java b/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/Solution.java new file mode 100644 index 000000000..eefcd6d46 --- /dev/null +++ b/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/Solution.java @@ -0,0 +1,44 @@ +package g3001_3100.s3005_count_elements_with_maximum_frequency; + +// #Easy #Array #Hash_Table #Counting #2024_02_26_Time_1_ms_(99.76%)_Space_41.6_MB_(98.97%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + public int maxFrequencyElements(int[] nums) { + if (nums.length == 1) { + return 1; + } + List list = new ArrayList<>(); + int co = 0; + int prev = 0; + for (int num : nums) { + if (list.contains(num)) { + continue; + } + list.add(num); + if (list.size() == nums.length) { + break; + } + int c = 0; + for (int i : nums) { + if (num == i) { + c++; + } + } + if (c > 1) { + if (c > prev) { + co = c; + prev = c; + } else if (c == prev) { + co = c + co; + } + } + } + if (co == 0) { + return nums.length; + } + return co; + } +} diff --git a/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/readme.md b/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/readme.md new file mode 100644 index 000000000..1d7e14492 --- /dev/null +++ b/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/readme.md @@ -0,0 +1,30 @@ +3005\. Count Elements With Maximum Frequency + +Easy + +You are given an array `nums` consisting of **positive** integers. + +Return _the **total frequencies** of elements in_ `nums` _such that those elements all have the **maximum** frequency_. + +The **frequency** of an element is the number of occurrences of that element in the array. + +**Example 1:** + +**Input:** nums = [1,2,2,3,1,4] + +**Output:** 4 + +**Explanation:** The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array. So the number of elements in the array with maximum frequency is 4. + +**Example 2:** + +**Input:** nums = [1,2,3,4,5] + +**Output:** 5 + +**Explanation:** All elements of the array have a frequency of 1 which is the maximum. So the number of elements in the array with maximum frequency is 5. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java new file mode 100644 index 000000000..29a8f14ae --- /dev/null +++ b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java @@ -0,0 +1,116 @@ +package g3001_3100.s3006_find_beautiful_indices_in_the_given_array_i; + +// #Medium #String #Binary_Search #Two_Pointers #Hash_Function #String_Matching #Rolling_Hash +// #2024_02_26_Time_8_ms_(95.86%)_Space_45.8_MB_(80.19%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + public List beautifulIndices(String s, String a, String b, int q) { + char[] sc = s.toCharArray(); + char[] ac = a.toCharArray(); + char[] bc = b.toCharArray(); + int[] lpsa = getLps(ac); + int[] lpsb = getLps(bc); + int mo; + + int[] comp = new int[sc.length]; + int[] st = new int[sc.length]; + int si = 0, k; + mo = -bc.length + 1; + if (bc[0] == sc[0]) { + comp[0] = 1; + if (bc.length == 1) { + st[si++] = mo; + } + } + for (int i = 1; i < comp.length; i++) { + mo++; + if (sc[i] == bc[0]) { + comp[i] = 1; + } + k = comp[i - 1]; + if (k == bc.length) { + k = lpsb[k - 1]; + } + while (k > 0) { + if (bc[k] == sc[i]) { + comp[i] = k + 1; + break; + } + k = lpsb[k - 1]; + } + if (comp[i] == bc.length) { + st[si++] = mo; + } + } + int sia = 0; + mo = -ac.length + 1; + List ret = new ArrayList<>(); + if (si == 0) { + return ret; + } + + if (sc[0] == ac[0]) { + comp[0] = 1; + if (ac.length == 1 && st[0] <= q) { + ret.add(0); + } + } else { + comp[0] = 0; + } + + for (int i = 1; i < comp.length; i++) { + mo++; + if (sc[i] == ac[0]) { + comp[i] = 1; + } else { + comp[i] = 0; + } + k = comp[i - 1]; + if (k == ac.length) { + k = lpsa[k - 1]; + } + while (k > 0) { + if (ac[k] == sc[i]) { + comp[i] = k + 1; + break; + } + k = lpsa[k - 1]; + } + if (comp[i] == ac.length) { + while (sia < si && st[sia] + q < mo) { + sia++; + } + if (sia == si) { + break; + } + if (mo >= st[sia] - q && mo <= st[sia] + q) { + ret.add(mo); + } + } + } + + return ret; + } + + private int[] getLps(char[] xc) { + int[] r = new int[xc.length]; + int k; + for (int i = 1; i < xc.length; i++) { + if (xc[i] == xc[0]) { + r[i] = 1; + } + k = r[i - 1]; + while (k > 0) { + if (xc[k] == xc[i]) { + r[i] = k + 1; + break; + } + k = r[k - 1]; + } + } + return r; + } +} diff --git a/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/readme.md b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/readme.md new file mode 100644 index 000000000..007313fbe --- /dev/null +++ b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/readme.md @@ -0,0 +1,41 @@ +3006\. Find Beautiful Indices in the Given Array I + +Medium + +You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`. + +An index `i` is **beautiful** if: + +* `0 <= i <= s.length - a.length` +* `s[i..(i + a.length - 1)] == a` +* There exists an index `j` such that: + * `0 <= j <= s.length - b.length` + * `s[j..(j + b.length - 1)] == b` + * `|j - i| <= k` + +Return _the array that contains beautiful indices in **sorted order from smallest to largest**_. + +**Example 1:** + +**Input:** s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15 + +**Output:** [16,33] + +**Explanation:** There are 2 beautiful indices: [16,33]. +- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15. +- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15. Thus we return [16,33] as the result. + +**Example 2:** + +**Input:** s = "abcd", a = "a", b = "a", k = 4 + +**Output:** [0] + +**Explanation:** There is 1 beautiful index: [0]. +- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4. Thus we return [0] as the result. + +**Constraints:** + +* 1 <= k <= s.length <= 105 +* `1 <= a.length, b.length <= 10` +* `s`, `a`, and `b` contain only lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/Solution.java b/src/main/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/Solution.java new file mode 100644 index 000000000..e50fd6d1e --- /dev/null +++ b/src/main/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/Solution.java @@ -0,0 +1,25 @@ +package g3001_3100.s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k; + +// #Medium #Dynamic_Programming #Binary_Search #Bit_Manipulation +// #2024_02_26_Time_1_ms_(100.00%)_Space_41_MB_(47.71%) + +public class Solution { + private long count(long k, int bit, int x) { + if (k < bit) { + return 0; + } + long n = 1; + long bits = bit; + long p = 1; + while (2 * bits + (p % x == 0 ? n : 0) <= k) { + bits = 2 * bits + (p % x == 0 ? n : 0); + n *= 2; + ++p; + } + return n + count(k - bits, bit + (p % x == 0 ? 1 : 0), x); + } + + public long findMaximumNumber(long k, int x) { + return count(k, 0, x) - 1; + } +} diff --git a/src/main/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/readme.md b/src/main/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/readme.md new file mode 100644 index 000000000..8eea3fca0 --- /dev/null +++ b/src/main/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/readme.md @@ -0,0 +1,55 @@ +3007\. Maximum Number That Sum of the Prices Is Less Than or Equal to K + +Medium + +You are given an integer `k` and an integer `x`. + +Consider `s` is the **1-indexed** binary representation of an integer `num`. The **price** of a number `num` is the number of `i`'s such that `i % x == 0` and `s[i]` is a **set bit**. + +Return _the **greatest** integer_ `num` _such that the sum of **prices** of all numbers from_ `1` _to_ `num` _is less than or equal to_ `k`_._ + +**Note**: + +* In the binary representation of a number **set bit** is a bit of value `1`. +* The binary representation of a number will be indexed from right to left. For example, if `s == 11100`, `s[4] == 1` and `s[2] == 0`. + +**Example 1:** + +**Input:** k = 9, x = 1 + +**Output:** 6 + +**Explanation:** The numbers 1, 2, 3, 4, 5, and 6 can be written in binary representation as "1", "10", "11", "100", "101", and "110" respectively. Since x is equal to 1, the price of each number is the number of its set bits. + +The number of set bits in these numbers is 9. So the sum of the prices of the first 6 numbers is 9. So the answer is 6. + +**Example 2:** + +**Input:** k = 7, x = 2 + +**Output:** 9 + +**Explanation:** Since x is equal to 2, we should just check eventh bits. + +The second bit of binary representation of numbers 2 and 3 is a set bit. + +So the sum of their prices is 2. + +The second bit of binary representation of numbers 6 and 7 is a set bit. + +So the sum of their prices is 2. + +The fourth bit of binary representation of numbers 8 and 9 is a set bit but their second bit is not. So the sum of their prices is 2. + +Numbers 1, 4, and 5 don't have set bits in their eventh bits in their binary representation. So the sum of their prices is 0. + +The second and the fourth bit of the binary representation of the number 10 are a set bit. So its price is 2. + +The sum of the prices of the first 9 numbers is 6. + +Because the sum of the prices of the first 10 numbers is 8, the answer is 9. + +**Constraints:** + +* 1 <= k <= 1015 +* `1 <= x <= 8` \ No newline at end of file diff --git a/src/test/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/SolutionTest.java b/src/test/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/SolutionTest.java new file mode 100644 index 000000000..d5210dbe8 --- /dev/null +++ b/src/test/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/SolutionTest.java @@ -0,0 +1,31 @@ +package g3001_3100.s3002_maximum_size_of_a_set_after_removals; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumSetSize() { + assertThat( + new Solution().maximumSetSize(new int[] {1, 2, 1, 2}, new int[] {1, 1, 1, 1}), + equalTo(2)); + } + + @Test + void maximumSetSize2() { + assertThat( + new Solution() + .maximumSetSize(new int[] {1, 2, 3, 4, 5, 6}, new int[] {2, 3, 2, 3, 2, 3}), + equalTo(5)); + } + + @Test + void maximumSetSize3() { + assertThat( + new Solution() + .maximumSetSize(new int[] {1, 1, 2, 2, 3, 3}, new int[] {4, 4, 5, 5, 6, 6}), + equalTo(6)); + } +} diff --git a/src/test/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/SolutionTest.java b/src/test/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/SolutionTest.java new file mode 100644 index 000000000..f1ef97773 --- /dev/null +++ b/src/test/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/SolutionTest.java @@ -0,0 +1,23 @@ +package g3001_3100.s3003_maximize_the_number_of_partitions_after_operations; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxPartitionsAfterOperations() { + assertThat(new Solution().maxPartitionsAfterOperations("accca", 2), equalTo(3)); + } + + @Test + void maxPartitionsAfterOperations2() { + assertThat(new Solution().maxPartitionsAfterOperations("aabaab", 3), equalTo(1)); + } + + @Test + void maxPartitionsAfterOperations3() { + assertThat(new Solution().maxPartitionsAfterOperations("xxyz", 1), equalTo(4)); + } +} diff --git a/src/test/java/g3001_3100/s3005_count_elements_with_maximum_frequency/SolutionTest.java b/src/test/java/g3001_3100/s3005_count_elements_with_maximum_frequency/SolutionTest.java new file mode 100644 index 000000000..77136a6e1 --- /dev/null +++ b/src/test/java/g3001_3100/s3005_count_elements_with_maximum_frequency/SolutionTest.java @@ -0,0 +1,18 @@ +package g3001_3100.s3005_count_elements_with_maximum_frequency; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxFrequencyElements() { + assertThat(new Solution().maxFrequencyElements(new int[] {1, 2, 2, 3, 1, 4}), equalTo(4)); + } + + @Test + void maxFrequencyElements2() { + assertThat(new Solution().maxFrequencyElements(new int[] {1, 2, 3, 4, 5}), equalTo(5)); + } +} diff --git a/src/test/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/SolutionTest.java b/src/test/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/SolutionTest.java new file mode 100644 index 000000000..452e85207 --- /dev/null +++ b/src/test/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3001_3100.s3006_find_beautiful_indices_in_the_given_array_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void beautifulIndices() { + assertThat( + new Solution() + .beautifulIndices( + "isawsquirrelnearmysquirrelhouseohmy", "my", "squirrel", 15), + equalTo(Arrays.asList(16, 33))); + } + + @Test + void beautifulIndices2() { + assertThat(new Solution().beautifulIndices("abcd", "a", "a", 4), equalTo(Arrays.asList(0))); + } +} diff --git a/src/test/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/SolutionTest.java b/src/test/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/SolutionTest.java new file mode 100644 index 000000000..a09eea954 --- /dev/null +++ b/src/test/java/g3001_3100/s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k/SolutionTest.java @@ -0,0 +1,18 @@ +package g3001_3100.s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findMaximumNumber() { + assertThat(new Solution().findMaximumNumber(9, 1), equalTo(6L)); + } + + @Test + void findMaximumNumber2() { + assertThat(new Solution().findMaximumNumber(7, 2), equalTo(9L)); + } +} From 5df8f3f649ba1839fbc727e2d55080e0209cb717 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Mon, 26 Feb 2024 11:07:02 +0700 Subject: [PATCH 2/9] Update Solution.java --- .../Solution.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java index 29a8f14ae..c15e5da35 100644 --- a/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java +++ b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java @@ -17,7 +17,8 @@ public List beautifulIndices(String s, String a, String b, int q) { int[] comp = new int[sc.length]; int[] st = new int[sc.length]; - int si = 0, k; + int si = 0; + int k; mo = -bc.length + 1; if (bc[0] == sc[0]) { comp[0] = 1; From 8d50f593f8de16780d46932fac92629aeeb88823 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Mon, 26 Feb 2024 11:23:44 +0700 Subject: [PATCH 3/9] Update Solution.java --- .../Solution.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java index d5616479d..a8092fd5c 100644 --- a/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java +++ b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java @@ -33,7 +33,8 @@ public int maxPartitionsAfterOperations(String s, int k) { int ansl = 0; ans = ansr[0]; - for (int l = 0; l < n; ) { + int l = 0; + while (l < n) { used = 0; cntUsed = 0; int usedBeforeLast = 0; From 16ac06f4095938fab6b5a8f9e7b7f293fc0a5a4f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 26 Feb 2024 07:29:34 +0200 Subject: [PATCH 4/9] Update readme.md --- .../readme.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/readme.md b/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/readme.md index 4e779978d..4a04aaf24 100644 --- a/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/readme.md +++ b/src/main/java/g3001_3100/s3002_maximum_size_of_a_set_after_removals/readme.md @@ -14,7 +14,9 @@ Return _the **maximum** possible size of the set_ `s`. **Output:** 2 -**Explanation:** We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}. It can be shown that 2 is the maximum possible size of the set s after the removals. +**Explanation:** We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}. + +It can be shown that 2 is the maximum possible size of the set s after the removals. **Example 2:** @@ -22,7 +24,9 @@ Return _the **maximum** possible size of the set_ `s`. **Output:** 5 -**Explanation:** We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}. It can be shown that 5 is the maximum possible size of the set s after the removals. +**Explanation:** We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}. + +It can be shown that 5 is the maximum possible size of the set s after the removals. **Example 3:** @@ -30,11 +34,13 @@ Return _the **maximum** possible size of the set_ `s`. **Output:** 6 -**Explanation:** We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}. It can be shown that 6 is the maximum possible size of the set s after the removals. +**Explanation:** We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}. + +It can be shown that 6 is the maximum possible size of the set s after the removals. **Constraints:** * `n == nums1.length == nums2.length` * 1 <= n <= 2 * 104 * `n` is even. -* 1 <= nums1[i], nums2[i] <= 109 \ No newline at end of file +* 1 <= nums1[i], nums2[i] <= 109 From 5f7c2c64206ec51ba4511dcff776bcfede7d478f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 26 Feb 2024 07:31:27 +0200 Subject: [PATCH 5/9] Update Solution.java --- .../Solution.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java index a8092fd5c..9e6cf5719 100644 --- a/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java +++ b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java @@ -30,7 +30,6 @@ public int maxPartitionsAfterOperations(String s, int k) { ansr[i] = ans; usedr[i] = used; } - int ansl = 0; ans = ansr[0]; int l = 0; @@ -56,7 +55,6 @@ public int maxPartitionsAfterOperations(String s, int k) { } r++; } - if (cntUsed == k) { if (last - l > Integer.bitCount(usedBeforeLast)) { ans = Math.max(ans, ansl + 1 + ansr[last]); @@ -85,7 +83,6 @@ public int maxPartitionsAfterOperations(String s, int k) { l = r; ansl++; } - return ans; } } From bff0e1e6ac7d40ce05e91848fead3562d78ef890 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 26 Feb 2024 07:32:26 +0200 Subject: [PATCH 6/9] Update Solution.java --- .../Solution.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java index c15e5da35..4e4a2ed92 100644 --- a/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java +++ b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java @@ -14,7 +14,6 @@ public List beautifulIndices(String s, String a, String b, int q) { int[] lpsa = getLps(ac); int[] lpsb = getLps(bc); int mo; - int[] comp = new int[sc.length]; int[] st = new int[sc.length]; int si = 0; @@ -52,7 +51,6 @@ public List beautifulIndices(String s, String a, String b, int q) { if (si == 0) { return ret; } - if (sc[0] == ac[0]) { comp[0] = 1; if (ac.length == 1 && st[0] <= q) { @@ -61,7 +59,6 @@ public List beautifulIndices(String s, String a, String b, int q) { } else { comp[0] = 0; } - for (int i = 1; i < comp.length; i++) { mo++; if (sc[i] == ac[0]) { @@ -92,7 +89,6 @@ public List beautifulIndices(String s, String a, String b, int q) { } } } - return ret; } From 8047f12934ed8401dd61a91320b5de1ed381b46b Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 26 Feb 2024 07:35:11 +0200 Subject: [PATCH 7/9] Update Solution.java --- .../Solution.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java index 9e6cf5719..d77b1a644 100644 --- a/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java +++ b/src/main/java/g3001_3100/s3003_maximize_the_number_of_partitions_after_operations/Solution.java @@ -3,6 +3,7 @@ // #Hard #String #Dynamic_Programming #Bit_Manipulation #Bitmask // #2024_02_26_Time_1_ms_(100.00%)_Space_42.1_MB_(99.44%) +@SuppressWarnings("java:S6541") public class Solution { private static final int ALPHABET_SIZE = 'z' - 'a' + 1; From e0ca4b2cbb593b9b4394f334659bd456b2bddf88 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 26 Feb 2024 07:36:58 +0200 Subject: [PATCH 8/9] Update Solution.java --- .../s3005_count_elements_with_maximum_frequency/Solution.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/Solution.java b/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/Solution.java index eefcd6d46..a1e356bd0 100644 --- a/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/Solution.java +++ b/src/main/java/g3001_3100/s3005_count_elements_with_maximum_frequency/Solution.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.List; +@SuppressWarnings("java:S135") public class Solution { public int maxFrequencyElements(int[] nums) { if (nums.length == 1) { From 462b3ab1ba1dbaa8c6443a961d064dd9123781c3 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 26 Feb 2024 07:38:32 +0200 Subject: [PATCH 9/9] Update Solution.java --- .../Solution.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java index 4e4a2ed92..78e32db7f 100644 --- a/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java +++ b/src/main/java/g3001_3100/s3006_find_beautiful_indices_in_the_given_array_i/Solution.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; +@SuppressWarnings("java:S6541") public class Solution { public List beautifulIndices(String s, String a, String b, int q) { char[] sc = s.toCharArray();