Skip to content

Commit 4c99f0b

Browse files
authored
Added taks 3309-3321
1 parent 64ae546 commit 4c99f0b

File tree

24 files changed

+1385
-42
lines changed
  • src/main/java
    • g0201_0300/s0208_implement_trie_prefix_tree
    • g0401_0500/s0432_all_oone_data_structure
    • g0501_0600/s0572_subtree_of_another_tree
    • g0801_0900/s0843_guess_the_word
    • g1101_1200/s1191_k_concatenation_maximum_sum
    • g1501_1600/s1584_min_cost_to_connect_all_points
    • g2301_2400/s2400_number_of_ways_to_reach_a_position_after_exactly_k_steps
    • g2501_2600/s2502_design_memory_allocator
    • g2601_2700/s2612_minimum_reverse_operations
    • g2801_2900/s2858_minimum_edge_reversals_so_every_node_is_reachable
    • g3201_3300/s3210_find_the_encrypted_string
    • g3301_3400
      • s3309_maximum_possible_number_by_binary_concatenation
      • s3310_remove_methods_from_project
      • s3311_construct_2d_grid_matching_graph_layout
      • s3312_sorted_gcd_pair_queries
      • s3314_construct_the_minimum_bitwise_array_i
      • s3315_construct_the_minimum_bitwise_array_ii
      • s3316_find_maximum_removals_from_source_string
      • s3317_find_the_number_of_possible_ways_for_an_event
      • s3318_find_x_sum_of_all_k_long_subarrays_i
      • s3319_k_th_largest_perfect_subtree_size_in_binary_tree
      • s3320_count_the_number_of_winning_sequences
      • s3321_find_x_sum_of_all_k_long_subarrays_ii

24 files changed

