Skip to content

Commit 87b87ac

Browse files
authored
Added tasks 1726, 1727, 1728, 1732, 1733.
1 parent 7f1f441 commit 87b87ac

File tree

15 files changed

+566
-0
lines changed

15 files changed

+566
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1701_1800.s1726_tuple_with_same_product;
2+
3+
// #Medium #Array #Hash_Table #2022_04_28_Time_235_ms_(90.73%)_Space_63.4_MB_(87.42%)
4+
5+
import java.util.HashMap;
6+
7+
public class Solution {
8+
public int tupleSameProduct(int[] nums) {
9+
HashMap<Integer, Integer> ab = new HashMap<>();
10+
11+
for (int i = 0; i < nums.length; i++) {
12+
for (int j = i + 1; j < nums.length; j++) {
13+
ab.put(nums[i] * nums[j], ab.getOrDefault(nums[i] * nums[j], 0) + 1);
14+
}
15+
}
16+
17+
int count = 0;
18+
for (Integer key : ab.keySet()) {
19+
int val = ab.get(key);
20+
count = count + (val * (val - 1)) / 2;
21+
}
22+
23+
return count * 8;
24+
}
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1726\. Tuple with Same Product
2+
3+
Medium
4+
5+
Given an array `nums` of **distinct** positive integers, return _the number of tuples_ `(a, b, c, d)` _such that_ `a * b = c * d` _where_ `a`_,_ `b`_,_ `c`_, and_ `d` _are elements of_ `nums`_, and_ `a != b != c != d`_._
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [2,3,4,6]
10+
11+
**Output:** 8
12+
13+
**Explanation:** There are 8 valid tuples:
14+
15+
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
16+
17+
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,2,4,5,10]
22+
23+
**Output:** 16
24+
25+
**Explanation:** There are 16 valid tuples:
26+
27+
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
28+
29+
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
30+
31+
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
32+
33+
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 1000`
38+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
39+
* All elements in `nums` are **distinct**.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g1701_1800.s1727_largest_submatrix_with_rearrangements;
2+
3+
// #Medium #Array #Sorting #Greedy #Matrix #2022_04_28_Time_9_ms_(90.48%)_Space_67.7_MB_(91.27%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int largestSubmatrix(int[][] matrix) {
9+
int m = matrix.length;
10+
int n = matrix[0].length;
11+
for (int i = 1; i < m; i++) {
12+
for (int j = 0; j < n; j++) {
13+
if (matrix[i][j] != 0) {
14+
matrix[i][j] = matrix[i - 1][j] + 1;
15+
}
16+
}
17+
}
18+
int count = 0;
19+
for (int[] ints : matrix) {
20+
Arrays.sort(ints);
21+
for (int j = 1; j <= n; j++) {
22+
count = Math.max(count, j * ints[n - j]);
23+
}
24+
}
25+
return count;
26+
}
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
1727\. Largest Submatrix With Rearrangements
2+
3+
Medium
4+
5+
You are given a binary matrix `matrix` of size `m x n`, and you are allowed to rearrange the **columns** of the `matrix` in any order.
6+
7+
Return _the area of the largest submatrix within_ `matrix` _where **every** element of the submatrix is_ `1` _after reordering the columns optimally._
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40536-pm.png)
12+
13+
**Input:** matrix = [[0,0,1],[1,1,1],[1,0,1]]
14+
15+
**Output:** 4
16+
17+
**Explanation:** You can rearrange the columns as shown above. The largest submatrix of 1s, in bold, has an area of 4.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40852-pm.png)
22+
23+
**Input:** matrix = [[1,0,1,0,1]]
24+
25+
**Output:** 3
26+
27+
**Explanation:** You can rearrange the columns as shown above. The largest submatrix of 1s, in bold, has an area of 3.
28+
29+
**Example 3:**
30+
31+
**Input:** matrix = [[1,1,0],[1,0,1]]
32+
33+
**Output:** 2
34+
35+
**Explanation:** Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.
36+
37+
**Constraints:**
38+
39+
* `m == matrix.length`
40+
* `n == matrix[i].length`
41+
* <code>1 <= m * n <= 10<sup>5</sup></code>
42+
* `matrix[i][j]` is either `0` or `1`.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package g1701_1800.s1728_cat_and_mouse_ii;
2+
3+
// #Hard #Dynamic_Programming #Math #Breadth_First_Search #Graph #Memoization #Game_Theory
4+
// #2022_04_28_Time_12_ms_(99.12%)_Space_42.7_MB_(96.49%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private final int mouseTurn = 0;
11+
private final List<Integer>[][] graphs = new List[2][];
12+
private int foodPos;
13+
private int[][][] memo;
14+
15+
public boolean canMouseWin(String[] grid, int catJump, int mouseJump) {
16+
int m = grid.length;
17+
int n = grid[0].length();
18+
int mousePos = 0;
19+
int catPos = 0;
20+
for (int i = 0; i < m; i++) {
21+
for (int j = 0; j < n; j++) {
22+
char c = grid[i].charAt(j);
23+
if (c == 'F') {
24+
foodPos = i * n + j;
25+
} else if (c == 'C') {
26+
catPos = i * n + j;
27+
} else if (c == 'M') {
28+
mousePos = i * n + j;
29+
}
30+
}
31+
}
32+
graphs[0] = buildGraph(mouseJump, grid);
33+
graphs[1] = buildGraph(catJump, grid);
34+
memo = new int[m * n][m * n][2];
35+
for (int i = 0; i < m; i++) {
36+
for (int j = 0; j < n; j++) {
37+
char c = grid[i].charAt(j);
38+
if (c == '#' || c == 'F') {
39+
continue;
40+
}
41+
int catTurn = 1;
42+
dfs(i * n + j, foodPos, catTurn);
43+
}
44+
}
45+
return memo[mousePos][catPos][mouseTurn] < 0;
46+
}
47+
48+
private List<Integer>[] buildGraph(int jump, String[] grid) {
49+
int[][] dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
50+
int m = grid.length;
51+
int n = grid[0].length();
52+
List<Integer>[] graph = new List[m * n];
53+
for (int i = 0; i < m; i++) {
54+
for (int j = 0; j < n; j++) {
55+
List<Integer> list = new ArrayList<>();
56+
graph[i * n + j] = list;
57+
if (grid[i].charAt(j) == '#') {
58+
continue;
59+
}
60+
list.add(i * n + j);
61+
for (int[] dir : dirs) {
62+
for (int step = 1; step <= jump; step++) {
63+
int x = i + dir[0] * step;
64+
int y = j + dir[1] * step;
65+
if (x < 0 || x >= m || y < 0 || y >= n || grid[x].charAt(y) == '#') {
66+
break;
67+
}
68+
list.add(x * n + y);
69+
}
70+
}
71+
}
72+
}
73+
return graph;
74+
}
75+
76+
private void dfs(int p1, int p2, int turn) {
77+
if (p1 == p2) {
78+
return;
79+
}
80+
if ((turn == 0 ? p2 : p1) == foodPos) {
81+
return;
82+
}
83+
if (memo[p1][p2][turn] < 0) {
84+
return;
85+
}
86+
memo[p1][p2][turn] = -1;
87+
turn ^= 1;
88+
for (int w : graphs[turn][p2]) {
89+
if (turn == mouseTurn) {
90+
dfs(w, p1, turn);
91+
} else if (++memo[w][p1][turn] == graphs[turn][w].size()) {
92+
dfs(w, p1, turn);
93+
}
94+
}
95+
}
96+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
1728\. Cat and Mouse II
2+
3+
Hard
4+
5+
A game is played by a cat and a mouse named Cat and Mouse.
6+
7+
The environment is represented by a `grid` of size `rows x cols`, where each element is a wall, floor, player (Cat, Mouse), or food.
8+
9+
* Players are represented by the characters `'C'`(Cat)`,'M'`(Mouse).
10+
* Floors are represented by the character `'.'` and can be walked on.
11+
* Walls are represented by the character `'#'` and cannot be walked on.
12+
* Food is represented by the character `'F'` and can be walked on.
13+
* There is only one of each character `'C'`, `'M'`, and `'F'` in `grid`.
14+
15+
Mouse and Cat play according to the following rules:
16+
17+
* Mouse **moves first**, then they take turns to move.
18+
* During each turn, Cat and Mouse can jump in one of the four directions (left, right, up, down). They cannot jump over the wall nor outside of the `grid`.
19+
* `catJump, mouseJump` are the maximum lengths Cat and Mouse can jump at a time, respectively. Cat and Mouse can jump less than the maximum length.
20+
* Staying in the same position is allowed.
21+
* Mouse can jump over Cat.
22+
23+
The game can end in 4 ways:
24+
25+
* If Cat occupies the same position as Mouse, Cat wins.
26+
* If Cat reaches the food first, Cat wins.
27+
* If Mouse reaches the food first, Mouse wins.
28+
* If Mouse cannot get to the food within 1000 turns, Cat wins.
29+
30+
Given a `rows x cols` matrix `grid` and two integers `catJump` and `mouseJump`, return `true` _if Mouse can win the game if both Cat and Mouse play optimally, otherwise return_ `false`.
31+
32+
**Example 1:**
33+
34+
![](https://assets.leetcode.com/uploads/2020/09/12/sample_111_1955.png)
35+
36+
**Input:** grid = ["####F","#C...","M...."], catJump = 1, mouseJump = 2
37+
38+
**Output:** true
39+
40+
**Explanation:** Cat cannot catch Mouse on its turn nor can it get the food before Mouse.
41+
42+
**Example 2:**
43+
44+
![](https://assets.leetcode.com/uploads/2020/09/12/sample_2_1955.png)
45+
46+
**Input:** grid = ["M.C...F"], catJump = 1, mouseJump = 4
47+
48+
**Output:** true
49+
50+
**Example 3:**
51+
52+
**Input:** grid = ["M.C...F"], catJump = 1, mouseJump = 3
53+
54+
**Output:** false
55+
56+
**Constraints:**
57+
58+
* `rows == grid.length`
59+
* `cols = grid[i].length`
60+
* `1 <= rows, cols <= 8`
61+
* `grid[i][j]` consist only of characters `'C'`, `'M'`, `'F'`, `'.'`, and `'#'`.
62+
* There is only one of each character `'C'`, `'M'`, and `'F'` in `grid`.
63+
* `1 <= catJump, mouseJump <= 8`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g1701_1800.s1732_find_the_highest_altitude;
2+
3+
// #Easy #Array #Prefix_Sum #2022_04_28_Time_0_ms_(100.00%)_Space_40.1_MB_(83.65%)
4+
5+
public class Solution {
6+
public int largestAltitude(int[] gain) {
7+
int max = 0;
8+
int[] altitudes = new int[gain.length + 1];
9+
for (int i = 0; i < gain.length; i++) {
10+
altitudes[i + 1] = altitudes[i] + gain[i];
11+
max = Math.max(max, altitudes[i + 1]);
12+
}
13+
return max;
14+
}
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
1732\. Find the Highest Altitude
2+
3+
Easy
4+
5+
There is a biker going on a road trip. The road trip consists of `n + 1` points at different altitudes. The biker starts his trip on point `0` with altitude equal `0`.
6+
7+
You are given an integer array `gain` of length `n` where `gain[i]` is the **net gain in altitude** between points `i` and `i + 1` for all (`0 <= i < n)`. Return _the **highest altitude** of a point._
8+
9+
**Example 1:**
10+
11+
**Input:** gain = [-5,1,5,0,-7]
12+
13+
**Output:** 1
14+
15+
**Explanation:** The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
16+
17+
**Example 2:**
18+
19+
**Input:** gain = [-4,-3,-2,-1,4,3,2]
20+
21+
**Output:** 0
22+
23+
**Explanation:** The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
24+
25+
**Constraints:**
26+
27+
* `n == gain.length`
28+
* `1 <= n <= 100`
29+
* `-100 <= gain[i] <= 100`

0 commit comments

Comments
 (0)