Skip to content

Commit 24492e2

Browse files
author
digitspace
committed
569-Week 05
1 parent 5c5e257 commit 24492e2

File tree

4 files changed

+251
-0
lines changed

4 files changed

+251
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
/*
3+
* 221. Maximal Square
4+
* 最大正方形
5+
*
6+
* 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。
7+
8+
示例:
9+
10+
输入:
11+
12+
1 0 1 0 0
13+
1 0 1 1 1
14+
1 1 1 1 1
15+
1 0 0 1 0
16+
17+
输出: 4
18+
19+
20+
*/
21+
public class LeetCode_221_569 {
22+
23+
public static void main(String[] args) {
24+
// TODO Auto-generated method stub
25+
char[][] matrix = {
26+
{ '1', '0', '1', '0', '0' },
27+
{ '1', '0', '1', '1', '1' },
28+
{ '1', '1', '1', '1', '1' },
29+
{ '1', '0', '0', '1', '0' }
30+
};
31+
32+
int result = new LeetCode_221_569().new Solution().maximalSquare(matrix);
33+
System.out.println(result);
34+
}
35+
36+
/*
37+
* 看来的别人的思路,关键在于状态方程
38+
* 状态数组dp[i][j],里面放的是最大长方形边长
39+
* 状态方程: dp(i, j)=min(dp(i−1, j), dp(i−1, j−1), dp(i, j−1))+1
40+
* if (grid(i, j) == 1) {
41+
dp(i, j) = min(dp(i-1, j), dp(i, j-1), dp(i-1, j-1)) + 1;
42+
}
43+
*/
44+
45+
class Solution {
46+
public int maximalSquare(char[][] matrix) {
47+
int rows = matrix.length;
48+
int cols = matrix[0].length;
49+
int[][] dp = new int[rows + 1][cols + 1];
50+
int maxsqlen = 0;
51+
for (int i = 1; i <= rows; i++) {
52+
for (int j = 1; j <= cols; j++) {
53+
if (matrix[i-1][j-1] == '1'){
54+
dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
55+
maxsqlen = Math.max(maxsqlen, dp[i][j]);
56+
}
57+
}
58+
}
59+
return maxsqlen * maxsqlen;
60+
}
61+
}
62+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.Arrays;
2+
3+
/*
4+
* 621. Task Scheduler
5+
* 任务调度器
6+
*
7+
* 给定一个用字符数组表示的 CPU 需要执行的任务列表。其中包含使用大写的 A - Z 字母表示的26 种不同种类的任务。
8+
* 任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时间内执行完。CPU 在任何一个单位时间内都可以执行一
9+
* 个任务,或者在待命状态。
10+
11+
然而,两个相同种类的任务之间必须有长度为 n 的冷却时间,因此至少有连续 n 个单位时间内 CPU 在执行不同的任务,
12+
或者在待命状态。
13+
14+
你需要计算完成所有任务所需要的最短时间。
15+
16+
示例 1:
17+
18+
输入: tasks = ["A","A","A","B","B","B"], n = 2
19+
输出: 8
20+
执行顺序: A -> B -> (待命) -> A -> B -> (待命) -> A -> B.
21+
注:
22+
23+
任务的总个数为 [1, 10000]。
24+
n 的取值范围为 [0, 100]。
25+
26+
*/
27+
public class LeetCode_621_569 {
28+
29+
public static void main(String[] args) {
30+
char[] tasks = { 'A', 'A', 'A', 'B', 'B', 'B' };
31+
int n = 2;
32+
int result = new LeetCode_621_569().new Solution().leastInterval(tasks, n);
33+
System.out.println( result );
34+
}
35+
36+
/*
37+
* 解题思路:
38+
* 1、将任务按类型分组,正好A-Z用一个int[26]保存任务类型个数
39+
* 2、对数组进行排序,优先排列个数(count)最大的任务,
40+
* 如题得到的时间至少为 retCount =(count-1)* (n+1) + 1 ==> A->X->X->A->X->X->A(X为其他任务或者待命)
41+
* 3、再排序下一个任务,如果下一个任务B个数和最大任务数一致,
42+
* 则retCount++ ==> A->B->X->A->B->X->A->B
43+
* 4、如果空位都插满之后还有任务,那就随便在这些间隔里面插入就可以,因为间隔长度肯定会大于n,在这种情况下就是任务的总数是最小所需时间
44+
*
45+
*/
46+
class Solution {
47+
public int leastInterval(char[] tasks, int n) {
48+
int[] taskCounts = new int[26];
49+
for(char task: tasks) {
50+
taskCounts[task-'A']++;
51+
}
52+
Arrays.sort(taskCounts);
53+
int lastCount = 0; //那最后一个桶大小如何求呢,很明显就是 拥有最多数任务的个数
54+
for (int i = 25; i >= 0; i--) {
55+
if(taskCounts[i] != taskCounts[25]){
56+
break;
57+
}
58+
lastCount++;
59+
}
60+
return Math.max((taskCounts[25] - 1) * (n + 1) + lastCount, tasks.length);
61+
}
62+
}
63+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* 64. Minimum Path Sum
3+
* 最小路径和
4+
*
5+
*
6+
* 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
7+
8+
说明:每次只能向下或者向右移动一步。
9+
10+
示例:
11+
12+
输入:
13+
[
14+
  [1,3,1],
15+
[1,5,1],
16+
[4,2,1]
17+
]
18+
输出: 7
19+
解释: 因为路径 1→3→1→1→1 的总和最小。
20+
21+
*/
22+
public class LeetCode_64_569 {
23+
24+
public static void main(String[] args) {
25+
// TODO Auto-generated method stub
26+
int[][] grid = {
27+
{1, 3, 1 },
28+
{1, 5, 1},
29+
{4, 2, 1}
30+
};
31+
int result = new LeetCode_64_569().new Solution().minPathSum(grid);
32+
System.out.println( result);
33+
}
34+
35+
/*
36+
* 从终点到起点
37+
* 分治: problem[i,j] = min( subproblem[i+1,j], subproblem[i,j+1] )
38+
* 状态表: grid[i,j]
39+
* dp方程: f[i,j] = min( f[i+1,j], f[i,j+1] ) + grid[i,j];
40+
*
41+
*/
42+
43+
class Solution {
44+
public int minPathSum(int[][] grid) {
45+
for(int i = grid.length -1; i>=0; i-- ) {
46+
for( int j = grid[0].length-1; j>=0; j-- ) {
47+
if( i == grid.length-1 && j != grid[0].length -1 ) {
48+
grid[i][j] = grid[i][j] + grid[i][j+1];
49+
}else if( j == grid[0].length-1 && i != grid.length-1 ) {
50+
grid[i][j] = grid[i][j] + grid[i+1][j];
51+
}else if( i != grid.length-1 && j != grid[0].length-1 ) {
52+
grid[i][j] = Math.min(grid[i+1][j], grid[i][j+1]) + grid[i][j];
53+
}
54+
}
55+
}
56+
return grid[0][0];
57+
}
58+
}
59+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/*
3+
* 91. Decode Ways
4+
* 解码方法
5+
* 一条包含字母 A-Z 的消息通过以下方式进行了编码:
6+
7+
'A' -> 1
8+
'B' -> 2
9+
...
10+
'Z' -> 26
11+
给定一个只包含数字的非空字符串,请计算解码方法的总数。
12+
13+
示例 1:
14+
15+
输入: "12"
16+
输出: 2
17+
解释: 它可以解码为 "AB"(1 2)或者 "L"(12)。
18+
示例 2:
19+
20+
输入: "226"
21+
输出: 3
22+
解释: 它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
23+
24+
25+
*/
26+
public class LeetCode_91_569 {
27+
28+
public static void main(String[] args) {
29+
// TODO Auto-generated method stub
30+
String s = "12031";
31+
int result = new LeetCode_91_569().new Solution().numDecodings(s);
32+
System.out.println( result );
33+
}
34+
35+
/*
36+
* 自底而上
37+
* 分治:problem[i] = subproblem[i+1] + subproblem[i+2](i+2如果该双字符不大于26,则i+2=0)
38+
* 状态数组: dp[n+1]
39+
* dp方程:
40+
* 如果s[i]=0, dp[i]=0
41+
* 如果s[i]+s[i+1] <= 26, dp[i] = dp[i+1]+dp[i+2]
42+
* 如果s[i]+s[i+1] > 26, dp[i] = dp[i+1]
43+
*
44+
*/
45+
class Solution {
46+
public int numDecodings(String s) {
47+
int n = s.length();
48+
if( n <= 0)
49+
return 0;
50+
51+
int[] dp = new int[n+1];
52+
dp[n] = 1;
53+
dp[n-1] = s.charAt(n-1) != '0' ? 1 : 0;
54+
55+
for( int i = n-2; i >= 0; i-- ) {
56+
if ( s.charAt(i) == '0') {
57+
dp[i] = 0;
58+
} else if (Integer.parseInt(s.substring(i,i+2)) <= 26 ) {
59+
dp[i] = dp[i+1] + dp[i+2];
60+
} else {
61+
dp[i] = dp[i+1];
62+
}
63+
}
64+
return dp[0];
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)