+1385
-42
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@
434434
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
435435
|-|-|-|-|-|-
436436
| 0117 |[Populating Next Right Pointers in Each Node II](src/main/java/g0101_0200/s0117_populating_next_right_pointers_in_each_node_ii)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Linked_List | 0 | 100.00
437-
| 0572 |[Subtree of Another Tree](src/main/java/g0501_0600/s0572_subtree_of_another_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Hash_Function, String_Matching | 1 | 100.00
437+
| 0572 |[Subtree of Another Tree](src/main/java/g0501_0600/s0572_subtree_of_another_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Hash_Function, String_Matching | 2 | 97.06
438438

439439
#### Day 8 Breadth First Search Depth First Search
440440

@@ -1816,6 +1816,18 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3321 |[Find X-Sum of All K-Long Subarrays II](src/main/java/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii)| Hard | Array, Hash_Table, Heap_Priority_Queue, Sliding_Window | 410 | 94.03
1820+
| 3320 |[Count The Number of Winning Sequences](src/main/java/g3301_3400/s3320_count_the_number_of_winning_sequences)| Hard | String, Dynamic_Programming | 43 | 99.76
1821+
| 3319 |[K-th Largest Perfect Subtree Size in Binary Tree](src/main/java/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree)| Medium | Sorting, Tree, Binary_Tree, Depth_First_Search | 10 | 87.48
1822+
| 3318 |[Find X-Sum of All K-Long Subarrays I](src/main/java/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i)| Easy | Array, Hash_Table, Heap_Priority_Queue, Sliding_Window | 11 | 77.35
1823+
| 3317 |[Find the Number of Possible Ways for an Event](src/main/java/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event)| Hard | Dynamic_Programming, Math, Combinatorics | 20 | 97.08
1824+
| 3316 |[Find Maximum Removals From Source String](src/main/java/g3301_3400/s3316_find_maximum_removals_from_source_string)| Medium | Array, String, Hash_Table, Dynamic_Programming, Two_Pointers | 10 | 100.00
1825+
| 3315 |[Construct the Minimum Bitwise Array II](src/main/java/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii)| Medium | Array, Bit_Manipulation | 1 | 100.00
1826+
| 3314 |[Construct the Minimum Bitwise Array I](src/main/java/g3301_3400/s3314_construct_the_minimum_bitwise_array_i)| Easy | Array, Bit_Manipulation | 3 | 92.32
1827+
| 3312 |[Sorted GCD Pair Queries](src/main/java/g3301_3400/s3312_sorted_gcd_pair_queries)| Hard | Array, Hash_Table, Math, Binary_Search, Prefix_Sum, Counting, Number_Theory, Combinatorics | 29 | 94.69
1828+
| 3311 |[Construct 2D Grid Matching Graph Layout](src/main/java/g3301_3400/s3311_construct_2d_grid_matching_graph_layout)| Hard | Array, Hash_Table, Matrix, Graph | 43 | 94.34
1829+
| 3310 |[Remove Methods From Project](src/main/java/g3301_3400/s3310_remove_methods_from_project)| Medium | Graph, Depth_First_Search, Breadth_First_Search | 41 | 99.76
1830+
| 3309 |[Maximum Possible Number by Binary Concatenation](src/main/java/g3301_3400/s3309_maximum_possible_number_by_binary_concatenation)| Medium | Array, Bit_Manipulation, Enumeration | 3 | 97.01
18191831
| 3307 |[Find the K-th Character in String Game II](src/main/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii)| Hard | Math, Bit_Manipulation, Recursion | 1 | 99.65
18201832
| 3306 |[Count of Substrings Containing Every Vowel and K Consonants II](src/main/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii)| Medium | String, Hash_Table, Sliding_Window | 340 | 44.09
18211833
| 3305 |[Count of Substrings Containing Every Vowel and K Consonants I](src/main/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i)| Medium | String, Hash_Table, Sliding_Window | 2 | 99.72
@@ -3980,7 +3992,7 @@
39803992
| 0577 |[Employee Bonus](src/main/java/g0501_0600/s0577_employee_bonus)| Easy | Database | 1473 | 95.83
39813993
| 0576 |[Out of Boundary Paths](src/main/java/g0501_0600/s0576_out_of_boundary_paths)| Medium | Dynamic_Programming | 5 | 92.95
39823994
| 0575 |[Distribute Candies](src/main/java/g0501_0600/s0575_distribute_candies)| Easy | Array, Hash_Table | 67 | 42.81
3983-
| 0572 |[Subtree of Another Tree](src/main/java/g0501_0600/s0572_subtree_of_another_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Hash_Function, String_Matching, Algorithm_II_Day_7_Breadth_First_Search_Depth_First_Search | 1 | 100.00
3995+
| 0572 |[Subtree of Another Tree](src/main/java/g0501_0600/s0572_subtree_of_another_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Hash_Function, String_Matching, Algorithm_II_Day_7_Breadth_First_Search_Depth_First_Search | 2 | 97.06
39843996
| 0570 |[Managers with at Least 5 Direct Reports](src/main/java/g0501_0600/s0570_managers_with_at_least_5_direct_reports)| Medium | Database | 503 | 69.79
39853997
| 0567 |[Permutation in String](src/main/java/g0501_0600/s0567_permutation_in_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Two_Pointers, Sliding_Window, Algorithm_I_Day_6_Sliding_Window | 5 | 93.93
39863998
| 0566 |[Reshape the Matrix](src/main/java/g0501_0600/s0566_reshape_the_matrix)| Easy | Array, Matrix, Simulation, Data_Structure_I_Day_4_Array, Programming_Skills_I_Day_7_Array | 1 | 90.08

src/main/java/g0201_0300/s0208_implement_trie_prefix_tree/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Implement the Trie class:
4343
```java
4444
@SuppressWarnings("java:S1104")
4545
public class Trie {
46-
private TrieNode root;
46+
private final TrieNode root;
4747
private boolean startWith;
4848

4949
private static class TrieNode {
@@ -82,7 +82,7 @@ public class Trie {
8282
return search(word, root, 0);
8383
}
8484

85-
public boolean search(String word, TrieNode root, int idx) {
85+
private boolean search(String word, TrieNode root, int idx) {
8686
if (idx == word.length()) {
8787
startWith = true;
8888
return root.isWord;

src/main/java/g0401_0500/s0432_all_oone_data_structure/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ public class AllOne {
111111

112112
/* Returns one of the keys with maximal value. */
113113
public String getMaxKey() {
114-
return tail.pre == head ? "" : (String) tail.pre.keySet.iterator().next();
114+
return tail.pre == head ? "" : tail.pre.keySet.iterator().next();
115115
}
116116

117117
/* Returns one of the keys with Minimal value. */
118118
public String getMinKey() {
119-
return head.next == tail ? "" : (String) head.next.keySet.iterator().next();
119+
return head.next == tail ? "" : head.next.keySet.iterator().next();
120120
}
121121

122122
// helper function to make change on given key according to offset

src/main/java/g0501_0600/s0572_subtree_of_another_tree/readme.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,30 @@ import com_github_leetcode.TreeNode;
5353
* }
5454
*/
5555
public class Solution {
56-
private boolean isSubtreeFound(TreeNode root, TreeNode subRoot) {
57-
if (root == null && subRoot == null) {
56+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
57+
if (root == null) {
58+
return false;
59+
}
60+
if (traverse(root, subRoot)) {
5861
return true;
5962
}
60-
if (root == null || subRoot == null) {
63+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
64+
}
65+
66+
private boolean traverse(TreeNode root, TreeNode subRoot) {
67+
if (root == null && subRoot != null) {
6168
return false;
6269
}
63-
if (root.val == subRoot.val) {
64-
return isSubtreeFound(root.left, subRoot.left) && isSubtree(root.right, subRoot.right);
65-
} else {
70+
if (root != null && subRoot == null) {
6671
return false;
6772
}
68-
}
69-
70-
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
71-
if (root == null && subRoot == null) {
73+
if (root == null) {
7274
return true;
7375
}
74-
if (root == null || subRoot == null) {
76+
if (root.val != subRoot.val) {
7577
return false;
7678
}
77-
return isSubtreeFound(root, subRoot)
78-
|| isSubtree(root.left, subRoot)
79-
|| isSubtree(root.right, subRoot);
79+
return traverse(root.left, subRoot.left) && traverse(root.right, subRoot.right);
8080
}
8181
}
8282
```

src/main/java/g0801_0900/s0843_guess_the_word/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import java.util.List;
6060
* }
6161
*/
6262
public class Solution {
63-
interface Master {
63+
public interface Master {
6464
int guess(String word);
6565
}
6666

src/main/java/g1101_1200/s1191_k_concatenation_maximum_sum/readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ As the answer can be very large, return the answer **modulo** <code>10<sup>9</su
4141

4242
```java
4343
public class Solution {
44-
private long mod = 1000000007;
44+
private static final long MOD = 1000000007;
4545

4646
public int kConcatenationMaxSum(int[] arr, int k) {
4747
// int kadane = Kadane(arr);
4848
// #1 when k 1 simply calculate kadanes
4949
if (k == 1) {
50-
return (int) (kadane(arr) % mod);
50+
return (int) (kadane(arr) % MOD);
5151
}
5252
// #2 else calculate the total sum and then check if sum is -Ve or +Ve
5353
long totalSum = 0;
@@ -57,11 +57,11 @@ public class Solution {
5757
// #3 when negative then calculate of arr 2 times only the answer is in there only
5858
if (totalSum < 0) {
5959
// when -ve sum put a extra check here of max from 0
60-
return (int) Math.max(kadaneTwo(arr) % mod, 0);
60+
return (int) Math.max(kadaneTwo(arr) % MOD, 0);
6161
} else {
6262
// #4 when sum is positve then the ans is kadane of 2 + sum * (k-2);
6363
// these two are used sUm*(k-2) ensures that all other are also included
64-
return (int) ((kadaneTwo(arr) + ((k - 2) * totalSum) + mod) % mod);
64+
return (int) ((kadaneTwo(arr) + ((k - 2) * totalSum) + MOD) % MOD);
6565
}
6666
}
6767

src/main/java/g1501_1600/s1584_min_cost_to_connect_all_points/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ public class Solution {
6969
return cost;
7070
}
7171

72-
public void constructMST(
72+
private void constructMST(
7373
int[] parent, int[][] points, boolean[] mst, PriorityQueue<Pair> pq, int[] dist) {
7474
if (!containsFalse(mst)) {
7575
return;
7676
}
7777
Pair newPair = pq.poll();
78+
assert newPair != null;
7879
int pointIndex = newPair.getV();
7980
mst[pointIndex] = true;
8081
for (int i = 0; i < parent.length; i++) {

src/main/java/g2301_2400/s2400_number_of_ways_to_reach_a_position_after_exactly_k_steps/readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ It can be proven that no other way is possible, so we return 3.
4545

4646
```java
4747
public class Solution {
48-
private int mod = 1000000007;
48+
private static final int MOD = 1000000007;
4949

5050
public int numberOfWays(int startPos, int endPos, int k) {
5151
if (Math.abs(endPos - startPos) > k) {
@@ -64,9 +64,9 @@ public class Solution {
6464
rev[1] = 1;
6565
int ans = k;
6666
for (int i = 2; i <= min; i++) {
67-
rev[i] = (int) ((long) (mod - mod / i) * (long) rev[mod % i] % mod);
68-
ans = (int) ((long) ans * (long) (k - i + 1) % mod);
69-
ans = (int) ((long) ans * (long) rev[i] % mod);
67+
rev[i] = (int) ((long) (MOD - MOD / i) * (long) rev[MOD % i] % MOD);
68+
ans = (int) ((long) ans * (long) (k - i + 1) % MOD);
69+
ans = (int) ((long) ans * (long) rev[i] % MOD);
7070
}
7171
return ans;
7272
}

src/main/java/g2501_2600/s2502_design_memory_allocator/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ loc.free(7); // Free all memory units with mID 7. The memory array remains the s
6262

6363
```java
6464
public class Allocator {
65-
Node root;
65+
private final Node root;
6666

6767
public Allocator(int n) {
6868
root = new Node(0, n, -1);
@@ -94,7 +94,7 @@ public class Allocator {
9494
return collapse(root, mID);
9595
}
9696

97-
public int collapse(Node cur, int id) {
97+
private int collapse(Node cur, int id) {
9898
// base case
9999
if (cur == null) {
100100
return 0;

src/main/java/g2601_2700/s2612_minimum_reverse_operations/readme.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,13 @@ In this case the 1 is initially at position 0, so the answer for that position i
5959

6060
```java
6161
import java.util.ArrayList;
62+
import java.util.Arrays;
6263
import java.util.List;
6364

6465
public class Solution {
6566
public int[] minReverseOperations(int n, int p, int[] banned, int k) {
6667
int[] out = new int[n];
67-
for (int i = 0; i < n; i++) {
68-
out[i] = -1;
69-
}
68+
Arrays.fill(out, -1);
7069
for (int node : banned) {
7170
out[node] = -2;
7271
}

src/main/java/g2801_2900/s2858_minimum_edge_reversals_so_every_node_is_reachable/readme.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ So, answer[2] = 1.
7777

7878
```java
7979
import java.util.ArrayList;
80+
import java.util.Arrays;
8081
import java.util.LinkedList;
8182
import java.util.List;
8283
import java.util.Queue;
@@ -95,9 +96,7 @@ public class Solution {
9596
nexts[v].add(new int[] {-1, u});
9697
}
9798
int[] res = new int[n];
98-
for (int i = 0; i < n; i++) {
99-
res[i] = -1;
100-
}
99+
Arrays.fill(res, -1);
101100
res[0] = dfs(nexts, 0, -1);
102101
Queue<Integer> queue = new LinkedList<>();
103102
queue.add(0);

src/main/java/g3201_3300/s3210_find_the_encrypted_string/readme.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ As all the characters are the same, the encrypted string will also be the same.
4646
public class Solution {
4747
public String getEncryptedString(String s, int k) {
4848
int n = s.length();
49-
k = k % n;
50-
StringBuilder str = new StringBuilder(s.substring(k, n));
51-
str.append(s.substring(0, k));
52-
return str.toString();
49+
int localK = k % n;
50+
return s.substring(localK, n) + s.substring(0, localK);
5351
}
5452
}
5553
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3309\. Maximum Possible Number by Binary Concatenation
5+
6+
Medium
7+
8+
You are given an array of integers `nums` of size 3.
9+
10+
Return the **maximum** possible number whose _binary representation_ can be formed by **concatenating** the _binary representation_ of **all** elements in `nums` in some order.
11+
12+
**Note** that the binary representation of any number _does not_ contain leading zeros.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3]
17+
18+
**Output:** 30
19+
20+
**Explanation:**
21+
22+
Concatenate the numbers in the order `[3, 1, 2]` to get the result `"11110"`, which is the binary representation of 30.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [2,8,16]
27+
28+
**Output:** 1296
29+
30+
**Explanation:**
31+
32+
Concatenate the numbers in the order `[2, 8, 16]` to get the result `"10100010000"`, which is the binary representation of 1296.
33+
34+
**Constraints:**
35+
36+
* `nums.length == 3`
37+
* `1 <= nums[i] <= 127`
38+
39+
## Solution
40+
41+
```java
42+
public class Solution {
43+
private String result = "0";
44+
45+
public int maxGoodNumber(int[] nums) {
46+
boolean[] visited = new boolean[nums.length];
47+
StringBuilder sb = new StringBuilder();
48+
solve(nums, visited, 0, sb);
49+
int score = 0;
50+
int val;
51+
for (char c : result.toCharArray()) {
52+
val = c - '0';
53+
score *= 2;
54+
score += val;
55+
}
56+
return score;
57+
}
58+
59+
private void solve(int[] nums, boolean[] visited, int pos, StringBuilder sb) {
60+
if (pos == nums.length) {
61+
String val = sb.toString();
62+
if ((result.length() == val.length() && result.compareTo(val) < 0)
63+
|| val.length() > result.length()) {
64+
result = val;
65+
}
66+
return;
67+
}
68+
String cur;
69+
for (int i = 0; i < nums.length; ++i) {
70+
if (visited[i]) {
71+
continue;
72+
}
73+
visited[i] = true;
74+
cur = Integer.toBinaryString(nums[i]);
75+
sb.append(cur);
76+
solve(nums, visited, pos + 1, sb);
77+
sb.setLength(sb.length() - cur.length());
78+
visited[i] = false;
79+
}
80+
}
81+
}
82+
```

0 commit comments

Comments
 (0)