Updated files: .sync.cmd src/main/java/cn/whaifree/MonotoneStack/LeetCode739.java src/main/java/cn/whaifree/leetCode/LeetCode/LeetCode42.java

This commit is contained in:
whai 2024-04-09 10:50:12 +08:00
parent 30cc40a5ab
commit 040afd5488
3 changed files with 193 additions and 3 deletions

View File

@ -1,5 +1,30 @@
git add .
git commit -m "Sync"
git push origin master
@echo off
setlocal enabledelayedexpansion
git add .
:: 获取暂存区中的更改文件列表
for /f "delims=" %%a in ('git diff --cached --name-only') do (
set "changed_files=!changed_files! %%a"
)
:: 如果没有文件更改,则设置一个默认的提交信息
if "%changed_files%"=="" (
set "commit_message=No files changed"
) else (
:: 设置提交信息,包括修改的文件列表
set "commit_message=Updated files: %changed_files%"
)
:: 执行git commit
git commit -m "%commit_message%"
echo "git push origin master"
git push origin master
echo "git push gitee master"
git push gitee master git push gitee master
echo "git push github master"
git push github master
endlocal

View File

@ -0,0 +1,59 @@
package cn.whaifree.MonotoneStack;
import org.junit.Test;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/8 11:48
* @注释
*/
public class LeetCode739 {
@Test
public void test()
{
int[] temperatures = {73,74,75,71,69,72,76,73};
Solution solution = new Solution();
int[] res = solution.dailyTemperatures(temperatures);
System.out.println(Arrays.toString(res));
}
class Solution {
/**
* 单调栈
*
* 不断把i放入栈
* if nums[i] > stack.peek
* pop = stack.pop
* res[] = i - pop
* else
* stack.push(i)
*
* @param temperatures
* @return
*/
public int[] dailyTemperatures(int[] temperatures) {
int[] res = new int[temperatures.length];
Deque<Integer> stack = new LinkedList<>();
stack.push(0);
for (int i = 1; i < temperatures.length ; i++) {
if (temperatures[i] > temperatures[stack.peek()]) {
// 如果当前比遍历元素比 栈顶的大证明该元素就是栈顶元素右边第一个大于他的值
// 并且需要判断一下当前元素是不是 新栈顶 的下一个大于他的
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
res[stack.peek()] = i - stack.peek();
stack.pop();
}
}
stack.push(i);
}
return res;
}
}
}

View File

@ -0,0 +1,106 @@
package cn.whaifree.leetCode.LeetCode;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/8 11:23
* @注释
*/
public class LeetCode42 {
@Test
public void test()
{
int[] height = new int[]{0,1,0,2,1,0,1,3,2,1,2,1};
int trap = new Solution2().trap(height);
System.out.println(trap);
}
class Solution {
/**
* 从左到右 获得每个位置的左边最高
* 从右到左 获得每个位置的右边最高
*
* 取交集
*
* @param height
* @return
*/
public int trap(int[] height) {
int[] leftHeight = new int[height.length];
int leftH = 0;
for (int i = 0; i < height.length; i++) {
leftH = Math.max(leftH, height[i]);
leftHeight[i] = leftH;
}
int[] rightHeight = new int[height.length];
int rightH = 0;
for (int i = height.length - 1; i >= 0; i--) {
rightH = Math.max(rightH, height[i]);
rightHeight[i] = rightH;
}
int res = 0;
for (int i = 0; i < height.length; i++) {
res += Math.min(rightHeight[i], leftHeight[i]) - height[i];
}
return res;
}
}
class Solution2{
/**
* 关键在于
* - i值 < 栈顶值 入栈
* 3 2 1 递减下一个遇到5时先计算left=2 mid=1 right=5 的容积,再while left=3 mid=1 right=5的容积
* - i值 > 栈顶值 需要计算i为槽时的容积
* - i值 == 栈顶值 更新
* @param height
* @return
*/
public int trap(int[] height) {
// 单调栈 找到右边第一个高于他的墙
// 保证栈内元素从上到下为递增的这样就能获取到槽的两边并且确保不会有一边为空的
Deque<Integer> stack = new LinkedList<>();
stack.push(0);
int res = 0;
for (int i = 1; i < height.length; i++) {
if (height[i] > height[stack.peek()]) {
// 遇到凹陷
//pop up all lower value
int heightAtIdx = height[i]; // 右边墙的高度
// 不断判断左边的墙因为如果左边的墙存在必然大于上一个左墙 如height 3 2 1 5
while (!stack.isEmpty() && (heightAtIdx > height[stack.peek()])){
int mid = stack.pop(); // 中间槽的下标
if (!stack.isEmpty()){
int left = stack.peek(); // 左边墙的下标
int h = Math.min(height[left], height[i]) - height[mid];
int w = i - left - 1;
int hold = h * w;
if (hold > 0) res += hold;
}
}
stack.push(i);
} else if (height[i] == height[stack.peek()]) {
stack.pop();
stack.push(i);
}else {
stack.push(i);
}
}
return res;
}
}
}