Skip to content

Commit cadf285

Browse files
authored
Updated readme.
1 parent e72cd94 commit cadf285

File tree

21 files changed

+1866
-37
lines changed
  • src/main/java
    • g0001_0100/s0001_two_sum
    • g2101_2200
      • s2120_execution_of_all_suffix_instructions_staying_in_a_grid
      • s2121_intervals_between_identical_elements
      • s2122_recover_the_original_array
      • s2124_check_if_all_as_appears_before_all_bs
      • s2125_number_of_laser_beams_in_a_bank
      • s2126_destroying_asteroids
      • s2127_maximum_employees_to_be_invited_to_a_meeting
      • s2129_capitalize_the_title
      • s2130_maximum_twin_sum_of_a_linked_list
      • s2131_longest_palindrome_by_concatenating_two_letter_words
      • s2132_stamping_the_grid
      • s2133_check_if_every_row_and_column_contains_all_numbers
      • s2134_minimum_swaps_to_group_all_1s_together_ii
      • s2135_count_words_obtained_after_adding_a_letter
      • s2136_earliest_possible_day_of_full_bloom
      • s2155_all_divisions_with_the_highest_score_of_a_binary_array
      • s2160_minimum_sum_of_four_digit_number_after_splitting_digits
      • s2161_partition_array_according_to_given_pivot
      • s2162_minimum_cost_to_set_cooking_time

21 files changed

