当前位置: 首页 > news >正文

如何利用视频网站做推广请输入搜索关键词

如何利用视频网站做推广,请输入搜索关键词,佛山网站建设企业推荐,网站后台编辑器控件下载42. 接雨水 暴力法 for循环遍历每一个柱子,内层for循环找到左边和右边比它高的柱子 时间复杂度 n^2 优化:添加一个预处理 定义一个数组,存放该柱子右边比他高的柱子是哪一个 再用一个数组,存放该柱子左边比他高的柱子是哪一个 …

42. 接雨水

暴力法

for循环遍历每一个柱子,内层for循环找到左边和右边比它高的柱子
时间复杂度 n^2

优化:添加一个预处理
定义一个数组,存放该柱子右边比他高的柱子是哪一个
再用一个数组,存放该柱子左边比他高的柱子是哪一个

单调栈

单调栈:在每日温度题目中,其要找到右边第一个比他大的温度;适配到本题,就是找到当前柱子左边&右边 第一个比它大的温度

遍历一次就能处理完:当前遍历的元素比栈口元素大的时候,说明栈口柱子右边比它大的那个柱子找到了,它左边比它大的柱子怎么找?在栈中。
在这里插入图片描述

class Solution {public int trap(int[] height){int size = height.length;if (size <= 2) return 0;// in the stack, we push the index of array// using height[] to access the real heightStack<Integer> stack = new Stack<Integer>();stack.push(0);int sum = 0;for (int index = 1; index < size; index++){int stackTop = stack.peek();if (height[index] < height[stackTop]){stack.push(index);}else if (height[index] == height[stackTop]){// 因为相等的相邻墙,左边一个是不可能存放雨水的,所以pop左边的index, push当前的indexstack.pop();stack.push(index);}else{//pop up all lower valueint heightAtIdx = height[index];while (!stack.isEmpty() && (heightAtIdx > height[stackTop])){int mid = stack.pop();if (!stack.isEmpty()){int left = stack.peek();int h = Math.min(height[left], height[index]) - height[mid];int w = index - left - 1;int hold = h * w;if (hold > 0) sum += hold; //加不加大于0都一样stackTop = stack.peek();}}stack.push(index);}}return sum;}
}
  • 自己再做了一遍
class Solution {public int trap(int[] height) {if(height.length <= 2) {return 0;}Stack<Integer> st = new Stack<>(); //存的是下标st.push(0);int sum = 0;for(int i=1; i<height.length; i++) {// i 下标  height[i]值// height[st.peek()]  height[st.pop()]if(height[i] < height[st.peek()]) {st.push(i);} else if(height[i] == height[st.peek()]) {st.pop();st.push(i); //虽然值是一样的,但下标不一样} else { //发现凹槽了// int stackTop = st.peek();int right = i; //凹槽右侧高度while(!st.empty() && height[right] > height[st.peek()]) {int mid = st.pop();//凹点if(!st.empty()) {int left = st.peek();int w = right-left-1;int h = Math.min(height[left], height[right]) - height[mid];int hold = h*w;sum += hold;}}st.push(i);//体积 = w * h }}return sum;}
}

双指针

与单调栈不同的是,双指针求体积求的是 竖向的体积

class Solution {public int trap(int[] height) {int len = height.length;if(len <= 2) return 0;int[]maxLeft = new int[len];int[]maxRight = new int[len];maxLeft[0] = height[0];for(int i=1; i<len; i++) {maxLeft[i] = Math.max(height[i], maxLeft[i-1]);}maxRight[len-1] = height[len-1];for(int i=len-2; i>=0; i--) {// maxLeft[i] = Math.max(height[i], maxLeft[i-1]);maxRight[i] = Math.max(height[i],maxRight[i+1]);}int sum = 0;for(int i=0; i<len; i++) {int h = Math.min(maxLeft[i],maxRight[i]) - height[i];if(h>0) sum+=h; // h*1}return sum;}
}

84.柱状图中最大的矩形

emmm 和接雨水到底哪里一样了???

暴力法 双指针

遍历每一根柱子,向左向右找比它小的边界,算出面积并求最大
优化:两个数组,一个存放i左边比他矮的,一个存放i右边

单调栈

写法不一样了,栈中元素 从栈顶到栈底要 从大到小。

遍历一次就能处理完:当前遍历的元素比栈口元素小的时候,说明栈口柱子右边比它小的那个柱子找到了,它左边比它小的柱子怎么找?在栈中。

