Skip to content

Commit ea9fe98

Browse files
author
haotf
committed
动态规划
1 parent 599cf77 commit ea9fe98

20 files changed

+450
-0
lines changed

1024.视频拼接.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.Arrays;
2+
3+
/*
4+
* @lc app=leetcode.cn id=1024 lang=java
5+
*
6+
* [1024] 视频拼接
7+
*/
8+
9+
// @lc code=start
10+
class Solution {
11+
public int videoStitching(int[][] clips, int time) {
12+
if (time == 0)
13+
return 0;
14+
// 按起点升序排列,起点相同的降序排列
15+
Arrays.sort(clips, (a, b) -> {
16+
if (a[0] == b[0]) {
17+
return b[1] - a[1];
18+
}
19+
return a[0] - b[0];
20+
});
21+
// 记录选择的短视频个数
22+
int res = 0;
23+
24+
int curEnd = 0, nextEnd = 0;
25+
int i = 0, n = clips.length;
26+
while (i < n && clips[i][0] <= curEnd) {
27+
// 在第 res 个视频的区间内贪心选择下一个视频
28+
while (i < n && clips[i][0] <= curEnd) {
29+
nextEnd = Math.max(nextEnd, clips[i][1]);
30+
i++;
31+
}
32+
// 找到下一个视频,更新 curEnd
33+
res++;
34+
curEnd = nextEnd;
35+
if (curEnd >= time) {
36+
// 已经可以拼出区间 [0, T]
37+
return res;
38+
}
39+
}
40+
// 无法连续拼出区间 [0, T]
41+
return -1;
42+
}
43+
}
44+
// @lc code=end

1109.航班预订统计.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode.cn id=1109 lang=java
3+
*
4+
* [1109] 航班预订统计
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int[] corpFlightBookings(int[][] bookings, int n) {
10+
int[] diff = new int[n];
11+
for (int i = 0; i < bookings.length; i++) {
12+
int start = bookings[i][0];
13+
int end = bookings[i][1];
14+
int value = bookings[i][2];
15+
diff[start - 1] = diff[start - 1] + value;
16+
if (end < n) {
17+
diff[end] = diff[end] - value;
18+
}
19+
}
20+
int[] res = new int[n];
21+
res[0] = diff[0];
22+
for (int i = 1; i < diff.length; i++) {
23+
res[i] = diff[i] + res[i - 1];
24+
}
25+
return res;
26+
}
27+
}
28+
// @lc code=end

1143.最长公共子序列.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode.cn id=1143 lang=java
3+
*
4+
* [1143] 最长公共子序列
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int longestCommonSubsequence(String text1, String text2) {
10+
int len1 = text1.length();
11+
int len2 = text2.length();
12+
int[][] dp = new int[len1 + 1][len2 + 1];
13+
for (int i = 1; i <= len1; i++) {
14+
for (int j = 1; j <= len2; j++) {
15+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
16+
dp[i][j] = dp[i - 1][j - 1] + 1;
17+
} else {
18+
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
19+
}
20+
}
21+
}
22+
return dp[len1][len2];
23+
}
24+
}
25+
// @lc code=end

130.被围绕的区域.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode.cn id=130 lang=java
3+
*
4+
* [130] 被围绕的区域
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public void solve(char[][] board) {
10+
11+
}
12+
}
13+
// @lc code=end
14+

1514.概率最大的路径.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode.cn id=1514 lang=java
3+
*
4+
* [1514] 概率最大的路径
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
10+
11+
}
12+
}
13+
// @lc code=end
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode.cn id=1584 lang=java
3+
*
4+
* [1584] 连接所有点的最小费用
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int minCostConnectPoints(int[][] points) {
10+
11+
}
12+
}
13+
// @lc code=end
14+

1631.最小体力消耗路径.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode.cn id=1631 lang=java
3+
*
4+
* [1631] 最小体力消耗路径
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int minimumEffortPath(int[][] heights) {
10+
11+
}
12+
}
13+
// @lc code=end
14+

207.课程表.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode.cn id=207 lang=java
3+
*
4+
* [207] 课程表
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public boolean canFinish(int numCourses, int[][] prerequisites) {
10+
11+
}
12+
}
13+
// @lc code=end
14+

210.课程表-ii.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode.cn id=210 lang=java
3+
*
4+
* [210] 课程表 II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int[] findOrder(int numCourses, int[][] prerequisites) {
10+
11+
}
12+
}
13+
// @lc code=end
14+

