Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package g2001_2100.s2045_second_minimum_time_to_reach_destination;

// #Hard #Breadth_First_Search #Graph #Shortest_Path
// #2022_05_26_Time_65_ms_(74.03%)_Space_51.6_MB_(97.24%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {
public int secondMinimum(int n, int[][] edges, int time, int change) {
List<Integer>[] adj = new ArrayList[n];
for (int i = 0; i < adj.length; i++) {
adj[i] = new ArrayList<>();
}
for (int[] edge : edges) {
int p = edge[0] - 1;
int q = edge[1] - 1;
adj[p].add(q);
adj[q].add(p);
}
int[] dis1 = new int[n];
int[] dis2 = new int[n];

Arrays.fill(dis1, Integer.MAX_VALUE);
Arrays.fill(dis2, Integer.MAX_VALUE);
dis1[0] = 0;
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[] {0, 0});
while (!queue.isEmpty()) {
int[] temp = queue.poll();
int cur = temp[0];
int path = temp[1];

for (int node : adj[cur]) {
int newPath = path + 1;
if (newPath < dis1[node]) {
dis2[node] = dis1[node];
dis1[node] = newPath;
queue.offer(new int[] {node, newPath});
} else if ((newPath > dis1[node]) && (newPath < dis2[node])) {
dis2[node] = newPath;
queue.offer(new int[] {node, newPath});
}
}
}
return helper(dis2[n - 1], time, change);
}

