Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8003da
commit 4811c9f
Showing
8 changed files
with
193 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This is a little non-obvious, but the problem can be solved in O(n) time. Just move forward the head cursor, and "shrink the tail" when duplicate chars appear, while maintaining the max substring length. | ||
|
||
Need to take care of some special cases, like the start and end of string. | ||
|
41 changes: 41 additions & 0 deletions
41
003_longest_substring_without_repeating_chars/longest_substr_without_repeat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Solution(object): | ||
def lengthOfLongestSubstring(self, s): | ||
""" | ||
:type s: str | ||
:rtype: int | ||
""" | ||
# mark the positions of each individual char | ||
pos = {} | ||
duplicates = set() | ||
for i, c in enumerate(s): | ||
pos.setdefault(c, []).append(i) | ||
if len(pos[c])>=2: | ||
duplicates.add(c) | ||
|
||
pairs = [] | ||
for i, c in enumerate(s): | ||
if c in duplicates: | ||
pairs.append((i, c)) | ||
if not pairs: | ||
return len(s) | ||
d = {} | ||
start = 0 | ||
lmax = 0 | ||
cnt = 0 | ||
for pos, c in pairs: | ||
if c not in d: | ||
d[c]=pos | ||
else: | ||
if pos-start>lmax: | ||
lmax = pos-start | ||
start = max(start, d[c]+1) | ||
d[c] = pos | ||
#print pos, start, lmax | ||
cnt += 1 | ||
if cnt == len(pairs): | ||
lmax = max(lmax, len(s)-start) | ||
|
||
return lmax | ||
|
||
|
||
print Solution().lengthOfLongestSubstring("c") |
74 changes: 74 additions & 0 deletions
74
003_longest_substring_without_repeating_chars/longest_substr_without_repeat_TLE.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
class Solution(object): | ||
def lengthOfLongestSubstring(self, s): | ||
""" | ||
:type s: str | ||
:rtype: int | ||
""" | ||
# mark the positions of each individual char | ||
pos = {} | ||
duplicates = set() | ||
for i, c in enumerate(s): | ||
pos.setdefault(c, []).append(i) | ||
if len(pos[c])>=2: | ||
duplicates.add(c) | ||
|
||
pairs = [] | ||
for i, c in enumerate(s): | ||
if c in duplicates: | ||
pairs.append((i, c)) | ||
if not pairs: | ||
return len(s) | ||
_start, _end = object(), object() | ||
if pairs[0][0]>0: | ||
pairs.insert(0, (0, _start)) | ||
if pairs[1][0]<len(s)-1: | ||
pairs.append((len(s)-1, _end)) | ||
|
||
lmax = 0 | ||
for i, pair in enumerate(pairs): | ||
mm, cc = pair | ||
seen = set([cc]) | ||
idx = i+1 | ||
while idx<len(pairs): | ||
k, c = pairs[idx] | ||
if c in seen: | ||
if k-mm>lmax: | ||
lmax = k-mm | ||
break | ||
seen.add(c) | ||
idx += 1 | ||
if idx == len(pairs): | ||
lmax = max(lmax, len(s)-mm) | ||
|
||
return lmax | ||
|
||
|
||
|
||
|
||
|
||
# cadidate intervals that can be longest nonrepeating substr | ||
# intervals = {} | ||
# for c in pos: | ||
# vec = pos[c] | ||
# vec.insert(0, -1) | ||
# vec.append(len(s)) | ||
# ints = [] | ||
# i = 0 | ||
# while i+2<len(vec): | ||
# ints.append((vec[i]+1, vec[i+2]-1)) | ||
# i+=1 | ||
# intervals[c]=ints | ||
# print intervals | ||
|
||
# maxlen = 0 | ||
# for c in intervals: | ||
# for interval in intervals[c]: | ||
# substr = s[interval[0]:interval[1]+1] | ||
# setlen = len(set(substr)) | ||
# strlen = len(substr) | ||
# if setlen == strlen and strlen > maxlen: | ||
# maxlen = strlen | ||
# return maxlen | ||
|
||
|
||
print Solution().lengthOfLongestSubstring("c") |
23 changes: 23 additions & 0 deletions
23
318_maximum_product_of_word_lengths/maximum_product_of_word_lengths.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import operator | ||
class Solution(object): | ||
def maxProduct(self, words): | ||
""" | ||
:type words: List[str] | ||
:rtype: int | ||
""" | ||
maskLen = {self.getMask(word):len(word) for word in sorted(words, key=lambda w:len(w))} | ||
maxP = 0 | ||
masks = sorted(maskLen.items(), key=operator.itemgetter(1)) | ||
for i in range(len(masks)): | ||
for j in range(i+1, len(masks)): | ||
if masks[i][0]&masks[j][0]==0: | ||
p = masks[i][1]*masks[j][1] | ||
if p>maxP: | ||
maxP = p | ||
return maxP | ||
|
||
def getMask(self, word): | ||
mask = 0 | ||
for c in word: | ||
mask = mask|(1<<(ord(c)-97)) | ||
return mask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
This is a brain teaser kind of problem. | ||
|
||
Observation #1, whether #N light stays on depends on how many unique factors N have. Prime numbers must be off, because it is switched on Round 1 and off in Round N. | ||
|
||
Observation #2, for a light #N to stays on, N needs to have odd number of factors. Fact: only square numbers can have odd number of unique factors. | ||
|
||
Proof by contradiction: If a number has odd number of unique factors, and is not a square number, write down all its unique factors, and start crossing off the factors: If I*J=N, cross off J as well as I. In the end, there is only one factor M left, M*M has to be N. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import math | ||
class Solution(object): | ||
def bulbSwitch(self, n): | ||
""" | ||
:type n: int | ||
:rtype: int | ||
""" | ||
return int(math.sqrt(n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import sys | ||
class Solution(object): | ||
def coinChange(self, coins, amount): | ||
""" | ||
:type coins: List[int] | ||
:type amount: int | ||
:rtype: int | ||
""" | ||
self.d = {} | ||
self.coins = sorted(coins) | ||
if amount==0: return 0 | ||
return self.recurse(amount) | ||
|
||
def recurse(self, n): | ||
if n in self.d: | ||
return self.d[n] | ||
elif n<self.coins[0]: | ||
result = -1 | ||
elif n in self.coins: | ||
result = 1 | ||
else: | ||
min = sys.maxint | ||
found = False | ||
for coin in self.coins: | ||
t = self.recurse(n-coin) | ||
if t > 0 and t+1<min: | ||
min = t+1 | ||
found = True | ||
if not found: | ||
result = -1 | ||
else: | ||
result = min | ||
self.d[n]=result | ||
|
||
return result |
This file was deleted.
Oops, something went wrong.