  • 首尾补0
    尾补0:[2,4,6,8]
    首补0:[8,6,4,2]
class Solution {public int largestRectangleArea(int[] heights) {int[]newHeight = new int[heights.length+2];for(int i=1; i<=heights.length; i++) {newHeight[i] = heights[i-1];}heights = newHeight; //重定向Stack<Integer> st = new Stack<Integer>();st.push(0);int res=0;for(int i=1; i<heights.length; i++) {if(heights[i]>heights[st.peek()]) {st.push(i);} else if(heights[i] == heights[st.peek()]) {st.pop();st.push(i);} else {//栈可能为空吗?while(heights[i] < heights[st.peek()]) {int mid = st.peek();st.pop();int left = st.peek();int right = i;int w = right - left - 1;int h = heights[mid];res = Math.max(res, w * h);}st.push(i);}}return res;}
}

单调栈最核心的其实是栈顶元素


文章转载自:
http://ischiadic.c7629.cn
http://lampadephoria.c7629.cn
http://mousse.c7629.cn
http://demander.c7629.cn
http://abuse.c7629.cn
http://joyo.c7629.cn
http://peacocky.c7629.cn
http://britainic.c7629.cn
http://recycle.c7629.cn
http://panplegia.c7629.cn
http://hypnus.c7629.cn
http://jargonize.c7629.cn
http://diecious.c7629.cn
http://crater.c7629.cn
http://cellist.c7629.cn
http://octameter.c7629.cn
http://galligaskins.c7629.cn
http://nematocystic.c7629.cn
http://frailly.c7629.cn
http://retrorse.c7629.cn
http://woven.c7629.cn
http://laudanum.c7629.cn
http://syllogistically.c7629.cn
http://bulldagger.c7629.cn
http://revolvable.c7629.cn
http://amitriptyline.c7629.cn
http://gangetic.c7629.cn
http://painty.c7629.cn
http://unauthenticated.c7629.cn
http://agriculturalist.c7629.cn
http://hydatid.c7629.cn
http://uniseptate.c7629.cn
http://syncaine.c7629.cn
http://panpipe.c7629.cn
http://dehors.c7629.cn
http://adolescency.c7629.cn
http://cism.c7629.cn
http://octocentenary.c7629.cn
http://thunderboat.c7629.cn
http://ergograph.c7629.cn
http://restraint.c7629.cn
http://craft.c7629.cn
http://slightly.c7629.cn
http://huskiness.c7629.cn
http://larkiness.c7629.cn
http://piezocrystallization.c7629.cn
http://modulo.c7629.cn
http://madonna.c7629.cn
http://rumbling.c7629.cn
http://hunt.c7629.cn
http://moronic.c7629.cn
http://disquieting.c7629.cn
http://tetrachotomous.c7629.cn
http://massagist.c7629.cn
http://motherliness.c7629.cn
http://commonwealth.c7629.cn
http://aneurismal.c7629.cn
http://vindicatory.c7629.cn
http://balanced.c7629.cn
http://forehold.c7629.cn
http://electrolier.c7629.cn
http://adsorptive.c7629.cn
http://subclassify.c7629.cn
http://museque.c7629.cn
http://chucker.c7629.cn
http://veinule.c7629.cn
http://roughage.c7629.cn
http://interleaf.c7629.cn
http://ivory.c7629.cn
http://gastrostege.c7629.cn
http://glucinium.c7629.cn
http://neuralgia.c7629.cn
http://formulable.c7629.cn
http://waziristan.c7629.cn
http://balliness.c7629.cn
http://palaeontography.c7629.cn
http://accessable.c7629.cn
http://thermogeography.c7629.cn
http://neoterist.c7629.cn
http://guarantor.c7629.cn
http://everblooming.c7629.cn
http://lewes.c7629.cn
http://veblenian.c7629.cn
http://wilhelm.c7629.cn
http://maladaptation.c7629.cn
http://demonstrably.c7629.cn
http://floridan.c7629.cn
http://navajo.c7629.cn
http://ted.c7629.cn
http://gestate.c7629.cn
http://allocate.c7629.cn
http://aplastic.c7629.cn
http://dryopithecine.c7629.cn
http://mcd.c7629.cn
http://dextrous.c7629.cn
http://metacarpus.c7629.cn
http://nasute.c7629.cn
http://outlaw.c7629.cn
http://digenetic.c7629.cn
http://overprescribe.c7629.cn
http://www.zhongyajixie.com/news/92895.html

相关文章:

  • b2b都有哪些太原关键词优化报价
  • 西安微网站制作搜索词排行榜
  • 聊城手机网站建设谷歌seo 外贸建站
  • 电影网站嵌入广告怎么做Java营销软件app
  • 界面设计心得百度seo排名原理
  • 测评网站怎么做宁波抖音seo搜索优化软件
  • 河南建设工程材料信息网官网西安网站关键词优化费用
  • 万网空间 wordpress林云seo博客
  • 网站原型设计规范广州权威发布
  • 苏州园区教育网网站建设优化公司
  • 网站策划与运营课程认知广告代运营公司
  • 网站开发strutsseow
  • 郑州网站设计收费低品牌推广策略分析
  • 搞笑网站建设目的和意义seo职位
  • 网站建设项目表广告推广平台网站有哪些
  • 网站如何做seo规划谷歌浏览器手机版免费官方下载
  • 网站镜像 动态开发网站需要多少钱
  • 网站开发技术可以做什么工作姓名查询
  • 网站开发步骤规划佛山旺道seo
  • 南京知名广告公司seo排名优化培训
  • 建站工具哪个好用广东佛山疫情最新情况
  • 扁平化网站首页网站怎么快速排名
  • wordpress放大指定图片seo整站优化报价
  • 惠州有没有做网站重庆seo职位
  • 网站建设 荆州重庆百度推广关键词优化
  • 天猫购物商城官网站长工具seo优化
  • 青岛网站建设咨询青岛seo服务公司
  • 上海做网站 公司台州关键词优化报价
  • 深圳网站建设有限公司 2019搜索引擎推广文案
  • 公司做网站买域名之后做什么网络推广优化方案