Skip to content

Commit e32aa63

Browse files
authored
Added tasks 2045, 2047, 2048, 2049, 2050.
1 parent 7c38e1d commit e32aa63

File tree

15 files changed

+685
-0
lines changed

15 files changed

+685
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g2001_2100.s2045_second_minimum_time_to_reach_destination;
2+
3+
// #Hard #Breadth_First_Search #Graph #Shortest_Path
4+
// #2022_05_26_Time_65_ms_(74.03%)_Space_51.6_MB_(97.24%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
import java.util.Queue;
11+
12+
public class Solution {
13+
public int secondMinimum(int n, int[][] edges, int time, int change) {
14+
List<Integer>[] adj = new ArrayList[n];
15+
for (int i = 0; i < adj.length; i++) {
16+
adj[i] = new ArrayList<>();
17+
}
18+
for (int[] edge : edges) {
19+
int p = edge[0] - 1;
20+
int q = edge[1] - 1;
21+
adj[p].add(q);
22+
adj[q].add(p);
23+
}
24+
int[] dis1 = new int[n];
25+
int[] dis2 = new int[n];
26+
27+
Arrays.fill(dis1, Integer.MAX_VALUE);
28+
Arrays.fill(dis2, Integer.MAX_VALUE);
29+
dis1[0] = 0;
30+
Queue<int[]> queue = new LinkedList<>();
31+
queue.offer(new int[] {0, 0});
32+
while (!queue.isEmpty()) {
33+
int[] temp = queue.poll();
34+
int cur = temp[0];
35+
int path = temp[1];
36+
37+
for (int node : adj[cur]) {
38+
int newPath = path + 1;
39+
if (newPath < dis1[node]) {
40+
dis2[node] = dis1[node];
41+
dis1[node] = newPath;
42+
queue.offer(new int[] {node, newPath});
43+
} else if ((newPath > dis1[node]) && (newPath < dis2[node])) {
44+
dis2[node] = newPath;
45+
queue.offer(new int[] {node, newPath});
46+
}
47+
}
48+
}
49+
return helper(dis2[n - 1], time, change);
50+
}
51+
52+
private int helper(int pathValue, int time, int change) {
53+
int sum = 0;
54+
for (int i = 0; i < pathValue; i++) {
55+
sum += time;
56+
if (i == pathValue - 1) {
57+
break;
58+
}
59+
if ((sum / change) % 2 == 0) {
60+
continue;
61+
}
62+
sum = (sum / change + 1) * change;
63+
}
64+
return sum;
65+
}
66+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2045\. Second Minimum Time to Reach Destination
2+
3+
Hard
4+
5+
A city is represented as a **bi-directional connected** graph with `n` vertices where each vertex is labeled from `1` to `n` (**inclusive**). The edges in the graph are represented as a 2D integer array `edges`, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by **at most one** edge, and no vertex has an edge to itself. The time taken to traverse any edge is `time` minutes.
6+
7+
Each vertex has a traffic signal which changes its color from **green** to **red** and vice versa every `change` minutes. All signals change **at the same time**. You can enter a vertex at **any time**, but can leave a vertex **only when the signal is green**. You **cannot wait** at a vertex if the signal is **green**.
8+
9+
The **second minimum value** is defined as the smallest value **strictly larger** than the minimum value.
10+
11+
* For example the second minimum value of `[2, 3, 4]` is `3`, and the second minimum value of `[2, 2, 4]` is `4`.
12+
13+
Given `n`, `edges`, `time`, and `change`, return _the **second minimum time** it will take to go from vertex_ `1` _to vertex_ `n`.
14+
15+
**Notes:**
16+
17+
* You can go through any vertex **any** number of times, **including** `1` and `n`.
18+
* You can assume that when the journey **starts**, all signals have just turned **green**.
19+
20+
**Example 1:**
21+
22+
![](https://assets.leetcode.com/uploads/2021/09/29/e1.png)         ![](https://assets.leetcode.com/uploads/2021/09/29/e2.png)
23+
24+
**Input:** n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5
25+
26+
**Output:** 13
27+
28+
**Explanation:**
29+
30+
The figure on the left shows the given graph.
31+
32+
The blue path in the figure on the right is the minimum time path.
33+
34+
The time taken is:
35+
36+
- Start at 1, time elapsed=0
37+
38+
- 1 -> 4: 3 minutes, time elapsed=3
39+
40+
- 4 -> 5: 3 minutes, time elapsed=6
41+
42+
Hence the minimum time needed is 6 minutes.
43+
44+
The red path shows the path to get the second minimum time.
45+
46+
- Start at 1, time elapsed=0
47+
48+
- 1 -> 3: 3 minutes, time elapsed=3
49+
50+
- 3 -> 4: 3 minutes, time elapsed=6
51+
52+
- Wait at 4 for 4 minutes, time elapsed=10
53+
54+
- 4 -> 5: 3 minutes, time elapsed=13
55+
56+
Hence the second minimum time is 13 minutes.
57+
58+
**Example 2:**
59+
60+
![](https://assets.leetcode.com/uploads/2021/09/29/eg2.png)
61+
62+
**Input:** n = 2, edges = [[1,2]], time = 3, change = 2
63+
64+
**Output:** 11
65+
66+
**Explanation:**
67+
68+
The minimum time path is 1 -> 2 with time = 3 minutes.
69+
70+
The second minimum time path is 1 -> 2 -> 1 -> 2 with time = 11 minutes.
71+
72+
**Constraints:**
73+
74+
* <code>2 <= n <= 10<sup>4</sup></code>
75+
* <code>n - 1 <= edges.length <= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code>
76+
* `edges[i].length == 2`
77+
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
78+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
79+
* There are no duplicate edges.
80+
* Each vertex can be reached directly or indirectly from every other vertex.
81+
* <code>1 <= time, change <= 10<sup>3</sup></code>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g2001_2100.s2047_number_of_valid_words_in_a_sentence;
2+
3+
// #Easy #String #2022_05_26_Time_19_ms_(42.57%)_Space_44.9_MB_(37.62%)
4+
5+
public class Solution {
6+
public int countValidWords(String sentence) {
7+
String[] tokens = sentence.split("\\s+");
8+
int count = 0;
9+
for (String token : tokens) {
10+
int hyphenCount = 0;
11+
int punctuationMarkCount = 0;
12+
boolean valid = true;
13+
if (token.isEmpty() || token.equals("") || token.length() == 0) {
14+
continue;
15+
}
16+
for (int i = 0; i < token.length(); i++) {
17+
if (token.charAt(i) == '-') {
18+
hyphenCount++;
19+
if (hyphenCount > 1
20+
|| i == 0
21+
|| i == token.length() - 1
22+
|| !Character.isAlphabetic(token.charAt(i - 1))
23+
|| !Character.isAlphabetic(token.charAt(i + 1))) {
24+
valid = false;
25+
break;
26+
}
27+
} else if (token.charAt(i) == '!'
28+
|| token.charAt(i) == '.'
29+
|| token.charAt(i) == ',') {
30+
punctuationMarkCount++;
31+
if (punctuationMarkCount > 1 || i != token.length() - 1) {
32+
valid = false;
33+
break;
34+
}
35+
} else if (Character.isDigit(token.charAt(i))) {
36+
valid = false;
37+
break;
38+
}
39+
}
40+
if (valid) {
41+
count++;
42+
}
43+
}
44+
return count;
45+
}
46+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2047\. Number of Valid Words in a Sentence
2+
3+
Easy
4+
5+
A sentence consists of lowercase letters (`'a'` to `'z'`), digits (`'0'` to `'9'`), hyphens (`'-'`), punctuation marks (`'!'`, `'.'`, and `','`), and spaces (`' '`) only. Each sentence can be broken down into **one or more tokens** separated by one or more spaces `' '`.
6+
7+
A token is a valid word if **all three** of the following are true:
8+
9+
* It only contains lowercase letters, hyphens, and/or punctuation (**no** digits).
10+
* There is **at most one** hyphen `'-'`. If present, it **must** be surrounded by lowercase characters (`"a-b"` is valid, but `"-ab"` and `"ab-"` are not valid).
11+
* There is **at most one** punctuation mark. If present, it **must** be at the **end** of the token (`"ab,"`, `"cd!"`, and `"."` are valid, but `"a!b"` and `"c.,"` are not valid).
12+
13+
Examples of valid words include `"a-b."`, `"afad"`, `"ba-c"`, `"a!"`, and `"!"`.
14+
15+
Given a string `sentence`, return _the **number** of valid words in_ `sentence`.
16+
17+
**Example 1:**
18+
19+
**Input:** sentence = "cat and dog"
20+
21+
**Output:** 3
22+
23+
**Explanation:** The valid words in the sentence are "cat", "and", and "dog".
24+
25+
**Example 2:**
26+
27+
**Input:** sentence = "!this 1-s b8d!"
28+
29+
**Output:** 0
30+
31+
**Explanation:** There are no valid words in the sentence.
32+
33+
"!this" is invalid because it starts with a punctuation mark.
34+
35+
"1-s" and "b8d" are invalid because they contain digits.
36+
37+
**Example 3:**
38+
39+
**Input:** sentence = "alice and bob are playing stone-game10"
40+
41+
**Output:** 5
42+
43+
**Explanation:** The valid words in the sentence are "alice", "and", "bob", "are", and "playing". "stone-game10" is invalid because it contains digits.
44+
45+
**Constraints:**
46+
47+
* `1 <= sentence.length <= 1000`
48+
* `sentence` only contains lowercase English letters, digits, `' '`, `'-'`, `'!'`, `'.'`, and `','`.
49+
* There will be at least `1` token.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g2001_2100.s2048_next_greater_numerically_balanced_number;
2+
3+
// #Medium #Math #Backtracking #Enumeration #2022_05_26_Time_2_ms_(95.19%)_Space_41.8_MB_(72.12%)
4+
5+
public class Solution {
6+
public int nextBeautifulNumber(int n) {
7+
int[] arr = new int[] {0, 1, 2, 3, 4, 5, 6};
8+
boolean[] select = new boolean[7];
9+
int d = n == 0 ? 1 : (int) Math.log10(n) + 1;
10+
return solve(1, n, d, 0, select, arr);
11+
}
12+
13+
private int solve(int i, int n, int d, int sz, boolean[] select, int[] arr) {
14+
if (sz > d + 1) {
15+
return Integer.MAX_VALUE;
16+
}
17+
if (i == select.length) {
18+
return sz >= d ? make(0, n, sz, select, arr) : Integer.MAX_VALUE;
19+
}
20+
int ans = solve(i + 1, n, d, sz, select, arr);
21+
select[i] = true;
22+
ans = Math.min(ans, solve(i + 1, n, d, sz + i, select, arr));
23+
select[i] = false;
24+
return ans;
25+
}
26+
27+
private int make(int cur, int n, int end, boolean[] select, int[] arr) {
28+
if (end == 0) {
29+
return cur > n ? cur : Integer.MAX_VALUE;
30+
}
31+
32+
int ans = Integer.MAX_VALUE;
33+
for (int j = 1; j < arr.length; j++) {
34+
if (!select[j] || arr[j] == 0) {
35+
continue;
36+
}
37+
--arr[j];
38+
ans = Math.min(make(10 * cur + j, n, end - 1, select, arr), ans);
39+
++arr[j];
40+
}
41+
return ans;
42+
}
43+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2048\. Next Greater Numerically Balanced Number
2+
3+
Medium
4+
5+
An integer `x` is **numerically balanced** if for every digit `d` in the number `x`, there are **exactly** `d` occurrences of that digit in `x`.
6+
7+
Given an integer `n`, return _the **smallest numerically balanced** number **strictly greater** than_ `n`_._
8+
9+
**Example 1:**
10+
11+
**Input:** n = 1
12+
13+
**Output:** 22
14+
15+
**Explanation:**
16+
17+
22 is numerically balanced since:
18+
19+
- The digit 2 occurs 2 times.
20+
21+
It is also the smallest numerically balanced number strictly greater than 1.
22+
23+
**Example 2:**
24+
25+
**Input:** n = 1000
26+
27+
**Output:** 1333
28+
29+
**Explanation:**
30+
31+
1333 is numerically balanced since:
32+
33+
- The digit 1 occurs 1 time.
34+
35+
- The digit 3 occurs 3 times.
36+
37+
It is also the smallest numerically balanced number strictly greater than 1000. Note that 1022 cannot be the answer because 0 appeared more than 0 times.
38+
39+
**Example 3:**
40+
41+
**Input:** n = 3000
42+
43+
**Output:** 3133
44+
45+
**Explanation:**
46+
47+
3133 is numerically balanced since:
48+
49+
- The digit 1 occurs 1 time.
50+
51+
- The digit 3 occurs 3 times.
52+
53+
It is also the smallest numerically balanced number strictly greater than 3000.
54+
55+
**Constraints:**
56+
57+
* <code>0 <= n <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)