Skip to content

Commit 78f73f2

Browse files
authored
Added tasks 2451, 2452, 2453, 2454, 2455.
1 parent 5c16e0d commit 78f73f2

File tree

15 files changed

+487
-0
lines changed

15 files changed

+487
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g2401_2500.s2451_odd_string_difference;
2+
3+
// #Easy #String #Hash_Table #Math #2022_12_15_Time_0_ms_(100.00%)_Space_40_MB_(91.61%)
4+
5+
public class Solution {
6+
public String oddString(String[] w) {
7+
int n = w[0].length() - 1;
8+
int[] x = new int[n];
9+
int s = 1;
10+
int y = 0;
11+
int index = 1;
12+
for (int i = 0; i < n; i++) {
13+
x[i] = w[0].charAt(i + 1) - w[0].charAt(i);
14+
}
15+
for (int i = 1; y * s == 0 || s + y < 3; i++) {
16+
boolean b = true;
17+
for (int j = 0; j < n; j++) {
18+
if (x[j] != w[i].charAt(j + 1) - w[i].charAt(j)) {
19+
b = false;
20+
break;
21+
}
22+
}
23+
if (b) {
24+
s++;
25+
} else {
26+
y++;
27+
index = i;
28+
}
29+
}
30+
return s == 1 ? w[0] : w[index];
31+
}
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2451\. Odd String Difference
2+
3+
Easy
4+
5+
You are given an array of equal-length strings `words`. Assume that the length of each string is `n`.
6+
7+
Each string `words[i]` can be converted into a **difference integer array** `difference[i]` of length `n - 1` where `difference[i][j] = words[i][j+1] - words[i][j]` where `0 <= j <= n - 2`. Note that the difference between two letters is the difference between their **positions** in the alphabet i.e. the position of `'a'` is `0`, `'b'` is `1`, and `'z'` is `25`.
8+
9+
* For example, for the string `"acb"`, the difference integer array is `[2 - 0, 1 - 2] = [2, -1]`.
10+
11+
All the strings in words have the same difference integer array, **except one**. You should find that string.
12+
13+
Return _the string in_ `words` _that has different **difference integer array**._
14+
15+
**Example 1:**
16+
17+
**Input:** words = ["adc","wzy","abc"]
18+
19+
**Output:** "abc"
20+
21+
**Explanation:**
22+
23+
- The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1].
24+
25+
- The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1].
26+
27+
- The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1].
28+
29+
The odd array out is [1, 1], so we return the corresponding string, "abc".
30+
31+
**Example 2:**
32+
33+
**Input:** words = ["aaa","bob","ccc","ddd"]
34+
35+
**Output:** "bob"
36+
37+
**Explanation:** All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
38+
39+
**Constraints:**
40+
41+
* `3 <= words.length <= 100`
42+
* `n == words[i].length`
43+
* `2 <= n <= 20`
44+
* `words[i]` consists of lowercase English letters.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g2401_2500.s2452_words_within_two_edits_of_dictionary;
2+
3+
// #Medium #Array #String #2022_12_15_Time_16_ms_(70.33%)_Space_49.2_MB_(14.26%)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private Node root;
11+
12+
static class Node {
13+
HashMap<Character, Node> childs = new HashMap<>();
14+
}
15+
16+
private void insert(String s) {
17+
Node curr = root;
18+
for (char ch : s.toCharArray()) {
19+
if (curr.childs.get(ch) == null) {
20+
curr.childs.put(ch, new Node());
21+
}
22+
curr = curr.childs.get(ch);
23+
}
24+
}
25+
26+
private boolean search(String word, Node curr, int i, int edits) {
27+
// if reached the end with less than or equal 2 edits then return truem
28+
if (i == word.length()) {
29+
return edits <= 2;
30+
}
31+
// more than 2 mismatch don't go further
32+
if (edits > 2) {
33+
return false;
34+
}
35+
// there might be a case start is matching but others are diff and that's a edge case to
36+
// handle
37+
boolean ans = false;
38+
for (Character ch : curr.childs.keySet()) {
39+
ans |=
40+
search(
41+
word,
42+
curr.childs.get(ch),
43+
i + 1,
44+
ch == word.charAt(i) ? edits : edits + 1);
45+
}
46+
return ans;
47+
}
48+
49+
public List<String> twoEditWords(String[] queries, String[] dictionary) {
50+
root = new Node();
51+
for (String s : dictionary) {
52+
insert(s);
53+
}
54+
List<String> ans = new ArrayList<>();
55+
for (String s : queries) {
56+
boolean found = search(s, root, 0, 0);
57+
if (found) {
58+
ans.add(s);
59+
}
60+
}
61+
return ans;
62+
}
63+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2452\. Words Within Two Edits of Dictionary
2+
3+
Medium
4+
5+
You are given two string arrays, `queries` and `dictionary`. All words in each array comprise of lowercase English letters and have the same length.
6+
7+
In one **edit** you can take a word from `queries`, and change any letter in it to any other letter. Find all words from `queries` that, after a **maximum** of two edits, equal some word from `dictionary`.
8+
9+
Return _a list of all words from_ `queries`_,_ _that match with some word from_ `dictionary` _after a maximum of **two edits**_. Return the words in the **same order** they appear in `queries`.
10+
11+
**Example 1:**
12+
13+
**Input:** queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"]
14+
15+
**Output:** ["word","note","wood"]
16+
17+
**Explanation:**
18+
19+
- Changing the 'r' in "word" to 'o' allows it to equal the dictionary word "wood".
20+
21+
- Changing the 'n' to 'j' and the 't' to 'k' in "note" changes it to "joke".
22+
23+
- It would take more than 2 edits for "ants" to equal a dictionary word.
24+
25+
- "wood" can remain unchanged (0 edits) and match the corresponding dictionary word.
26+
27+
Thus, we return ["word","note","wood"].
28+
29+
**Example 2:**
30+
31+
**Input:** queries = ["yes"], dictionary = ["not"]
32+
33+
**Output:** []
34+
35+
**Explanation:**
36+
37+
Applying any two edits to "yes" cannot make it equal to "not". Thus, we return an empty array.
38+
39+
**Constraints:**
40+
41+
* `1 <= queries.length, dictionary.length <= 100`
42+
* `n == queries[i].length == dictionary[j].length`
43+
* `1 <= n <= 100`
44+
* All `queries[i]` and `dictionary[j]` are composed of lowercase English letters.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2401_2500.s2453_destroy_sequential_targets;
2+
3+
// #Medium #Array #Hash_Table #Counting #2022_12_15_Time_33_ms_(96.27%)_Space_58.9_MB_(95.38%)
4+
5+
import java.util.HashMap;
6+
7+
public class Solution {
8+
public int destroyTargets(int[] nums, int space) {
9+
HashMap<Integer, Integer> map = new HashMap<>();
10+
for (int num : nums) {
11+
int reminder = num % space;
12+
int freq = map.getOrDefault(reminder, 0);
13+
map.put(reminder, freq + 1);
14+
}
15+
int maxCount = 0;
16+
int ans = Integer.MAX_VALUE;
17+
for (int count : map.values()) {
18+
maxCount = Math.max(count, maxCount);
19+
}
20+
for (int val : nums) {
21+
if (map.get(val % space) == maxCount) {
22+
ans = Math.min(ans, val);
23+
}
24+
}
25+
return ans;
26+
}
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2453\. Destroy Sequential Targets
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `nums` consisting of positive integers, representing targets on a number line. You are also given an integer `space`.
6+
7+
You have a machine which can destroy targets. **Seeding** the machine with some `nums[i]` allows it to destroy all targets with values that can be represented as `nums[i] + c * space`, where `c` is any non-negative integer. You want to destroy the **maximum** number of targets in `nums`.
8+
9+
Return _the **minimum value** of_ `nums[i]` _you can seed the machine with to destroy the maximum number of targets._
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,7,8,1,1,5], space = 2
14+
15+
**Output:** 1
16+
17+
**Explanation:** If we seed the machine with nums[3], then we destroy all targets equal to 1,3,5,7,9,... In this case, we would destroy 5 total targets (all except for nums[2]). It is impossible to destroy more than 5 targets, so we return nums[3].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,3,5,2,4,6], space = 2
22+
23+
**Output:** 1
24+
25+
**Explanation:** Seeding the machine with nums[0], or nums[3] destroys 3 targets. It is not possible to destroy more than 3 targets. Since nums[0] is the minimal integer that can destroy 3 targets, we return 1.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [6,2,5], space = 100
30+
31+
**Output:** 2
32+
33+
**Explanation:** Whatever initial seed we select, we can only destroy 1 target. The minimal seed is nums[1].
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
39+
* <code>1 <= space <= 10<sup>9</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2401_2500.s2454_next_greater_element_iv;
2+
3+
// #Hard #Array #Sorting #Binary_Search #Stack #Monotonic_Stack #Heap_(Priority_Queue)
4+
// #2022_12_15_Time_29_ms_(95.84%)_Space_57.4_MB_(93.32%)
5+
6+
import java.util.ArrayDeque;
7+
import java.util.Arrays;
8+
import java.util.Deque;
9+
10+
public class Solution {
11+
public int[] secondGreaterElement(int[] nums) {
12+
int[] res = new int[nums.length];
13+
Arrays.fill(res, -1);
14+
Deque<Integer> stack1 = new ArrayDeque<>();
15+
Deque<Integer> stack2 = new ArrayDeque<>();
16+
Deque<Integer> tmp = new ArrayDeque<>();
17+
for (int i = 0; i < nums.length; i++) {
18+
while (!stack2.isEmpty() && nums[i] > nums[stack2.peek()]) {
19+
res[stack2.pop()] = nums[i];
20+
}
21+
while (!stack1.isEmpty() && nums[i] > nums[stack1.peek()]) {
22+
tmp.push(stack1.pop());
23+
}
24+
while (!tmp.isEmpty()) {
25+
stack2.push(tmp.pop());
26+
}
27+
stack1.push(i);
28+
}
29+
return res;
30+
}
31+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2454\. Next Greater Element IV
2+
3+
Hard
4+
5+
You are given a **0-indexed** array of non-negative integers `nums`. For each integer in `nums`, you must find its respective **second greater** integer.
6+
7+
The **second greater** integer of `nums[i]` is `nums[j]` such that:
8+
9+
* `j > i`
10+
* `nums[j] > nums[i]`
11+
* There exists **exactly one** index `k` such that `nums[k] > nums[i]` and `i < k < j`.
12+
13+
If there is no such `nums[j]`, the second greater integer is considered to be `-1`.
14+
15+
* For example, in the array `[1, 2, 4, 3]`, the second greater integer of `1` is `4`, `2` is `3`, and that of `3` and `4` is `-1`.
16+
17+
Return _an integer array_ `answer`_, where_ `answer[i]` _is the second greater integer of_ `nums[i]`_._
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [2,4,0,9,6]
22+
23+
**Output:** [9,6,6,-1,-1]
24+
25+
**Explanation:**
26+
27+
0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.
28+
29+
1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.
30+
31+
2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.
32+
33+
3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.
34+
35+
4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.
36+
37+
Thus, we return [9,6,6,-1,-1].
38+
39+
**Example 2:**
40+
41+
**Input:** nums = [3,3]
42+
43+
**Output:** [-1,-1]
44+
45+
**Explanation:** We return [-1,-1] since neither integer has any integer greater than it.
46+
47+
**Constraints:**
48+
49+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
50+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2401_2500.s2455_average_value_of_even_numbers_that_are_divisible_by_three;
2+
3+
// #Easy #Array #Math #2022_12_15_Time_1_ms_(100.00%)_Space_42.6_MB_(82.41%)
4+
5+
public class Solution {
6+
public int averageValue(int[] nums) {
7+
int count = 0;
8+
int sum = 0;
9+
for (int num : nums) {
10+
if (num % 2 == 0 && num % 3 == 0) {
11+
count++;
12+
sum += num;
13+
}
14+
}
15+
if (count == 0) {
16+
return 0;
17+
}
18+
return sum / count;
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2455\. Average Value of Even Numbers That Are Divisible by Three
2+
3+
Easy
4+
5+
Given an integer array `nums` of **positive** integers, return _the average value of all even integers that are divisible by_ `3`_._
6+
7+
Note that the **average** of `n` elements is the **sum** of the `n` elements divided by `n` and **rounded down** to the nearest integer.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,6,10,12,15]
12+
13+
**Output:** 9
14+
15+
**Explanation:** 6 and 12 are even numbers that are divisible by 3. (6 + 12) / 2 = 9.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1,2,4,7,10]
20+
21+
**Output:** 0
22+
23+
**Explanation:** There is no single number that satisfies the requirement, so return 0.
24+
25+
**Constraints:**
26+
27+
* `1 <= nums.length <= 1000`
28+
* `1 <= nums[i] <= 1000`

0 commit comments

Comments
 (0)