leecode更新

This commit is contained in:
markilue 2023-05-06 13:26:05 +08:00
parent 1f12cec9f0
commit 47beed6aa1
7 changed files with 359 additions and 23 deletions

View File

@ -1,5 +1,7 @@
package com.markilue.leecode.hot100.interviewHot.difference;
import org.junit.Test;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.interviewHot.difference
@ -15,11 +17,59 @@ package com.markilue.leecode.hot100.interviewHot.difference;
*/
public class LC_1094_CarPooling {
@Test
public void test() {
int[][] trips = {{2, 1, 5}, {
3, 5, 7
}};
int capacity = 4;
System.out.println(carPooling(trips, capacity));
}
/**
* 差分数组:本质上就是获得每个位置的一个大小判断是否超过capacity
* 而数组又是范围型累加所以可以使用差分数组法
* 速度击败99.33% 内存击败5.5% 1ms
* @param trips
* @param capacity
* @return
*/
public boolean carPooling(int[][] trips, int capacity) {
//构造差分数组
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int[] trip : trips) {
if (trip[1] < min) min = trip[1];
if (trip[2] > max) max = trip[2];
}
//通过最大值最小值构建差分数组
int[] diff = new int[max - min + 1];
return false;
for (int[] trip : trips) {
diff[trip[1] - min] += trip[0];
int end = trip[2] - min;//当前就需要减掉
if (end < diff.length) {
diff[end] -= trip[0];
}
}
if (diff[0] > capacity) {
return false;
}
//挨个遍历判断是否合适
for (int i = 1; i < diff.length; i++) {
diff[i] += diff[i - 1];
if (diff[i] > capacity) {
return false;
}
}
return true;
}
}

View File

@ -19,17 +19,17 @@ import java.util.Arrays;
public class LC_1109_CorpFlightBookings {
@Test
public void test(){
int[][] bookings = {{1, 2, 10}, {2, 3, 20},{2, 5, 25}};
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)));
System.out.println(Arrays.toString(corpFlightBookings1(bookings, n)));
}
@Test
public void test1(){
public void test1() {
int[][] bookings = {{1, 2, 10}, {2, 2, 15}};
int n = 2;
System.out.println(Arrays.toString(corpFlightBookings2(bookings,n)));
System.out.println(Arrays.toString(corpFlightBookings2(bookings, n)));
}
@ -68,12 +68,12 @@ public class LC_1109_CorpFlightBookings {
*/
public int[] corpFlightBookings1(int[][] bookings, int n) {
int[] result = new int[n+1];
int[] result = new int[n + 1];
for (int[] booking : bookings) {
int start = booking[0];
int end = booking[1];
result[start-1] += booking[2];
result[start - 1] += booking[2];
result[end] -= booking[2];
}
@ -84,7 +84,7 @@ public class LC_1109_CorpFlightBookings {
result[i] += result[i - 1];
}
return Arrays.copyOf(result,n);
return Arrays.copyOf(result, n);
}
@ -103,4 +103,32 @@ public class LC_1109_CorpFlightBookings {
}
return nums;
}
/**
* 二刷:差分
* @param bookings
* @param n
* @return
*/
public int[] corpFlightBookings3(int[][] bookings, int n) {
//构建差分数组
int[] diff = new int[n];
for (int[] booking : bookings) {
diff[booking[0] - 1] += booking[2];
if (booking[1] < n) {
diff[booking[1]] -= booking[2];
}
}
//通过差分数组还原当前位置
for (int i = 1; i < diff.length; i++) {
diff[i] += diff[i - 1];
}
return diff;
}
}

View File

