-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
JavaCode/random_records/N3001_N3400/N3128_right_triangles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package JavaCode.random_records.N3001_N3400; | ||
|
||
import utils.CheckUtil; | ||
import utils.Parse; | ||
|
||
/** | ||
* @author mikusugar | ||
* @version 1.0, 2024/8/2 下午3:30 | ||
* @description N3128_right_triangles | ||
*/ | ||
public class N3128_right_triangles { | ||
public static void main(String[] args) { | ||
CheckUtil.check(2L, | ||
new N3128_right_triangles().numberOfRightTriangles(Parse.toIntTwoArr("[[0,1,0],[0,1,1],[0,1,0]]"))); | ||
CheckUtil.check(2L, new | ||
N3128_right_triangles().numberOfRightTriangles(Parse.toIntTwoArr("[[1,0,1],[1,0,0],[1,0,0]]"))); | ||
} | ||
|
||
public long numberOfRightTriangles(int[][] grid) { | ||
//todo | ||
int[] sumI = new int[grid.length + 1]; | ||
int[] sumJ = new int[grid[0].length + 1]; | ||
|
||
|
||
long count = 0; | ||
for (int i = 0; i < grid.length; i++) { | ||
for (int j = 0; j < grid[0].length; j++) { | ||
|
||
} | ||
} | ||
return count; | ||
} | ||
|
||
|
||
} | ||
/* | ||
给你一个二维 boolean 矩阵 grid 。 | ||
请你返回使用 grid 中的 3 个元素可以构建的 直角三角形 数目,且满足 3 个元素值 都 为 1 。 | ||
注意: | ||
如果 grid 中 3 个元素满足:一个元素与另一个元素在 同一行,同时与第三个元素在 同一列 ,那么这 3 个元素称为一个 直角三角形 。这 3 个元素互相之间不需要相邻。 | ||
示例 1: | ||
0 1 0 | ||
0 1 1 | ||
0 1 0 | ||
0 1 0 | ||
0 1 1 | ||
0 1 0 | ||
输入:grid = [[0,1,0],[0,1,1],[0,1,0]] | ||
输出:2 | ||
解释: | ||
有 2 个直角三角形。 | ||
示例 2: | ||
1 0 0 0 | ||
0 1 0 1 | ||
1 0 0 0 | ||
输入:grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]] | ||
输出:0 | ||
解释: | ||
没有直角三角形。 | ||
示例 3: | ||
1 0 1 | ||
1 0 0 | ||
1 0 0 | ||
1 0 1 | ||
1 0 0 | ||
1 0 0 | ||
输入:grid = [[1,0,1],[1,0,0],[1,0,0]] | ||
输出:2 | ||
解释: | ||
有两个直角三角形。 | ||
提示: | ||
1 <= grid.length <= 1000 | ||
1 <= grid[i].length <= 1000 | ||
0 <= grid[i][j] <= 1 | ||
*/ |
124 changes: 124 additions & 0 deletions
124
JavaCode/random_records/N3001_N3400/N3129_find_all_possible_stable_binary_arrays_i.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package JavaCode.random_records.N3001_N3400; | ||
|
||
import utils.CheckUtil; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @author mikusugar | ||
* @version 1.0, 2024/8/6 下午4:16 | ||
* @description N3129_find_all_possible_stable_binary_arrays_i | ||
*/ | ||
public class N3129_find_all_possible_stable_binary_arrays_i { | ||
|
||
public static void main(String[] args) { | ||
CheckUtil.check(2, new N3129_find_all_possible_stable_binary_arrays_i().numberOfStableArrays(1, 1, 2)); | ||
CheckUtil.check(1, new N3129_find_all_possible_stable_binary_arrays_i().numberOfStableArrays(1, 2, 1)); | ||
CheckUtil.check(14, new N3129_find_all_possible_stable_binary_arrays_i().numberOfStableArrays(3, 3, 2)); | ||
CheckUtil.check(207227572, new N3129_find_all_possible_stable_binary_arrays_i().numberOfStableArrays(39, 20, 18)); | ||
} | ||
|
||
private final static int MOD = 1000000007; | ||
private long[][][] dp; | ||
private int limit; | ||
|
||
public int numberOfStableArrays(int zero, int one, int limit) { | ||
dp = new long[zero + 1][one + 1][2]; | ||
for (int i = 1; i <= zero; i++) { | ||
for (int j = 1; j <= one; j++) { | ||
Arrays.fill(dp[i][j], -1); | ||
} | ||
} | ||
this.limit = limit; | ||
return (int) ((dfs(zero, one, 0) + dfs(zero, one, 1)) % MOD); | ||
} | ||
|
||
private long dfs(int zero, int one, int next) { | ||
if (zero == 0) { | ||
if (next == 1 && one <= limit) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
if (one == 0) { | ||
if (next == 0 && zero <= limit) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
if (dp[zero][one][next] != -1) { | ||
return dp[zero][one][next]; | ||
} | ||
long result; | ||
if (next == 0) { | ||
result = dfs(zero - 1, one, 0) + dfs(zero - 1, one, 1); | ||
if (zero - limit > 0) { | ||
result -= dfs(zero - limit - 1, one, 1); | ||
result += MOD; | ||
} | ||
} else { | ||
result = dfs(zero, one - 1, 0) + dfs(zero, one - 1, 1); | ||
if (one - limit > 0) { | ||
result -= dfs(zero, one - limit - 1, 0); | ||
result += MOD; | ||
} | ||
} | ||
return dp[zero][one][next] = result % MOD; | ||
} | ||
} | ||
/* | ||
给你 3 个正整数 zero ,one 和 limit 。 | ||
一个 | ||
二进制数组 | ||
arr 如果满足以下条件,那么我们称它是 稳定的 : | ||
0 在 arr 中出现次数 恰好 为 zero 。 | ||
1 在 arr 中出现次数 恰好 为 one 。 | ||
arr 中每个长度超过 limit 的 | ||
子数组 | ||
都 同时 包含 0 和 1 。 | ||
请你返回 稳定 二进制数组的 总 数目。 | ||
由于答案可能很大,将它对 109 + 7 取余 后返回。 | ||
示例 1: | ||
输入:zero = 1, one = 1, limit = 2 | ||
输出:2 | ||
解释: | ||
两个稳定的二进制数组为 [1,0] 和 [0,1] ,两个数组都有一个 0 和一个 1 ,且没有子数组长度大于 2 。 | ||
示例 2: | ||
输入:zero = 1, one = 2, limit = 1 | ||
输出:1 | ||
解释: | ||
唯一稳定的二进制数组是 [1,0,1] 。 | ||
二进制数组 [1,1,0] 和 [0,1,1] 都有长度为 2 且元素全都相同的子数组,所以它们不稳定。 | ||
示例 3: | ||
输入:zero = 3, one = 3, limit = 2 | ||
输出:14 | ||
解释: | ||
所有稳定的二进制数组包括 [0,0,1,0,1,1] ,[0,0,1,1,0,1] ,[0,1,0,0,1,1] ,[0,1,0,1,0,1] ,[0,1,0,1,1,0] ,[0,1,1,0,0,1] ,[0,1,1,0,1,0] ,[1,0,0,1,0,1] ,[1,0,0,1,1,0] ,[1,0,1,0,0,1] ,[1,0,1,0,1,0] ,[1,0,1,1,0,0] ,[1,1,0,0,1,0] 和 [1,1,0,1,0,0] 。 | ||
提示: | ||
1 <= zero, one, limit <= 200 | ||
*/ |