1. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

Example:

Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"

Note:

  • If there is no such window in S that covers all characters in T, return the empty string "".
  • If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

思路

利用HashMap存储T中的每个字符,及其出现频率,先在S中找包含T的的第一个Window Substring,找到了后因为可能不是最小的所以还是要从左往右遍历找,只不过这是有点技巧的找。判断这个window substring的最左字符,如果不是在T中的直接删除,继续判断它的下一个位置,如果是在T中的话也删除它,不过这删除后下一步我们要转到window substring的最右边去寻找我们删除掉的字符,如果找到则构成新的window,再重复相同步骤,过程中记录windows的最小长度,如果找不到则表明后面没有能包含T的window substring了,也就是找到了所有包含T的Window Substring的情形。

    1. create a hashmap for each character in t and count their frequency in t as the value of hashmap.
    2. Find the first window in S that contains T.
    3. Checking from the leftmost index of the window and to see if it belongs to t. The reason we do so is that we want to shrink the size of the window.
      3-1) If the character at leftmost index does not belong to t, we can directly remove this leftmost value and update our window(its minLeft and minLen value)
      3-2) If the character indeed exists in t, we still remove it, but in the next step, we will increase the right pointer and expect the removed character. If find so, repeat step 3.

代码

class Solution {
public String minWindow(String s, String t) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();;
for(char c : t.toCharArray()){
if(map.containsKey(c)){
map.put(c, map.get(c)+1);
}else{
map.put(c, 1);
}
}
int counter=t.length(), begin=0, end=0, head=0, d=Integer.MAX_VALUE;
while(end<s.length()){ // 先找左边第一个包含T的substring
char c_left=s.charAt(end);
if(map.containsKey(c_left)){
map.put(c_left, map.get(c_left)-1);
if(map.get(c_left)>=0) counter--;
}
end++;
while(counter==0){ // 如果找到,则从substring的左边考虑
if(end-begin<d){
head=begin;
d=end-begin;
}
char c_right=s.charAt(begin);
if(map.containsKey(c_right)){
map.put(c_right, map.get(c_right)+1);
if(map.get(c_right)>0) counter++;
}
begin++;
}
}
return d==Integer.MAX_VALUE? "":s.substring(head, head+d); }

2. Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.


Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].


The largest rectangle is shown in the shaded area, which has area = 10 unit.

Example:

Input: [2,1,5,6,2,3]
Output: 10

思路

解法和trap water那题有点类似,不过得转换下思维。还是利用栈,想到这点了,但是我一直在纠结后输入的bar后前面bar的组成的rectangle有什么关系或规律,当然这也是看了这题后最直观的想法了。这个切入点不是很好解题,这个时候应该换下思路才行,感觉犯了和trap water那题一样的思维错误,最直观的想法往往只适合循环遍历,即brute force。利用栈来解这题的话还是得换个切入点的。

和trap water那题一样,不去考虑不同bar之间的组合情形,而是从单个bar的入手,求包含每个bar为最低高度的 rectangle 来考虑。

For every bar ‘x’, we calculate the area with ‘x’ as the smallest bar in the rectangle. If we calculate such area for every bar ‘x’ and find the maximum of all areas, our task is done

至于怎么计算这个rectangle,其实就和trap water那题计算每个bar所能trap的water 是差不多的。

代码

class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> st=new Stack();
int left=0;
int maxArea=0;
for(int i=0;i<=heights.length;i++){
int h= (i==heights.length? 0:heights[i]);
if(st.isEmpty() || h>=heights[st.peek()]){
st.push(i);
}else{
int hi=heights[st.pop()];
left=(st.isEmpty()? -1:st.peek());
maxArea=Math.max(maxArea, (i-left-1)*hi);
i--;
}
}
return maxArea;
}
}

LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram的更多相关文章

  1. 【LeetCode】76. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  2. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  3. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram

  4. 【一天一道LeetCode】#76. Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...

  6. 【LeetCode练习题】Minimum Window Substring

    找出包含子串的最小窗口 Given a string S and a string T, find the minimum window in S which will contain all the ...

  7. LeetCode第[84]题(Java):Largest Rectangle in Histogram(最大的矩形柱状图)

    题目:最大的矩形柱状图 难度:hard 题目内容: Given n non-negative integers representing the histogram's bar height wher ...

  8. LeetCode: Largest Rectangle in Histogram 解题报告

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  9. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

随机推荐

  1. 测试开发面试的Linux面试题总结之一:vim使用方法

    现在做测试没有说不用到linux,大部分公司都会涉及到,作为测试经常使用linux最常见手段就是查看日志,帮助开发定位问题,这是目前最常见的测试当中使用linux方法,今天就讲一讲vim文本编辑器的使 ...

  2. MapReduce(五) mapreduce的shuffle机制 与 Yarn

    一.shuffle机制 1.概述 (1)MapReduce 中, map 阶段处理的数据如何传递给 reduce 阶段,是 MapReduce 框架中最关键的一个流程,这个流程就叫 Shuffle:( ...

  3. 【bzoj3796】Mushroom追妹纸

    Portal -->bzoj3796 Description 给出字符串s1.s2.s3,找出一个字符串w,满足: 1.w是s1的子串: 2.w是s2的子串: 3.s3不是w的子串. ​ 求w的 ...

  4. 【bzoj3105】新Nim游戏

    Portal--> bzoj3105 新Nim游戏 Solution 转化一下问题 首先看一下原来的Nim游戏,先手必胜的条件是:每堆数量的异或和不为\(0\) 所以在新的游戏中,如果要保证自己 ...

  5. 【loj6436】【pkusc2018】神仙的游戏

    Portal --> pkuscD2T2(loj6436) Solution 个人觉得是道很好的法法塔题qwq 一开始的时候想偏了想到了另一种法法塔处理字符串匹配之类的奇怪技巧(万径人踪灭qwq ...

  6. Hdu5226 Tom and matrix

    Tom and matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. 洛谷P1230 智力大冲浪

    题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者m元.先不要太高兴!因为这些钱还不一定都是你的?!接下来主持人宣布了比赛规则: ...

  8. Java的StringAPI的小练习

    //-------------String类-------------- //求两个字符串的最大相同子串 /* 思路: 1.找出较短的那个字符串 2.找出短串的所有子串,使用contains函数判断是 ...

  9. Lua只读表

    利用Lua的元表(metatable)和元函数(metafunction)可以很简单的实现此功能. 其实现大致分为三个部分 1.禁止在表中创建新值 2.禁止改变已有的值 3.将子表也变为只读 1.禁止 ...

  10. iOS网络基础---iOS-Apple苹果官方文档翻译

    CHENYILONG Blog iOS网络基础---iOS-Apple苹果官方文档翻译 iOS网络基础 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http: ...