边工作边刷题:70天一遍leetcode: day 71-1
Longest Substring with At Most K Distinct Characters
要点:要搞清楚At Most Two Distinct和Longest Substring Without Repeating Characters (No Repeating)的区别:前者的sliding window里只有2个char,但是可以任意重复。而后者可以有任意多个char但是任何char都不能有重复。
- 所以解法上前者的hashset只有2个元素,而后者需要把所有distinct的元素放到hashset里。而前者因为只有当不在hashset中的元素才可能考虑更新hashset,而后者是有当前元素在hashset里更新。
- 推广到k,
- At Most K Distinct要把k个元素中最后一次出现(最右)最靠左的那个去掉,所以要不断更新最右边界,At Most K Repeating因为新元素在hashset中,所以去掉的只能是该元素,只是k扩展到要记录重复的元素个数是否到k才启动。
- At Most K Distinct还有用count的方法:把集中检查边界分布到从左向右每个元素减少count,最先count减少到0的就是最左。而At Most K Repeating因为只去掉一个元素,没有这个方法。
要点:
- 是对longest substring without repeating characters这题另一种思路的扩展。no repeating这题是用map存当前sliding window的字符,下一个char不能出现在map中。而distinct这题是下一个不能是没有在map中的(除非map中只有一个or <k个)。
- k个中选哪个取代?显然k个字符中最后出现中最靠左的字符是所选。这样能保证当前sliding window中的local maxLen
- 上面的方法每次新字符都要遍历k个在map中的字符,整体时间是O(n)*O(k)。另一种方法更类似no repeating的方法。直接从sliding window左边开始pop直到某个元素count==0(所以map中记录count)。其实也是找到最后出现的最靠左字符。
错误点:
- start的更新在清楚左边界的loop内,如果没有进入这个清除环节,不需要更新start
- start的更新在leftMost的+1位置
- count version不用检查>0,因为在map中的都是如此
- count version:注意start是index,umap[s[start]]
- count version: 初始map中的count value是1而不是0
- count version: 别忘了新元素进map(主要是光想着del key了)
https://repl.it/CaiH (map loop)
https://repl.it/CaiT/1 (k, count)
https://repl.it/CqmA (Two, use char1, char2 variables)
class Solution(object):
def lengthOfLongestSubstringKDistinct(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
n = len(s)
maxLen = 0
umap = {}
start = 0
for i in xrange(n):
if s[i] not in umap and len(umap)>=k:
leftMost = n
rmChar = None
for c in umap:
if umap[c]<leftMost:
leftMost = umap[c]
rmChar = c
del umap[rmChar]
start = leftMost+1 # error 1: start should be set inside update condition
# error 2: start index: next of leftMost
umap[s[i]]=i
if i-start+1>maxLen:
maxLen = i-start+1
#print i,start,maxLen,umap
return maxLen
sol = Solution()
print sol.lengthOfLongestSubstringKDistinct("aabbcc", 1)
print sol.lengthOfLongestSubstringKDistinct("aabbcc", 2)
print sol.lengthOfLongestSubstringKDistinct("aabbcc", 3)
print sol.lengthOfLongestSubstringKDistinct("eceba", 2)
# Given a string, find the length of the longest substring T that contains at most k distinct characters.
# For example, Given s = “eceba” and k = 2,
# T is "ece" which its length is 3.
# Hide Company Tags Google
# Hide Tags Hash Table String
# Hide Similar Problems (H) Longest Substring with At Most Two Distinct Characters
class Solution(object):
def lengthOfLongestSubstringKDistinct(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
umap = {}
if k==0: return 0
longest, start = 0,0
for i in xrange(len(s)):
# print "it=", i,umap,len(umap)
if s[i] in umap:
umap[s[i]]+=1
elif len(umap)<k:
umap[s[i]]=1 # error 1: should be 1, not 0
else:
umap[s[i]]=1 # error 2: don't forget to put myself into
while start<i:
c = s[start]
start+=1
umap[c]-=1
if not umap[c]:
del umap[c]
break
longest = max(longest, i-start+1)
return longest
sol = Solution()
assert sol.lengthOfLongestSubstringKDistinct("eceba", 2)==3
边工作边刷题:70天一遍leetcode: day 71-1的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 71
Longest Substring with At Most Two Distinct Characters # Given a string, find the length of the long ...
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
随机推荐
- 泛函编程(6)-数据结构-List基础
List是一种最普通的泛函数据结构,比较直观,有良好的示范基础.List就像一个管子,里面可以装载一长条任何类型的东西.如需要对管子里的东西进行处理,则必须在管子内按直线顺序一个一个的来,这符合泛函编 ...
- JavaScript基础15——js的DOM对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- javascript --- 词法分析
JavaScript代码自上而下执行,但是在js代码执行前,会首先进行词法分析,所以事实上,js运行要分为词法分析和执行两个阶段. 词法分析主要分为三步: 第一步: 分析形参: 第二步: 分析变量声明 ...
- NPOI Excel类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using NPOI.HSSF.Us ...
- andriod 带看括弧的计算器
界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=& ...
- Oracle之自动收集统计信息
一.Oracle 11g 在Oracle的11g版本中提供了统计数据自动收集的功能.在部署安装11g Oracle软件过程中,其中有一个步骤便是提示是否启动这个功能(默认是启用这个功能). 在这里介绍 ...
- SharePoint 2013 点击"关注" 报错
现象: 点击"关注" 报错. 解决办法: 1.确保bin文件夹下的.dll版本与web.config一致. 2.设置user porfile权限. 2.重启iis 结果如下:
- NDK、SDK以及JNI的关系
最近由于项目的需要,使用到了Android的NDK技术,对项目核心算法跨平台的移植.简答而言,就是使用C对原来的算法进行了改进,并集成到原 来的app项目里. 从前的项目一直没有使用NDK进行开发的机 ...
- Linux0.11内核剖析--初始化程序(init)
1.概述 在内核源代码的 init/目录中只有一个 main.c 文件. 系统在执行完 boot/目录中的 head.s 程序后就会将执行权交给 main.c.该程序虽然不长,但却包括了内核初始化的所 ...
- Android ListView用EditText实现搜索功能
前言 最近在开发一个IM项目的时候有一个需求就是,好友搜索功能.即在EditText中输入好友名字,ListView列表中动态展示刷选的好友列表.我把这个功能抽取出来了,先贴一下效果图: 分析 在查阅 ...