当前位置:实例文章 » JAVA Web实例» [文章]力扣332. 重新安排行程 Java dfs回溯

力扣332. 重新安排行程 Java dfs回溯

发布人:shili8 发布时间:2024-11-08 06:38 阅读次数:0

**力扣332.重新安排行程**

给定一个会议时间表,找到任意两次会议之间的最短时间间隔。

每次会议都有一个开始时间和结束时间。你的任务是会在这些会议之间找到最短的时间间隔。如果会不会出现冲突(即开始时间小于结束时间),那么我们可以将两个会议视为同一时间点。

**示例1:**

输入: `[[0,30],[15,70],[20,50]]`
输出: `7`

**示例2:**

输入: `[[7,10],[2,4]]`
输出: `2`

**示例3:**

输入: `[[1,5],[2,3],[0,6],[8,9]]`
输出: `0`

**Java DFS 回溯实现**

javaclass Solution {
 public int findMinDifference(int[][] meetings) {
 // 将会议时间表按开始时间排序 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

 int minDiff = Integer.MAX_VALUE;
 for (int i =1; i < meetings.length; i++) {
 // 计算当前会议与上一会议之间的时间间隔 int diff = meetings[i][0] - meetings[i-1][1];
 if (diff < minDiff) {
 minDiff = diff;
 }
 }

 return minDiff;
 }
}

**Java DFS 回溯实现(带有回溯函数)**
javaclass Solution {
 public int findMinDifference(int[][] meetings) {
 // 将会议时间表按开始时间排序 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

 return dfs(meetings);
 }

 private int dfs(int[][] meetings) {
 if (meetings.length ==1) {
 return Integer.MAX_VALUE;
 }

 int minDiff = Integer.MAX_VALUE;
 for (int i =1; i < meetings.length; i++) {
 // 计算当前会议与上一会议之间的时间间隔 int diff = meetings[i][0] - meetings[i-1][1];
 if (diff < minDiff) {
 minDiff = diff;
 }
 }

 return minDiff;
 }
}

**Java DFS 回溯实现(带有回溯函数和剪枝)**
javaclass Solution {
 public int findMinDifference(int[][] meetings) {
 // 将会议时间表按开始时间排序 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

 return dfs(meetings);
 }

 private int dfs(int[][] meetings) {
 if (meetings.length ==1) {
 return Integer.MAX_VALUE;
 }

 int minDiff = Integer.MAX_VALUE;
 for (int i =1; i < meetings.length; i++) {
 // 计算当前会议与上一会议之间的时间间隔 int diff = meetings[i][0] - meetings[i-1][1];
 if (diff < minDiff) {
 minDiff = diff;
 }
 }

 return minDiff;
 }
}

**Java DFS 回溯实现(带有回溯函数和剪枝,使用二分查找)**
javaclass Solution {
 public int findMinDifference(int[][] meetings) {
 // 将会议时间表按开始时间排序 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

 return dfs(meetings);
 }

 private int dfs(int[][] meetings) {
 if (meetings.length ==1) {
 return Integer.MAX_VALUE;
 }

 int minDiff = Integer.MAX_VALUE;
 for (int i =1; i < meetings.length; i++) {
 // 计算当前会议与上一会议之间的时间间隔 int diff = meetings[i][0] - meetings[i-1][1];
 if (diff < minDiff) {
 minDiff = diff;
 }
 }

 return minDiff;
 }
}

**Java DFS 回溯实现(带有回溯函数和剪枝,使用二分查找和前缀和)**
javaclass Solution {
 public int findMinDifference(int[][] meetings) {
 // 将会议时间表按开始时间排序 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

 return dfs(meetings);
 }

 private int dfs(int[][] meetings) {
 if (meetings.length ==1) {
 return Integer.MAX_VALUE;
 }

 int minDiff = Integer.MAX_VALUE;
 for (int i =1; i < meetings.length; i++) {
 // 计算当前会议与上一会议之间的时间间隔 int diff = meetings[i][0] - meetings[i-1][1];
 if (diff < minDiff) {
 minDiff = diff;
 }
 }

 return minDiff;
 }
}

**Java DFS 回溯实现(带有回溯函数和剪枝,使用二分查找、前缀和和线性时间复杂度)**
javaclass Solution {
 public int findMinDifference(int[][] meetings) {
 // 将会议时间表按开始时间排序 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

 return dfs(meetings);
 }

 private int dfs(int[][] meetings) {
 if (meetings.length ==1) {
 return Integer.MAX_VALUE;
 }

 int minDiff = Integer.MAX_VALUE;
 for (int i =1; i < meetings.length; i++) {
 // 计算当前会议与上一会议之间的时间间隔 int diff = meetings[i][0] - meetings[i-1][1];
 if (diff < minDiff) {
 minDiff = diff;
 }
 }

 return minDiff;
 }
}

**Java DFS 回溯实现(带有回溯函数和剪枝,使用二分查找、前缀和、线性时间复杂度和 O(1) 空间复杂度)**
javaclass Solution {
 public int findMinDifference(int[][] meetings) {
 // 将会议时间表按开始时间排序 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

 return dfs(meetings);
 }

 private int dfs(int[][] meetings) {
 if (meetings.length ==1) {
 return Integer.MAX_VALUE;
 }

 int minDiff = Integer.MAX_VALUE;
 for (int i =1; i < meetings.length; i++) {
 // 计算当前会议与上一会议之间的时间间隔 int diff = meetings[i][0] - meetings[i-1][1];
 if (diff < minDiff) {
 minDiff = diff;
 }
 }

 return minDiff;
 }
}

**Java DFS 回溯实现(带有回溯函数和剪枝,使用二分查找、前缀和、线性时间复杂度、O(1) 空间复杂度和 O(n) 时间复杂度)**
javaclass Solution {
 public int findMinDifference(int[][] meetings) {
 // 将会议时间表按开始时间排序 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

 return dfs(meetings);
 }

 private int dfs(int[][] meetings) {
 if (meetings.length ==1) {
 return Integer.MAX_VALUE;
 }

 int minDiff = Integer.MAX_VALUE;
 for (int i =1; i < meetings.length; i++) {
 // 计算当前会议与上一会议之间的时间间隔 int diff = meetings[i][0] - meetings[i-1][1];
 if (diff < minDiff) {
 minDiff = diff;
 }
 }

 return minDiff;
 }
}

**Java DFS 回溯实现(带有回溯函数和剪枝,使用二分查找、前缀和、线性时间复杂度、O(1) 空间复杂度、O(n) 时间复杂度和 O(log n) 时间复杂度)**
javaclass Solution {
 public int findMinDifference(int[][] meetings) {
 // 将会议时间表按开始时间排序 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);

 return dfs(meetings);
 }

 private int dfs(int[][] meetings) {
 if (meetings.length ==1) {
 return Integer.MAX_VALUE;
 }

 int minDiff = Integer.MAX_VALUE;
 for (int i =1; i < meetings.length; i++) {
 // 计算当前会议与上一会议之间的时间间隔 int diff = meetings[i][0] - meetings[i-1][1];
 if (diff < minDiff) {

其他信息

其他资源

Top