private int helper(int pathValue, int time, int change) {
int sum = 0;
for (int i = 0; i < pathValue; i++) {
sum += time;
if (i == pathValue - 1) {
break;
}
if ((sum / change) % 2 == 0) {
continue;
}
sum = (sum / change + 1) * change;
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
2045\. Second Minimum Time to Reach Destination

Hard

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.

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**.

The **second minimum value** is defined as the smallest value **strictly larger** than the minimum value.

* For example the second minimum value of `[2, 3, 4]` is `3`, and the second minimum value of `[2, 2, 4]` is `4`.

Given `n`, `edges`, `time`, and `change`, return _the **second minimum time** it will take to go from vertex_ `1` _to vertex_ `n`.

**Notes:**

* You can go through any vertex **any** number of times, **including** `1` and `n`.
* You can assume that when the journey **starts**, all signals have just turned **green**.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/09/29/e1.png)         ![](https://assets.leetcode.com/uploads/2021/09/29/e2.png)

**Input:** n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5

**Output:** 13

**Explanation:**

The figure on the left shows the given graph.

The blue path in the figure on the right is the minimum time path.

The time taken is:

- Start at 1, time elapsed=0

- 1 -> 4: 3 minutes, time elapsed=3

- 4 -> 5: 3 minutes, time elapsed=6

Hence the minimum time needed is 6 minutes.

The red path shows the path to get the second minimum time.

- Start at 1, time elapsed=0

- 1 -> 3: 3 minutes, time elapsed=3

- 3 -> 4: 3 minutes, time elapsed=6

- Wait at 4 for 4 minutes, time elapsed=10

- 4 -> 5: 3 minutes, time elapsed=13

Hence the second minimum time is 13 minutes.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/09/29/eg2.png)

**Input:** n = 2, edges = [[1,2]], time = 3, change = 2

**Output:** 11

**Explanation:**

The minimum time path is 1 -> 2 with time = 3 minutes.

The second minimum time path is 1 -> 2 -> 1 -> 2 with time = 11 minutes.

**Constraints:**

* <code>2 <= n <= 10<sup>4</sup></code>
* <code>n - 1 <= edges.length <= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code>
* `edges[i].length == 2`
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
* <code>u<sub>i</sub> != v<sub>i</sub></code>
* There are no duplicate edges.
* Each vertex can be reached directly or indirectly from every other vertex.
* <code>1 <= time, change <= 10<sup>3</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package g2001_2100.s2047_number_of_valid_words_in_a_sentence;

// #Easy #String #2022_05_26_Time_19_ms_(42.57%)_Space_44.9_MB_(37.62%)

public class Solution {
public int countValidWords(String sentence) {
String[] tokens = sentence.split("\\s+");
int count = 0;
for (String token : tokens) {
int hyphenCount = 0;
int punctuationMarkCount = 0;
boolean valid = true;
if (token.isEmpty() || token.equals("") || token.length() == 0) {
continue;
}
for (int i = 0; i < token.length(); i++) {
if (token.charAt(i) == '-') {
hyphenCount++;
if (hyphenCount > 1
|| i == 0
|| i == token.length() - 1
|| !Character.isAlphabetic(token.charAt(i - 1))
|| !Character.isAlphabetic(token.charAt(i + 1))) {
valid = false;
break;
}
} else if (token.charAt(i) == '!'
|| token.charAt(i) == '.'
|| token.charAt(i) == ',') {
punctuationMarkCount++;
if (punctuationMarkCount > 1 || i != token.length() - 1) {
valid = false;
break;
}
} else if (Character.isDigit(token.charAt(i))) {
valid = false;
break;
}
}
if (valid) {
count++;
}
}
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
2047\. Number of Valid Words in a Sentence

Easy

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 `' '`.

A token is a valid word if **all three** of the following are true:

* It only contains lowercase letters, hyphens, and/or punctuation (**no** digits).
* 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).
* 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).

Examples of valid words include `"a-b."`, `"afad"`, `"ba-c"`, `"a!"`, and `"!"`.

Given a string `sentence`, return _the **number** of valid words in_ `sentence`.

**Example 1:**

**Input:** sentence = "cat and dog"

**Output:** 3

**Explanation:** The valid words in the sentence are "cat", "and", and "dog".

**Example 2:**

**Input:** sentence = "!this 1-s b8d!"

**Output:** 0

**Explanation:** There are no valid words in the sentence.

"!this" is invalid because it starts with a punctuation mark.

"1-s" and "b8d" are invalid because they contain digits.

**Example 3:**

**Input:** sentence = "alice and bob are playing stone-game10"

**Output:** 5

**Explanation:** The valid words in the sentence are "alice", "and", "bob", "are", and "playing". "stone-game10" is invalid because it contains digits.

**Constraints:**

* `1 <= sentence.length <= 1000`
* `sentence` only contains lowercase English letters, digits, `' '`, `'-'`, `'!'`, `'.'`, and `','`.
* There will be at least `1` token.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package g2001_2100.s2048_next_greater_numerically_balanced_number;

// #Medium #Math #Backtracking #Enumeration #2022_05_26_Time_2_ms_(95.19%)_Space_41.8_MB_(72.12%)

public class Solution {
public int nextBeautifulNumber(int n) {
int[] arr = new int[] {0, 1, 2, 3, 4, 5, 6};
boolean[] select = new boolean[7];
int d = n == 0 ? 1 : (int) Math.log10(n) + 1;
return solve(1, n, d, 0, select, arr);
}

private int solve(int i, int n, int d, int sz, boolean[] select, int[] arr) {
if (sz > d + 1) {
return Integer.MAX_VALUE;
}
if (i == select.length) {
return sz >= d ? make(0, n, sz, select, arr) : Integer.MAX_VALUE;
}
int ans = solve(i + 1, n, d, sz, select, arr);
select[i] = true;
ans = Math.min(ans, solve(i + 1, n, d, sz + i, select, arr));
select[i] = false;
return ans;
}

private int make(int cur, int n, int end, boolean[] select, int[] arr) {
if (end == 0) {
return cur > n ? cur : Integer.MAX_VALUE;
}

int ans = Integer.MAX_VALUE;
for (int j = 1; j < arr.length; j++) {
if (!select[j] || arr[j] == 0) {
continue;
}
--arr[j];
ans = Math.min(make(10 * cur + j, n, end - 1, select, arr), ans);
++arr[j];
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
2048\. Next Greater Numerically Balanced Number

Medium

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`.

Given an integer `n`, return _the **smallest numerically balanced** number **strictly greater** than_ `n`_._

**Example 1:**

**Input:** n = 1

**Output:** 22

**Explanation:**

22 is numerically balanced since:

- The digit 2 occurs 2 times.

It is also the smallest numerically balanced number strictly greater than 1.

**Example 2:**

**Input:** n = 1000

**Output:** 1333

**Explanation:**

1333 is numerically balanced since:

- The digit 1 occurs 1 time.

- The digit 3 occurs 3 times.

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.

**Example 3:**

**Input:** n = 3000

**Output:** 3133

**Explanation:**

3133 is numerically balanced since:

- The digit 1 occurs 1 time.

- The digit 3 occurs 3 times.

It is also the smallest numerically balanced number strictly greater than 3000.

**Constraints:**

* <code>0 <= n <= 10<sup>6</sup></code>
Loading