300.最长递增子序列.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode.cn id=300 lang=java
3+
*
4+
* [300] 最长递增子序列
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int lengthOfLIS(int[] nums) {
10+
int[] memo = new int[nums.length];
11+
12+
int max = 1;
13+
for (int i = 0; i < memo.length; i++) {
14+
memo[i] = 1;
15+
}
16+
17+
for (int i = 0; i < memo.length; i++) {
18+
for (int j = i; j >= 0; j--) {
19+
if (nums[j] < nums[i]) {
20+
memo[i] = Math.max(memo[j] + 1, memo[i]);
21+
}
22+
}
23+
max = Math.max(max, memo[i]);
24+
}
25+
26+
return max;
27+
}
28+
}
29+
// @lc code=end

435.无重叠区间.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
4+
/*
5+
* @lc app=leetcode.cn id=435 lang=java
6+
*
7+
* [435] 无重叠区间
8+
*/
9+
10+
// @lc code=start
11+
class Solution {
12+
public int eraseOverlapIntervals(int[][] intervals) {
13+
14+
if (intervals.length == 0)
15+
return 0;
16+
17+
Arrays.sort(intervals, new Comparator<int[]>() {
18+
@Override
19+
public int compare(int[] a, int[] b) {
20+
return a[1] - b[1];
21+
}
22+
});
23+
24+
int end = intervals[0][1];
25+
int count = 0;
26+
27+
for (int i = 1; i < intervals.length; i++) {
28+
int start = intervals[i][0];
29+
if (start < end) {
30+
count++;
31+
} else {
32+
end = intervals[i][1];
33+
}
34+
}
35+
36+
return count;
37+
}
38+
}
39+
// @lc code=end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
4+
/*
5+
* @lc app=leetcode.cn id=452 lang=java
6+
*
7+
* [452] 用最少数量的箭引爆气球
8+
*/
9+
10+
// @lc code=start
11+
class Solution {
12+
public int findMinArrowShots(int[][] points) {
13+
if (points.length == 0)
14+
return 0;
15+
16+
Arrays.sort(points, new Comparator<int[]>() {
17+
@Override
18+
public int compare(int[] a, int[] b) {
19+
return a[1] - b[1];
20+
}
21+
});
22+
23+
int end = points[0][1];
24+
int count = 1;
25+
26+
for (int i = 0; i < points.length; i++) {
27+
int start = points[i][0];
28+
if (start > end) {
29+
count++;
30+
end = points[i][1];
31+
}
32+
}
33+
return count;
34+
}
35+
}
36+
// @lc code=end

518.零钱兑换-ii.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @lc app=leetcode.cn id=518 lang=java
3+
*
4+
* [518] 零钱兑换 II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int change(int amount, int[] coins) {
10+
int[] dp = new int[amount + 1];
11+
dp[0] = 1;
12+
13+
for (int coin : coins) {
14+
for (int i = coin; i <= amount; i++) {
15+
if (i - coin >= 0) {
16+
dp[i] = dp[i] + dp[i - coin];
17+
}
18+
}
19+
}
20+
return dp[amount];
21+
}
22+
}
23+
// @lc code=end

53.最大子数组和.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @lc app=leetcode.cn id=53 lang=java
3+
*
4+
* [53] 最大子数组和
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int maxSubArray(int[] nums) {
10+
int max = Integer.MIN_VALUE;
11+
int sum = 0;
12+
13+
for (int i = 0; i < nums.length; i++) {
14+
if (sum < 0)
15+
sum = 0;
16+
sum = sum + nums[i];
17+
max = Math.max(sum, max);
18+
}
19+
20+
return max;
21+
}
22+
}
23+
// @lc code=end

743.网络延迟时间.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode.cn id=743 lang=java
3+
*
4+
* [743] 网络延迟时间
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int networkDelayTime(int[][] times, int n, int k) {
10+
11+
}
12+
}
13+
// @lc code=end
14+

785.判断二分图.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode.cn id=785 lang=java
3+
*
4+
* [785] 判断二分图
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public boolean isBipartite(int[][] graph) {
10+
11+
}
12+
}
13+
// @lc code=end
14+

797.所有可能的路径.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode.cn id=797 lang=java
3+
*
4+
* [797] 所有可能的路径
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
10+
11+
}
12+
}
13+
// @lc code=end
14+

0 commit comments

Comments
 (0)