Skip to content

Commit 63d9b22

Browse files
authored
Added tasks 741, 743, 744, 745.
1 parent 7013d30 commit 63d9b22

File tree

13 files changed

+409
-0
lines changed

13 files changed

+409
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g0701_0800.s0741_cherry_pickup;
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix
4+
5+
public class Solution {
6+
public int cherryPickup(int[][] grid) {
7+
int[][][] dp = new int[grid.length][grid.length][grid.length];
8+
int ans = solve(0, 0, 0, grid, dp);
9+
return Math.max(ans, 0);
10+
}
11+
12+
private int solve(int r1, int c1, int r2, int[][] arr, int[][][] dp) {
13+
int c2 = r1 + c1 - r2;
14+
if (r1 >= arr.length
15+
|| r2 >= arr.length
16+
|| c1 >= arr[0].length
17+
|| c2 >= arr[0].length
18+
|| arr[r1][c1] == -1
19+
|| arr[r2][c2] == -1) {
20+
return Integer.MIN_VALUE;
21+
}
22+
23+
if (r1 == arr.length - 1 && c1 == arr[0].length - 1) {
24+
return arr[r1][c1];
25+
}
26+
27+
if (dp[r1][c1][r2] != 0) {
28+
return dp[r1][c1][r2];
29+
}
30+
31+
int cherries = 0;
32+
if (r1 == r2 && c1 == c2) {
33+
cherries += arr[r1][c1];
34+
} else {
35+
cherries += arr[r1][c1] + arr[r2][c2];
36+
}
37+
38+
int a = solve(r1, c1 + 1, r2, arr, dp);
39+
int b = solve(r1 + 1, c1, r2 + 1, arr, dp);
40+
int c = solve(r1, c1 + 1, r2 + 1, arr, dp);
41+
int d = solve(r1 + 1, c1, r2, arr, dp);
42+
43+
cherries += Math.max(Math.max(a, b), Math.max(c, d));
44+
dp[r1][c1][r2] = cherries;
45+
return cherries;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
741\. Cherry Pickup
2+
3+
Hard
4+
5+
You are given an `n x n` `grid` representing a field of cherries, each cell is one of three possible integers.
6+
7+
* `0` means the cell is empty, so you can pass through,
8+
* `1` means the cell contains a cherry that you can pick up and pass through, or
9+
* `-1` means the cell contains a thorn that blocks your way.
10+
11+
Return _the maximum number of cherries you can collect by following the rules below_:
12+
13+
* Starting at the position `(0, 0)` and reaching `(n - 1, n - 1)` by moving right or down through valid path cells (cells with value `0` or `1`).
14+
* After reaching `(n - 1, n - 1)`, returning to `(0, 0)` by moving left or up through valid path cells.
15+
* When passing through a path cell containing a cherry, you pick it up, and the cell becomes an empty cell `0`.
16+
* If there is no valid path between `(0, 0)` and `(n - 1, n - 1)`, then no cherries can be collected.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/12/14/grid.jpg)
21+
22+
**Input:** grid = [[0,1,-1],[1,0,-1],[1,1,1]]
23+
24+
**Output:** 5
25+
26+
**Explanation:** The player started at (0, 0) and went down, down, right right to reach (2, 2). 4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]]. Then, the player went left, up, up, left to return home, picking up one more cherry. The total number of cherries picked up is 5, and this is the maximum possible.
27+
28+
**Example 2:**
29+
30+
**Input:** grid = [[1,1,-1],[1,-1,1],[-1,1,1]]
31+
32+
**Output:** 0
33+
34+
**Constraints:**
35+
36+
* `n == grid.length`
37+
* `n == grid[i].length`
38+
* `1 <= n <= 50`
39+
* `grid[i][j]` is `-1`, `0`, or `1`.
40+
* `grid[0][0] != -1`
41+
* `grid[n - 1][n - 1] != -1`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0701_0800.s0743_network_delay_time;
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Heap_Priority_Queue #Graph #Shortest_Path
4+
5+
import java.util.Arrays;
6+
import java.util.LinkedList;
7+
import java.util.Queue;
8+
9+
public class Solution {
10+
public int networkDelayTime(int[][] times, int n, int k) {
11+
int[][] graph = new int[n + 1][n + 1];
12+
for (int[] g : graph) {
13+
Arrays.fill(g, -1);
14+
}
15+
for (int[] t : times) {
16+
graph[t[0]][t[1]] = t[2];
17+
}
18+
boolean[] visited = new boolean[n + 1];
19+
int[] dist = new int[n + 1];
20+
Arrays.fill(dist, Integer.MAX_VALUE);
21+
dist[k] = 0;
22+
Queue<Integer> spfa = new LinkedList<>();
23+
spfa.add(k);
24+
visited[k] = true;
25+
while (!spfa.isEmpty()) {
26+
int curr = spfa.poll();
27+
visited[curr] = false;
28+
for (int i = 1; i <= n; i++) {
29+
if (graph[curr][i] != -1) {
30+
if (dist[i] > dist[curr] + graph[curr][i]) {
31+
dist[i] = dist[curr] + graph[curr][i];
32+
if (!visited[i]) {
33+
spfa.add(i);
34+
visited[i] = true;
35+
}
36+
}
37+
}
38+
}
39+
}
40+
int result = 0;
41+
for (int i = 1; i <= n; i++) {
42+
result = Math.max(dist[i], result);
43+
}
44+
return result == Integer.MAX_VALUE ? -1 : result;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
743\. Network Delay Time
2+
3+
Medium
4+
5+
You are given a network of `n` nodes, labeled from `1` to `n`. You are also given `times`, a list of travel times as directed edges <code>times[i] = (u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>)</code>, where <code>u<sub>i</sub></code> is the source node, <code>v<sub>i</sub></code> is the target node, and <code>w<sub>i</sub></code> is the time it takes for a signal to travel from source to target.
6+
7+
We will send a signal from a given node `k`. Return the time it takes for all the `n` nodes to receive the signal. If it is impossible for all the `n` nodes to receive the signal, return `-1`.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2019/05/23/931_example_1.png)
12+
13+
**Input:** times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
14+
15+
**Output:** 2
16+
17+
**Example 2:**
18+
19+
**Input:** times = [[1,2,1]], n = 2, k = 1
20+
21+
**Output:** 1
22+
23+
**Example 3:**
24+
25+
**Input:** times = [[1,2,1]], n = 2, k = 2
26+
27+
**Output:** -1
28+
29+
**Constraints:**
30+
31+
* `1 <= k <= n <= 100`
32+
* `1 <= times.length <= 6000`
33+
* `times[i].length == 3`
34+
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
35+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
36+
* <code>0 <= w<sub>i</sub> <= 100</code>
37+
* All the pairs <code>(u<sub>i</sub>, v<sub>i</sub>)</code> are **unique**. (i.e., no multiple edges.)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0701_0800.s0744_find_smallest_letter_greater_than_target;
2+
3+
// #Easy #Array #Binary_Search
4+
5+
public class Solution {
6+
public char nextGreatestLetter(char[] letters, char target) {
7+
if (letters[0] > target) {
8+
return letters[0];
9+
}
10+
int left = 0;
11+
int right = letters.length - 1;
12+
while (left < right) {
13+
int mid = left + (right - left) / 2;
14+
if (letters[mid] > target) {
15+
while (letters[mid] > target) {
16+
mid--;
17+
}
18+
return letters[++mid];
19+
} else {
20+
left = mid + 1;
21+
}
22+
}
23+
if (right < letters.length && letters[right] > target) {
24+
return letters[right];
25+
}
26+
return letters[0];
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
744\. Find Smallest Letter Greater Than Target
2+
3+
Easy
4+
5+
Given a characters array `letters` that is sorted in **non-decreasing** order and a character `target`, return _the smallest character in the array that is larger than_ `target`.
6+
7+
**Note** that the letters wrap around.
8+
9+
* For example, if `target == 'z'` and `letters == ['a', 'b']`, the answer is `'a'`.
10+
11+
**Example 1:**
12+
13+
**Input:** letters = ["c","f","j"], target = "a"
14+
15+
**Output:** "c"
16+
17+
**Example 2:**
18+
19+
**Input:** letters = ["c","f","j"], target = "c"
20+
21+
**Output:** "f"
22+
23+
**Example 3:**
24+
25+
**Input:** letters = ["c","f","j"], target = "d"
26+
27+
**Output:** "f"
28+
29+
**Constraints:**
30+
31+
* <code>2 <= letters.length <= 10<sup>4</sup></code>
32+
* `letters[i]` is a lowercase English letter.
33+
* `letters` is sorted in **non-decreasing** order.
34+
* `letters` contains at least two different characters.
35+
* `target` is a lowercase English letter.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package g0701_0800.s0745_prefix_and_suffix_search;
2+
3+
class TrieNode {
4+
public TrieNode[] children;
5+
public int weight;
6+
7+
TrieNode() {
8+
this.children = new TrieNode[27];
9+
this.weight = 0;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0701_0800.s0745_prefix_and_suffix_search;
2+
3+
// #Hard #String #Design #Trie
4+
5+
public class WordFilter {
6+
7+
TrieNode root = new TrieNode();
8+
9+
public WordFilter(String[] words) {
10+
for (int i = 0; i < words.length; i++) {
11+
String s = words[i];
12+
for (int j = 0; j < s.length(); j++) {
13+
insert(s.substring(j) + '{' + s, i);
14+
}
15+
}
16+
}
17+
18+
public void insert(String wd, int weight) {
19+
TrieNode node = root;
20+
for (char ch : wd.toCharArray()) {
21+
if (node.children[ch - 'a'] == null) {
22+
node.children[ch - 'a'] = new TrieNode();
23+
}
24+
node = node.children[ch - 'a'];
25+
node.weight = weight;
26+
}
27+
}
28+
29+
public int f(String prefix, String suffix) {
30+
return startsWith(suffix + '{' + prefix);
31+
}
32+
33+
public int startsWith(String pref) {
34+
TrieNode node = root;
35+
for (char ch : pref.toCharArray()) {
36+
if (node.children[ch - 'a'] == null) {
37+
return -1;
38+
}
39+
node = node.children[ch - 'a'];
40+
}
41+
return node.weight;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
745\. Prefix and Suffix Search
2+
3+
Hard
4+
5+
Design a special dictionary with some words that searchs the words in it by a prefix and a suffix.
6+
7+
Implement the `WordFilter` class:
8+
9+
* `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary.
10+
* `f(string prefix, string suffix)` Returns _the index of the word in the dictionary,_ which has the prefix `prefix` and the suffix `suffix`. If there is more than one valid index, return **the largest** of them. If there is no such word in the dictionary, return `-1`.
11+
12+
**Example 1:**
13+
14+
**Input**
15+
16+
["WordFilter", "f"]
17+
18+
[[["apple"]], ["a", "e"]]
19+
20+
**Output:** [null, 0]
21+
22+
**Explanation:**
23+
24+
WordFilter wordFilter = new WordFilter(["apple"]);
25+
wordFilter.f("a", "e"); // return 0, because the word at index 0 has prefix = "a" and suffix = 'e".
26+
27+
**Constraints:**
28+
29+
* `1 <= words.length <= 15000`
30+
* `1 <= words[i].length <= 10`
31+
* `1 <= prefix.length, suffix.length <= 10`
32+
* `words[i]`, `prefix` and `suffix` consist of lower-case English letters only.
33+
* At most `15000` calls will be made to the function `f`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0701_0800.s0741_cherry_pickup;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void cherryPickup() {
11+
assertThat(
12+
new Solution().cherryPickup(new int[][] {{0, 1, -1}, {1, 0, -1}, {1, 1, 1}}),
13+
equalTo(5));
14+
}
15+
16+
@Test
17+
void cherryPickup2() {
18+
assertThat(
19+
new Solution().cherryPickup(new int[][] {{1, 1, -1}, {1, -1, 1}, {-1, 1, 1}}),
20+
equalTo(0));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0701_0800.s0743_network_delay_time;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void networkDelayTime() {
11+
assertThat(
12+
new Solution()
13+
.networkDelayTime(new int[][] {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}}, 4, 2),
14+
equalTo(2));
15+
}
16+
17+
@Test
18+
void networkDelayTime2() {
19+
assertThat(new Solution().networkDelayTime(new int[][] {{1, 2, 1}}, 2, 1), equalTo(1));
20+
}
21+
22+
@Test
23+
void networkDelayTime3() {
24+
assertThat(new Solution().networkDelayTime(new int[][] {{1, 2, 1}}, 2, 2), equalTo(-1));
25+
}
26+
}

0 commit comments

Comments
 (0)