Skip to content

Commit 84899c5

Browse files
authored
Added tasks 1901, 1903, 1904, 1905, 1906.
1 parent 9b1f305 commit 84899c5

File tree

15 files changed

+606
-0
lines changed

15 files changed

+606
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g1901_2000.s1901_find_a_peak_element_ii;
2+
3+
// #Medium #Array #Binary_Search #Matrix #Divide_and_Conquer #Binary_Search_II_Day_17
4+
// #2022_05_11_Time_0_ms_(100.00%)_Space_115.1_MB_(45.96%)
5+
6+
public class Solution {
7+
public int[] findPeakGrid(int[][] mat) {
8+
int n = mat.length;
9+
int m = mat[0].length;
10+
int l = 0;
11+
int r = m - 1;
12+
int mid;
13+
while (l <= r) {
14+
mid = (l + r) / 2;
15+
int mx = mat[0][mid];
16+
int mxi = 0;
17+
for (int i = 1; i < n; i++) {
18+
if (mx < mat[i][mid]) {
19+
mx = mat[i][mid];
20+
mxi = i;
21+
}
22+
}
23+
int lv = mid > l ? mat[mxi][mid - 1] : -1;
24+
int rv = mid < r ? mat[mxi][mid + 1] : -1;
25+
if (mx > lv && mx > rv) {
26+
return new int[] {mxi, mid};
27+
} else if (mx > lv) {
28+
l = mid + 1;
29+
} else {
30+
r = mid - 1;
31+
}
32+
}
33+
return new int[] {-1, -1};
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1901\. Find a Peak Element II
2+
3+
Medium
4+
5+
A **peak** element in a 2D grid is an element that is **strictly greater** than all of its **adjacent** neighbors to the left, right, top, and bottom.
6+
7+
Given a **0-indexed** `m x n` matrix `mat` where **no two adjacent cells are equal**, find **any** peak element `mat[i][j]` and return _the length 2 array_ `[i,j]`.
8+
9+
You may assume that the entire matrix is surrounded by an **outer perimeter** with the value `-1` in each cell.
10+
11+
You must write an algorithm that runs in `O(m log(n))` or `O(n log(m))` time.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/06/08/1.png)
16+
17+
**Input:** mat = [[1,4],[3,2]]
18+
19+
**Output:** [0,1]
20+
21+
**Explanation:** Both 3 and 4 are peak elements so [1,0] and [0,1] are both acceptable answers.
22+
23+
**Example 2:**
24+
25+
**![](https://assets.leetcode.com/uploads/2021/06/07/3.png)**
26+
27+
**Input:** mat = [[10,20,15],[21,30,14],[7,16,32]]
28+
29+
**Output:** [1,1]
30+
31+
**Explanation:** Both 30 and 32 are peak elements so [1,1] and [2,2] are both acceptable answers.
32+
33+
**Constraints:**
34+
35+
* `m == mat.length`
36+
* `n == mat[i].length`
37+
* `1 <= m, n <= 500`
38+
* <code>1 <= mat[i][j] <= 10<sup>5</sup></code>
39+
* No two adjacent cells are equal.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g1901_2000.s1903_largest_odd_number_in_string;
2+
3+
// #Easy #String #Math #Greedy #2022_05_11_Time_6_ms_(23.18%)_Space_43.2_MB_(81.76%)
4+
5+
public class Solution {
6+
public String largestOddNumber(String num) {
7+
for (int i = num.length() - 1; i >= 0; i--) {
8+
if (Integer.parseInt("" + num.charAt(i)) % 2 == 1) {
9+
return num.substring(0, i + 1);
10+
}
11+
}
12+
return "";
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1903\. Largest Odd Number in String
2+
3+
Easy
4+
5+
You are given a string `num`, representing a large integer. Return _the **largest-valued odd** integer (as a string) that is a **non-empty substring** of_ `num`_, or an empty string_ `""` _if no odd integer exists_.
6+
7+
A **substring** is a contiguous sequence of characters within a string.
8+
9+
**Example 1:**
10+
11+
**Input:** num = "52"
12+
13+
**Output:** "5"
14+
15+
**Explanation:** The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.
16+
17+
**Example 2:**
18+
19+
**Input:** num = "4206"
20+
21+
**Output:** ""
22+
23+
**Explanation:** There are no odd numbers in "4206".
24+
25+
**Example 3:**
26+
27+
**Input:** num = "35427"
28+
29+
**Output:** "35427"
30+
31+
**Explanation:** "35427" is already an odd number.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= num.length <= 10<sup>5</sup></code>
36+
* `num` only consists of digits and does not contain any leading zeros.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g1901_2000.s1904_the_number_of_full_rounds_you_have_played;
2+
3+
// #Medium #String #Math #2022_05_11_Time_2_ms_(37.11%)_Space_42.1_MB_(40.55%)
4+
5+
public class Solution {
6+
public int numberOfRounds(String startTime, String finishTime) {
7+
int rounds = 0;
8+
int startHour = Integer.parseInt(startTime.split(":")[0]);
9+
int endHour = Integer.parseInt(finishTime.split(":")[0]);
10+
int startMin = Integer.parseInt(startTime.split(":")[1]);
11+
int endMin = Integer.parseInt(finishTime.split(":")[1]);
12+
if (endHour < startHour) {
13+
endHour += 24;
14+
} else if (endHour == startHour && endMin < startMin) {
15+
endHour += 24;
16+
}
17+
if (startHour == endHour) {
18+
if (startMin == 0 && endMin >= 15) {
19+
rounds++;
20+
}
21+
if (startMin <= 15 && endMin >= 30) {
22+
rounds++;
23+
}
24+
if (startMin <= 30 && endMin >= 45) {
25+
rounds++;
26+
}
27+
return rounds;
28+
} else {
29+
// compute all full rounds in the start hour
30+
if (startMin == 0) {
31+
rounds += 4;
32+
} else if (startMin <= 15) {
33+
rounds += 3;
34+
} else if (startMin <= 30) {
35+
rounds += 2;
36+
} else if (startMin <= 45) {
37+
rounds++;
38+
}
39+
40+
// compute all full rounds in the finish hour
41+
if (endMin >= 45) {
42+
rounds += 3;
43+
} else if (endMin >= 30) {
44+
rounds += 2;
45+
} else if (endMin >= 15) {
46+
rounds++;
47+
}
48+
49+
// compute all full rounds in the all full hours between finishHour and startHour
50+
rounds += (endHour - startHour - 1) * 4;
51+
return rounds;
52+
}
53+
}
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
1904\. The Number of Full Rounds You Have Played
2+
3+
Medium
4+
5+
You are participating in an online chess tournament. There is a chess round that starts every `15` minutes. The first round of the day starts at `00:00`, and after every `15` minutes, a new round starts.
6+
7+
* For example, the second round starts at `00:15`, the fourth round starts at `00:45`, and the seventh round starts at `01:30`.
8+
9+
You are given two strings `loginTime` and `logoutTime` where:
10+
11+
* `loginTime` is the time you will login to the game, and
12+
* `logoutTime` is the time you will logout from the game.
13+
14+
If `logoutTime` is **earlier** than `loginTime`, this means you have played from `loginTime` to midnight and from midnight to `logoutTime`.
15+
16+
Return _the number of full chess rounds you have played in the tournament_.
17+
18+
**Note:** All the given times follow the 24-hour clock. That means the first round of the day starts at `00:00` and the last round of the day starts at `23:45`.
19+
20+
**Example 1:**
21+
22+
**Input:** loginTime = "09:31", logoutTime = "10:14"
23+
24+
**Output:** 1
25+
26+
**Explanation:**
27+
28+
You played one full round from 09:45 to 10:00. You did not play the full round from 09:30 to 09:45 because you logged in at 09:31 after it began.
29+
30+
You did not play the full round from 10:00 to 10:15 because you logged out at 10:14 before it ended.
31+
32+
**Example 2:**
33+
34+
**Input:** loginTime = "21:30", logoutTime = "03:00"
35+
36+
**Output:** 22
37+
38+
**Explanation:** You played 10 full rounds from 21:30 to 00:00 and 12 full rounds from 00:00 to 03:00. 10 + 12 = 22.
39+
40+
**Constraints:**
41+
42+
* `loginTime` and `logoutTime` are in the format `hh:mm`.
43+
* `00 <= hh <= 23`
44+
* `00 <= mm <= 59`
45+
* `loginTime` and `logoutTime` are not equal.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g1901_2000.s1905_count_sub_islands;
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
4+
// #Graph_Theory_I_Day_3_Matrix_Related_Problems
5+
// #2022_05_11_Time_24_ms_(85.54%)_Space_142.6_MB_(18.19%)
6+
7+
public class Solution {
8+
private int ans = 0;
9+
10+
public int countSubIslands(int[][] grid1, int[][] grid2) {
11+
int count = 0;
12+
for (int i = 0; i < grid2.length; i++) {
13+
for (int j = 0; j < grid2[0].length; j++) {
14+
if (grid2[i][j] == 1) {
15+
ans = 1;
16+
dfs(grid1, grid2, i, j);
17+
count += ans;
18+
}
19+
}
20+
}
21+
return count;
22+
}
23+
24+
private void dfs(int[][] grid1, int[][] grid2, int i, int j) {
25+
if (i < 0 || j < 0 || i >= grid1.length || j >= grid1[0].length || grid2[i][j] == 0) {
26+
return;
27+
}
28+
if (grid1[i][j] == 0) {
29+
ans = 0;
30+
}
31+
grid2[i][j] = 0;
32+
dfs(grid1, grid2, i - 1, j);
33+
dfs(grid1, grid2, i + 1, j);
34+
dfs(grid1, grid2, i, j + 1);
35+
dfs(grid1, grid2, i, j - 1);
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1905\. Count Sub Islands
2+
3+
Medium
4+
5+
You are given two `m x n` binary matrices `grid1` and `grid2` containing only `0`'s (representing water) and `1`'s (representing land). An **island** is a group of `1`'s connected **4-directionally** (horizontal or vertical). Any cells outside of the grid are considered water cells.
6+
7+
An island in `grid2` is considered a **sub-island** if there is an island in `grid1` that contains **all** the cells that make up **this** island in `grid2`.
8+
9+
Return the _**number** of islands in_ `grid2` _that are considered **sub-islands**_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/06/10/test1.png)
14+
15+
**Input:** grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
16+
17+
**Output:** 3
18+
19+
**Explanation:** In the picture above, the grid on the left is grid1 and the grid on the right is grid2. The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/06/03/testcasex2.png)
24+
25+
**Input:** grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
26+
27+
**Output:** 2
28+
29+
**Explanation:** In the picture above, the grid on the left is grid1 and the grid on the right is grid2. The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
30+
31+
**Constraints:**
32+
33+
* `m == grid1.length == grid2.length`
34+
* `n == grid1[i].length == grid2[i].length`
35+
* `1 <= m, n <= 500`
36+
* `grid1[i][j]` and `grid2[i][j]` are either `0` or `1`.

0 commit comments

Comments
 (0)