leecode更新

This commit is contained in:
markilue 2023-05-05 13:22:09 +08:00
parent 2d9c0306cf
commit 05003d6c49
4 changed files with 217 additions and 3 deletions

View File

@ -0,0 +1,25 @@
package com.markilue.leecode.hot100.interviewHot.difference;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.interviewHot.difference
*@Author: markilue
*@CreateTime: 2023-05-05 11:02
*@Description:
* TODO 力扣1094 拼车:
* 车上最初有 capacity 个空座位 只能 向一个方向行驶也就是说不允许掉头或改变方向
* 给定整数 capacity 和一个数组 trips , trip[i] = [numPassengersi, fromi, toi] 表示第 i 次旅行有 numPassengersi 乘客接他们和放他们的位置分别是 fromi toi
* 这些位置是从汽车的初始位置向东的公里数
* 当且仅当你可以在所有给定的行程中接送所有乘客时返回 true否则请返回 false
*@Version: 1.0
*/
public class LC_1094_CarPooling {
public boolean carPooling(int[][] trips, int capacity) {
return false;
}
}

View File

@ -0,0 +1,106 @@
package com.markilue.leecode.hot100.interviewHot.difference;
import org.junit.Test;
import java.util.Arrays;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.interviewHot.difference
*@Author: markilue
*@CreateTime: 2023-05-05 11:01
*@Description:
* TODO 力扣1109 航班预订统计:
* 这里有 n 个航班它们分别从 1 n 进行编号
* 有一份航班预订表 bookings 表中第 i 条预订记录 bookings[i] = [firsti, lasti, seatsi] 意味着在从 firsti lasti 包含 firsti lasti 每个航班 上预订了 seatsi 个座位
* 请你返回一个长度为 n 的数组 answer里面的元素是每个航班预定的座位总数
*@Version: 1.0
*/
public class LC_1109_CorpFlightBookings {
@Test
public void test(){
int[][] bookings = {{1, 2, 10}, {2, 3, 20},{2, 5, 25}};
int n = 5;
System.out.println(Arrays.toString(corpFlightBookings1(bookings,n)));
}
@Test
public void test1(){
int[][] bookings = {{1, 2, 10}, {2, 2, 15}};
int n = 2;
System.out.println(Arrays.toString(corpFlightBookings2(bookings,n)));
}
/**
* 本人思路:那么直接构造一个数组把booking挨个加起来
* 时间复杂度O(n*m)
* 速度击败8.87% 内存击败5.21% 1249ms
* @param bookings
* @param n
* @return
*/
public int[] corpFlightBookings(int[][] bookings, int n) {
int[] result = new int[n];
for (int[] booking : bookings) {
int start = booking[0];
int end = booking[1];
while (start <= end) {
result[start++ - 1] += booking[2];
}
}
return result;
}
/**
* 差分的思想:
* 当我们希望对原数组的某一个区间 [l,r]施加一个增量inc时差分数组d对应的改变是d[l]增加incd[r+1]减少inc
* 速度击败100% 内存击败5.21% 2ms
* @param bookings
* @param n
* @return
*/
public int[] corpFlightBookings1(int[][] bookings, int n) {
int[] result = new int[n+1];
for (int[] booking : bookings) {
int start = booking[0];
int end = booking[1];
result[start-1] += booking[2];
result[end] -= booking[2];
}
//差分数组最后的结果:10 45 -10 -20 0 -25
//TODO 为什么result[n+1]对应的位置一定是0?
//因为原本就是0无论怎么算后来也是0
for (int i = 1; i <= n; i++) {
result[i] += result[i - 1];
}
return Arrays.copyOf(result,n);
}
public int[] corpFlightBookings2(int[][] bookings, int n) {
int[] nums = new int[n];
for (int[] booking : bookings) {
nums[booking[0] - 1] += booking[2];
if (booking[1] < n) {
nums[booking[1]] -= booking[2];
}
}
for (int i = 1; i < n; i++) {
nums[i] += nums[i - 1];
}
return nums;
}
}

View File

@ -138,4 +138,37 @@ public class T38_84_LargestRectangleArea {
return ans; return ans;
} }
public int largestRectangleArea3(int[] heights) {
int n = heights.length;
int[] left = new int[n];
int[] right = new int[n];
LinkedList<Integer> stack = new LinkedList<>();
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {//找到第一个比他小的数就停止
stack.pop();
}
left[i] = stack.isEmpty() ? -1 : stack.peek();
stack.push(i);
}
stack.clear();
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {//找到第一个比他小的数就停止
stack.pop();
}
right[i] = stack.isEmpty() ? n : stack.peek();
stack.push(i);
}
int maxResult = 0;
for (int i = 0; i < n; i++) {
maxResult = Math.max((right[i] - left[i] - 1) * heights[i], maxResult);
}
return maxResult;
}
} }

View File

@ -69,9 +69,9 @@ public class Question1 {
@Test @Test
public void test() { public void test() {
int[][] nums = new int[][]{{2, 5}, {8, 9}}; // int[][] nums = new int[][]{{2, 5}, {8, 9}};
// int[][] nums = new int[][]{{4,8}, {1,6},{2,9}}; int[][] nums = new int[][]{{4, 8}, {1, 6}, {2, 9}};
solve1(nums); solve2(nums);
} }
public void solve(int[][] nums) { public void solve(int[][] nums) {
@ -181,4 +181,54 @@ public class Question1 {
System.out.println(result); System.out.println(result);
} }
/**
* 差分法尝试:
* 两个示例都对
* @param nums
*/
public void solve2(int[][] nums) {
//1.寻找区间最大值和最小值
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int[] num : nums) {
if (min > num[0]) min = num[0];
if (max < num[1]) max = num[1];
}
//构造差分数组
int[] diff = new int[max - min + 1];
for (int[] num : nums) {
diff[num[0] - min] += 1;
int end = num[1] + 1 - min;
if (end < diff.length) {//超出位置可以不进行计算
diff[end] -= 1;
}
}
//通过差分数组还原原本数,并计算这个位置的能耗
int result = 0;
if (diff[0] == 1) {
result = 3;
} else if (diff[0] >= 2) {
result = 4;
}
for (int i = 1; i < diff.length; i++) {
diff[i] += diff[i - 1];
if (diff[i] == 1) {
result += 3;
} else if (diff[i] == 0) {
result += 1;
} else {
result += 4;
}
}
System.out.println(result);
}
} }