+1866
-37
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1515
|-|-|-|-|-|-
16-
| 0001 |[Two Sum](src/main/java/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 3 | 80.08
16+
| 0001 |[Two Sum](src/main/java/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 1 | 99.68
1717
| 0088 |[Merge Sorted Array](src/main/java/g0001_0100/s0088_merge_sorted_array)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers | 0 | 100.00
1818

1919
#### Day 3 Array
@@ -1338,9 +1338,28 @@
13381338
| 2181 |[Merge Nodes in Between Zeros](src/main/java/g2101_2200/s2181_merge_nodes_in_between_zeros)| Medium | Simulation, Linked_List | 6 | 96.26
13391339
| 2172 |[Maximum AND Sum of Array](src/main/java/g2101_2200/s2172_maximum_and_sum_of_array)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask | 21 | 87.50
13401340
| 2166 |[Design Bitset](src/main/java/g2101_2200/s2166_design_bitset)| Medium | Array, Hash_Table, Design | 81 | 73.38
1341+
| 2162 |[Minimum Cost to Set Cooking Time](src/main/java/g2101_2200/s2162_minimum_cost_to_set_cooking_time)| Medium | Math, Enumeration | 1 | 95.82
1342+
| 2161 |[Partition Array According to Given Pivot](src/main/java/g2101_2200/s2161_partition_array_according_to_given_pivot)| Medium | Array, Two_Pointers, Simulation | 7 | 72.76
1343+
| 2160 |[Minimum Sum of Four Digit Number After Splitting Digits](src/main/java/g2101_2200/s2160_minimum_sum_of_four_digit_number_after_splitting_digits)| Easy | Math, Sorting, Greedy | 1 | 78.31
1344+
| 2155 |[All Divisions With the Highest Score of a Binary Array](src/main/java/g2101_2200/s2155_all_divisions_with_the_highest_score_of_a_binary_array)| Medium | Array | 18 | 98.41
13411345
| 2154 |[Keep Multiplying Found Values by Two](src/main/java/g2101_2200/s2154_keep_multiplying_found_values_by_two)| Easy | Array, Hash_Table, Sorting, Simulation | 1 | 93.21
13421346
| 2151 |[Maximum Good People Based on Statements](src/main/java/g2101_2200/s2151_maximum_good_people_based_on_statements)| Hard | Array, Bit_Manipulation, Backtracking, Enumeration | 76 | 47.57
13431347
| 2150 |[Find All Lonely Numbers in the Array](src/main/java/g2101_2200/s2150_find_all_lonely_numbers_in_the_array)| Medium | Array, Hash_Table, Counting | 93 | 70.66
1348+
| 2136 |[Earliest Possible Day of Full Bloom](src/main/java/g2101_2200/s2136_earliest_possible_day_of_full_bloom)| Hard | Array, Sorting, Greedy | 63 | 94.92
1349+
| 2135 |[Count Words Obtained After Adding a Letter](src/main/java/g2101_2200/s2135_count_words_obtained_after_adding_a_letter)| Medium | Array, String, Hash_Table, Sorting, Bit_Manipulation | 67 | 93.08
1350+
| 2134 |[Minimum Swaps to Group All 1's Together II](src/main/java/g2101_2200/s2134_minimum_swaps_to_group_all_1s_together_ii)| Medium | Array, Sliding_Window | 11 | 72.59
1351+
| 2133 |[Check if Every Row and Column Contains All Numbers](src/main/java/g2101_2200/s2133_check_if_every_row_and_column_contains_all_numbers)| Easy | Array, Hash_Table, Matrix | 32 | 64.12
1352+
| 2132 |[Stamping the Grid](src/main/java/g2101_2200/s2132_stamping_the_grid)| Hard | Array, Greedy, Matrix, Prefix_Sum | 11 | 93.06
1353+
| 2131 |[Longest Palindrome by Concatenating Two Letter Words](src/main/java/g2101_2200/s2131_longest_palindrome_by_concatenating_two_letter_words)| Medium | Array, String, Hash_Table, Greedy, Counting | 73 | 76.60
1354+
| 2130 |[Maximum Twin Sum of a Linked List](src/main/java/g2101_2200/s2130_maximum_twin_sum_of_a_linked_list)| Medium | Two_Pointers, Stack, Linked_List | 9 | 57.92
1355+
| 2129 |[Capitalize the Title](src/main/java/g2101_2200/s2129_capitalize_the_title)| Easy | String | 2 | 94.22
1356+
| 2127 |[Maximum Employees to Be Invited to a Meeting](src/main/java/g2101_2200/s2127_maximum_employees_to_be_invited_to_a_meeting)| Hard | Depth_First_Search, Graph, Topological_Sort | 37 | 85.71
1357+
| 2126 |[Destroying Asteroids](src/main/java/g2101_2200/s2126_destroying_asteroids)| Medium | Array, Sorting, Greedy | 25 | 83.82
1358+
| 2125 |[Number of Laser Beams in a Bank](src/main/java/g2101_2200/s2125_number_of_laser_beams_in_a_bank)| Medium | Array, String, Math, Matrix | 19 | 76.00
1359+
| 2124 |[Check if All A's Appears Before All B's](src/main/java/g2101_2200/s2124_check_if_all_as_appears_before_all_bs)| Easy | String | 1 | 73.82
1360+
| 2122 |[Recover the Original Array](src/main/java/g2101_2200/s2122_recover_the_original_array)| Hard | Array, Hash_Table, Sorting, Enumeration | 19 | 89.04
1361+
| 2121 |[Intervals Between Identical Elements](src/main/java/g2101_2200/s2121_intervals_between_identical_elements)| Medium | Array, Hash_Table, Prefix_Sum | 93 | 79.63
1362+
| 2120 |[Execution of All Suffix Instructions Staying in a Grid](src/main/java/g2101_2200/s2120_execution_of_all_suffix_instructions_staying_in_a_grid)| Medium | String, Simulation | 31 | 88.00
13441363
| 2119 |[A Number After a Double Reversal](src/main/java/g2101_2200/s2119_a_number_after_a_double_reversal)| Easy | Math | 0 | 100.00
13451364
| 2117 |[Abbreviating the Product of a Range](src/main/java/g2101_2200/s2117_abbreviating_the_product_of_a_range)| Hard | Math | 70 | 89.47
13461365
| 2116 |[Check if a Parentheses String Can Be Valid](src/main/java/g2101_2200/s2116_check_if_a_parentheses_string_can_be_valid)| Medium | String, Greedy, Stack | 19 | 85.53
@@ -3016,5 +3035,5 @@
30163035
| 0004 |[Median of Two Sorted Arrays](src/main/java/g0001_0100/s0004_median_of_two_sorted_arrays)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Divide_and_Conquer | 3 | 79.08
30173036
| 0003 |[Longest Substring Without Repeating Characters](src/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Algorithm_I_Day_6_Sliding_Window | 3 | 95.99
30183037
| 0002 |[Add Two Numbers](src/main/java/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Data_Structure_II_Day_10_Linked_List, Programming_Skills_II_Day_15 | 1 | 100.00
3019-
| 0001 |[Two Sum](src/main/java/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Data_Structure_I_Day_2_Array | 3 | 80.08
3038+
| 0001 |[Two Sum](src/main/java/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Data_Structure_I_Day_2_Array | 1 | 99.68
30203039

src/main/java/g0001_0100/s0001_two_sum/readme.md

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,45 +40,20 @@ You can return the answer in any order.
4040
## Solution
4141

4242
```java
43-
import java.util.Arrays;
43+
import java.util.HashMap;
44+
import java.util.Map;
4445

4546
public class Solution {
46-
public int[] twoSum(int[] nums, int target) {
47-
if (nums == null || nums.length <= 1) {
48-
return new int[0];
49-
}
50-
int[] result = new int[2];
51-
int left = 0;
52-
int right = nums.length - 1;
53-
int[] nums1 = Arrays.copyOf(nums, nums.length);
54-
Arrays.sort(nums1);
55-
while (left < right) {
56-
if (nums1[left] + nums1[right] == target) {
57-
break;
58-
} else if (nums1[left] + nums1[right] > target) {
59-
right--;
60-
} else if (nums1[left] + nums1[right] < target) {
61-
left++;
62-
}
63-
}
64-
65-
for (int i = 0; i < nums.length; i++) {
66-
if (nums1[left] == nums[i]) {
67-
result[0] = i;
68-
break;
47+
public int[] twoSum(int[] numbers, int target) {
48+
Map<Integer, Integer> indexMap = new HashMap<>();
49+
for (int i = 0; i < numbers.length; i++) {
50+
Integer requiredNum = (Integer) (target - numbers[i]);
51+
if (indexMap.containsKey(requiredNum)) {
52+
return new int[] {indexMap.get(requiredNum), i};
6953
}
54+
indexMap.put(numbers[i], i);
7055
}
71-
for (int j = nums.length - 1; j >= 0; j--) {
72-
if (nums1[right] == nums[j]) {
73-
result[1] = j;
74-
break;
75-
}
76-
}
77-
78-
int tmp = result[0];
79-
result[0] = Math.min(result[0], result[1]);
80-
result[1] = Math.max(tmp, result[1]);
81-
return result;
56+
return new int[] {-1, -1};
8257
}
8358
}
8459
```
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
## 2120\. Execution of All Suffix Instructions Staying in a Grid
2+
3+
Medium
4+
5+
There is an `n x n` grid, with the top-left cell at `(0, 0)` and the bottom-right cell at `(n - 1, n - 1)`. You are given the integer `n` and an integer array `startPos` where <code>startPos = [start<sub>row</sub>, start<sub>col</sub>]</code> indicates that a robot is initially at cell <code>(start<sub>row</sub>, start<sub>col</sub>)</code>.
6+
7+
You are also given a **0-indexed** string `s` of length `m` where `s[i]` is the <code>i<sup>th</sup></code> instruction for the robot: `'L'` (move left), `'R'` (move right), `'U'` (move up), and `'D'` (move down).
8+
9+
The robot can begin executing from any <code>i<sup>th</sup></code> instruction in `s`. It executes the instructions one by one towards the end of `s` but it stops if either of these conditions is met:
10+
11+
* The next instruction will move the robot off the grid.
12+
* There are no more instructions left to execute.
13+
14+
Return _an array_ `answer` _of length_ `m` _where_ `answer[i]` _is **the number of instructions** the robot can execute if the robot **begins executing from** the_ <code>i<sup>th</sup></code> _instruction in_ `s`.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2021/12/09/1.png)
19+
20+
**Input:** n = 3, startPos = [0,1], s = "RRDDLU"
21+
22+
**Output:** [1,5,4,3,1,0]
23+
24+
**Explanation:** Starting from startPos and beginning execution from the i<sup>th</sup> instruction:
25+
26+
- 0<sup>th</sup>: "**R**RDDLU". Only one instruction "R" can be executed before it moves off the grid.
27+
28+
- 1<sup>st</sup>: "**RDDLU**". All five instructions can be executed while it stays in the grid and ends at (1, 1).
29+
30+
- 2<sup>nd</sup>: "**DDLU**". All four instructions can be executed while it stays in the grid and ends at (1, 0).
31+
32+
- 3<sup>rd</sup>: "**DLU**". All three instructions can be executed while it stays in the grid and ends at (0, 0).
33+
34+
- 4<sup>th</sup>: "**L**U". Only one instruction "L" can be executed before it moves off the grid.
35+
36+
- 5<sup>th</sup>: "U". If moving up, it would move off the grid.
37+
38+
**Example 2:**
39+
40+
![](https://assets.leetcode.com/uploads/2021/12/09/2.png)
41+
42+
**Input:** n = 2, startPos = [1,1], s = "LURD"
43+
44+
**Output:** [4,1,0,0]
45+
46+
**Explanation:**
47+
48+
- 0<sup>th</sup>: "**LURD**".
49+
50+
- 1<sup>st</sup>: "**U**RD".
51+
52+
- 2<sup>nd</sup>: "RD".
53+
54+
- 3<sup>rd</sup>: "D".
55+
56+
**Example 3:**
57+
58+
![](https://assets.leetcode.com/uploads/2021/12/09/3.png)
59+
60+
**Input:** n = 1, startPos = [0,0], s = "LRUD"
61+
62+
**Output:** [0,0,0,0]
63+
64+
**Explanation:** No matter which instruction the robot begins execution from, it would move off the grid.
65+
66+
**Constraints:**
67+
68+
* `m == s.length`
69+
* `1 <= n, m <= 500`
70+
* `startPos.length == 2`
71+
* <code>0 <= start<sub>row</sub>, start<sub>col</sub> < n</code>
72+
* `s` consists of `'L'`, `'R'`, `'U'`, and `'D'`.
73+
74+
## Solution
75+
76+
```java
77+
@SuppressWarnings("java:S135")
78+
public class Solution {
79+
public int[] executeInstructions(int n, int[] startPos, String s) {
80+
int[] answer = new int[s.length()];
81+
for (int i = 0; i < s.length(); i++) {
82+
int count = 0;
83+
int currX = startPos[0];
84+
int currY = startPos[1];
85+
for (int j = i; j < s.length(); j++) {
86+
char mv = s.charAt(j);
87+
if (mv == 'R') {
88+
currY++;
89+
if (currY > n - 1) {
90+
break;
91+
}
92+
} else if (mv == 'D') {
93+
currX++;
94+
if (currX > n - 1) {
95+
break;
96+
}
97+
} else if (mv == 'L') {
98+
currY--;
99+
if (currY < 0) {
100+
break;
101+
}
102+
} else if (mv == 'U') {
103+
currX--;
104+
if (currX < 0) {
105+
break;
106+
}
107+
}
108+
count++;
109+
}
110+
answer[i] = count;
111+
}
112+
return answer;
113+
}
114+
}
115+
```
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
## 2121\. Intervals Between Identical Elements
2+
3+
Medium
4+
5+
You are given a **0-indexed** array of `n` integers `arr`.
6+
7+
The **interval** between two elements in `arr` is defined as the **absolute difference** between their indices. More formally, the **interval** between `arr[i]` and `arr[j]` is `|i - j|`.
8+
9+
Return _an array_ `intervals` _of length_ `n` _where_ `intervals[i]` _is **the sum of intervals** between_ `arr[i]` _and each element in_ `arr` _with the same value as_ `arr[i]`_._
10+
11+
**Note:** `|x|` is the absolute value of `x`.
12+
13+
**Example 1:**
14+
15+
**Input:** arr = [2,1,3,1,2,3,3]
16+
17+
**Output:** [4,2,7,2,4,4,5]
18+
19+
**Explanation:**
20+
21+
- Index 0: Another 2 is found at index 4. |0 - 4| = 4
22+
23+
- Index 1: Another 1 is found at index 3. |1 - 3| = 2
24+
25+
- Index 2: Two more 3s are found at indices 5 and 6. |2 - 5| + |2 - 6| = 7
26+
27+
- Index 3: Another 1 is found at index 1. |3 - 1| = 2
28+
29+
- Index 4: Another 2 is found at index 0. |4 - 0| = 4
30+
31+
- Index 5: Two more 3s are found at indices 2 and 6. |5 - 2| + |5 - 6| = 4
32+
33+
- Index 6: Two more 3s are found at indices 2 and 5. |6 - 2| + |6 - 5| = 5
34+
35+
**Example 2:**
36+
37+
**Input:** arr = [10,5,10,10]
38+
39+
**Output:** [5,0,3,4]
40+
41+
**Explanation:**
42+
43+
- Index 0: Two more 10s are found at indices 2 and 3. |0 - 2| + |0 - 3| = 5
44+
45+
- Index 1: There is only one 5 in the array, so its sum of intervals to identical elements is 0.
46+
47+
- Index 2: Two more 10s are found at indices 0 and 3. |2 - 0| + |2 - 3| = 3
48+
49+
- Index 3: Two more 10s are found at indices 0 and 2. |3 - 0| + |3 - 2| = 4
50+
51+
**Constraints:**
52+
53+
* `n == arr.length`
54+
* <code>1 <= n <= 10<sup>5</sup></code>
55+
* <code>1 <= arr[i] <= 10<sup>5</sup></code>
56+
57+
## Solution
58+
59+
```java
60+
import java.util.ArrayList;
61+
import java.util.Arrays;
62+
import java.util.HashMap;
63+
import java.util.List;
64+
import java.util.Map;
65+
66+
public class Solution {
67+
public long[] getDistances(int[] a) {
68+
int n = a.length;
69+
Map<Integer, List<Integer>> map = new HashMap<>();
70+
for (int i = 0; i < n; i++) {
71+
List<Integer> list = map.get(a[i]);
72+
if (list == null) {
73+
list = new ArrayList<>();
74+
}
75+
list.add(i);
76+
map.put(a[i], list);
77+
}
78+
long[] ans = new long[n];
79+
Arrays.fill(ans, 0);
80+
for (List<Integer> list : map.values()) {
81+
long sum = 0;
82+
int first = list.get(0);
83+
for (int i = 1; i < list.size(); i++) {
84+
sum = sum + list.get(i) - first;
85+
}
86+
ans[first] = sum;
87+
int prevElements = 0;
88+
int nextElements = list.size() - 2;
89+
for (int i = 1; i < list.size(); i++) {
90+
int diff = list.get(i) - list.get(i - 1);
91+
sum = sum + (long) diff * (prevElements - nextElements);
92+
ans[list.get(i)] = sum;
93+
prevElements++;
94+
nextElements--;
95+
}
96+
}
97+
return ans;
98+
}
99+
}
100+
```

0 commit comments

Comments
 (0)