@ -25,7 +25,7 @@ public class T35_76_MinWindow {
@Test
public void test1() {
String s = "ADOBECODEBANC", t = "ABC";
System.out.println(minWindow1(s, t));
System.out.println(minWindow2(s, t));
}
@ -111,7 +111,7 @@ public class T35_76_MinWindow {
}
while (left < right && count == need.size()) {
int length = right - left ;
int length = right - left;
if (length < result) {
result = length;
start = left;
@ -119,7 +119,7 @@ public class T35_76_MinWindow {
char c1 = s.charAt(left++);
if (need.containsKey(c1)) {
Integer num = window.get(c1);
if (num .equals(need.get(c1)) ) {
if (num.equals(need.get(c1))) {
count--;
}
window.put(c1, num - 1);
@ -131,4 +131,55 @@ public class T35_76_MinWindow {
return result == Integer.MAX_VALUE ? "" : s.substring(start, start + result);
}
public String minWindow2(String s, String t) {
HashMap<Character, Integer> need = new HashMap<>();
//遍历t,获取每个字母需要的个数
for (int i = 0; i < t.length(); i++) {
char c = t.charAt(i);
need.put(c, need.getOrDefault(c, 0) + 1);
}
HashMap<Character, Integer> window = new HashMap<>();//窗口
int left = 0;
int right = 0;
int diff = need.size();
int minLength = Integer.MAX_VALUE;
int start = 0;
while (right < s.length()) {
char c = s.charAt(right++);
if (need.containsKey(c)) {
Integer count = window.getOrDefault(c, 0);
if (count.equals(need.get(c) - 1)) {
diff--;
}
window.put(c, count + 1);
}
while (diff == 0 && left < right) {
int curLength = right - left;//由于之前right++,所以这里不用+1了
if (curLength < minLength) {
minLength = curLength;
start = left;
}
char c1 = s.charAt(left++);
if (need.containsKey(c1)) {
Integer count = window.getOrDefault(c1, 0);
if (count.equals(need.get(c1))) {
diff++;
}
window.put(c1, count - 1);
}
}
}
return minLength == Integer.MAX_VALUE ? "" : s.substring(start, start + minLength);
}
}

View File

@ -0,0 +1,89 @@
package com.markilue.leecode.hot100.second;
import com.markilue.leecode.tree.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.second
*@Author: markilue
*@CreateTime: 2023-05-06 12:51
*@Description:
* TODO 力扣94 二叉树的中序遍历:
* 给定一个二叉树的根节点 root 返回 它的 中序 遍历
*@Version: 1.0
*/
public class T40_94_InorderTraversal {
//递归法
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
inorderTraversal(root, list);
return list;
}
public void inorderTraversal(TreeNode root, List<Integer> list) {
if (root == null) {
return;
}
inorderTraversal(root.left, list);
list.add(root.val);
inorderTraversal(root.right, list);
}
//morris遍历法
public List<Integer> inorderTraversal1(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
while (root != null) {
if (root.left != null) {
TreeNode left = root.left;
while (left.right != null && left.right != root) {
left = left.right;
}
//判断是怎么出来的
if (left.right == null) {
left.right = root;
//放心将root左移
root = root.left;
} else {
//第二次到这里了
list.add(root.val);
root = root.right;
left.right = null;
}
} else {
list.add(root.val);
root = root.right;
}
}
return list;
}
//stack栈法
public List<Integer> inorderTraversal2(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
LinkedList<TreeNode> stack = new LinkedList<>();
while (!stack.isEmpty() || root != null) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
TreeNode cur = stack.pop();
result.add(cur.val);
root = cur.right;
}
}
return result;
}
}

View File

@ -0,0 +1,35 @@
package com.markilue.leecode.hot100.second;
import org.junit.Test;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.second
*@Author: markilue
*@CreateTime: 2023-05-06 13:18
*@Description:
* TODO 力扣96 不同的二叉搜索树:
* 给你一个整数 n 求恰由 n 个节点组成且节点值从 1 n 互不相同的 二叉搜索树 有多少种返回满足题意的二叉搜索树的种数
*@Version: 1.0
*/
public class T41_96_NumTrees {
public int numTrees(int n) {
if (n <= 2) {
return n;
}
int[] dp = new int[n + 1];
dp[1] = 1;
dp[0] = 1;
for (int i = 2; i < dp.length; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[i - j - 1];
}
}
return dp[n];
}
}

46
interview/MeiTuan/pom.xml Normal file
View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.markilue.interview</groupId>
<artifactId>MeiTuan</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -30,9 +30,9 @@ public class Question2 {
@Test
public void test() {
int k=3;
int[] nums={1 ,2 ,3 ,2 ,1 ,4, 5, 1};
sovle(k,nums);
int k = 3;
int[] nums = {1, 2, 3, 2, 1, 4, 5, 1};
solve(k, nums);
}
@ -43,15 +43,15 @@ public class Question2 {
int right = 0;
int result=0;
int result = 0;
HashMap<Integer, Integer> map = new HashMap<>();//<nums,count>
while (right < nums.length) {
while (right < nums.length&&map.size() <= k) {
while (right < nums.length && map.size() <= k) {
if (!map.isEmpty() && map.containsKey(nums[right])) {
Integer count = map.getOrDefault(nums[right], 0);
map.put(nums[right], count+1);
map.put(nums[right], count + 1);
} else {
map.put(nums[right], 1);
}
@ -59,16 +59,16 @@ public class Question2 {
right++;
}
while (left<=right&&map.size() > k) {
while (left <= right && map.size() > k) {
//缩小窗口
if(right-left-1>result){
result=right-left-1;
if (right - left - 1 > result) {
result = right - left - 1;
}
if (!map.isEmpty() && map.containsKey(nums[left])) {
Integer count = map.getOrDefault(nums[left], 0);
map.put(nums[left], count-1);
if(count==1)map.remove(nums[left]);
map.put(nums[left], count - 1);
if (count == 1) map.remove(nums[left]);
}
left++;
}
@ -78,4 +78,41 @@ public class Question2 {
}
public void solve(int k, int[] nums) {
HashMap<Integer, Integer> window = new HashMap<>();//count
int left = 0;
int right = 0;
int count = 0;
int maxLength = Integer.MIN_VALUE;
while (right < nums.length) {
int num = nums[right++];
if (!window.containsKey(num)) {
count++;
}
window.put(num, window.getOrDefault(num, 0) + 1);
if (count == k) {
int curLength = right - left;
if (curLength > maxLength) {
maxLength = curLength;
}
}
while (count > k && left < right) {
int num1 = nums[left++];
int numLeft = window.getOrDefault(num1, 0) - 1;
if (numLeft == 0) {
window.remove(num1);
count--;
}
}
}
System.out.println(maxLength);
}
}