-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
17 changed files
with
485 additions
and
68 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 |
---|---|---|
@@ -1,55 +1,46 @@ | ||
# 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). | ||
|
||
# For example, | ||
# S = "ADOBECODEBANC" | ||
# T = "ABC" | ||
# Minimum window is "BANC". | ||
|
||
# Note: | ||
# If there is no such window in S that covers all characters in T, return the empty string "". | ||
|
||
# If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S. | ||
from collections import defaultdict | ||
class Solution(object): | ||
def minWindow(self, s, t): | ||
""" | ||
O(N) | ||
O(n) | ||
O(1) | ||
:type s: str | ||
:type t: str | ||
:rtype: str | ||
""" | ||
if not s: | ||
return "" | ||
if not t: | ||
return "" | ||
useddict = defaultdict(int) | ||
res = "" | ||
if not s or not t: | ||
return res | ||
# init | ||
dic = defaultdict(int) | ||
# sliding windows | ||
l,r = 0,0 | ||
min_length = len(s) | ||
size = len(t) | ||
for char in t: | ||
useddict[char] += 1 | ||
dic[char] += 1 | ||
|
||
minlength = len(s) + 1 | ||
length = len(t) | ||
l, r = 0, 0 | ||
res = "" | ||
|
||
while r < len(s): | ||
if s[r] in useddict: | ||
useddict[s[r]] -= 1 | ||
# only valid char in t will minus length | ||
if useddict[s[r]] >= 0: | ||
length -= 1 | ||
if s[r] in dic: | ||
if dic[s[r]] > 0: | ||
size -= 1 | ||
# value < 0 means duplicate char in s | ||
dic[s[r]] -= 1 | ||
|
||
r += 1 | ||
while length == 0: | ||
if minlength >= r-l: | ||
minlength = r-l | ||
|
||
while size == 0: | ||
if min_length >= r-l: | ||
min_length = r-l | ||
res = s[l:r] | ||
|
||
if s[l] in useddict: | ||
useddict[s[l]] += 1 | ||
# avoid duplicate | ||
if useddict[s[l]] > 0: | ||
length += 1 | ||
# left bound | ||
if s[l] in dic: | ||
dic[s[l]] += 1 | ||
if dic[s[l]] > 0: | ||
size += 1 | ||
|
||
l += 1 | ||
return res | ||
|
||
|
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
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
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
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 @@ | ||
# Find the contiguous subarray within an array (containing at least one number) which has the largest sum. | ||
|
||
# For example, given the array [-2,1,-3,4,-1,2,1,-5,4], | ||
# the contiguous subarray [4,-1,2,1] has the largest sum = 6. | ||
class Solution(object): | ||
def maxSubArray(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
if not nums: | ||
return 0 | ||
if len(nums) == 1: | ||
return nums[0] | ||
index=0 | ||
prevMax = nums[0] | ||
res = nums[0] | ||
for each in range(1,len(nums)): | ||
prevMax = max(prevMax+nums[each], nums[each]) | ||
if res < prevMax: | ||
index = each | ||
res = prevMax | ||
#res = max(res, prevMax) | ||
|
||
print res | ||
t = [] | ||
while res!=0: | ||
t.append(nums[index]) | ||
|
||
res -= nums[index] | ||
index -= 1 | ||
|
||
return t[::-1] | ||
m = Solution() | ||
print m.maxSubArray([-2,1,-3,4,-1,2,1,-5,4]) |
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,40 @@ | ||
class Solution(object): | ||
def isNumber(self, s): | ||
""" | ||
:type s: str | ||
:rtype: bool | ||
""" | ||
begin, last = 0, len(s) - 1 | ||
# | ||
while begin <= last and s[begin] == ' ': | ||
begin += 1 | ||
while begin <= last and s[last] == ' ': | ||
last -= 1 | ||
|
||
# | ||
if begin < last and (s[begin] == '+' or s[begin] == '-'): | ||
begin += 1 | ||
num, dot, exp = False, False, False | ||
|
||
while begin <= last: | ||
# | ||
if s[begin] >= '0' and s[begin] <= '9': | ||
num = True | ||
# | ||
elif s[begin] == '.': | ||
if dot or exp: | ||
return False | ||
dot = True | ||
# | ||
elif s[begin] == 'e' or s[begin] == 'E': | ||
if exp or not num: | ||
return False | ||
exp, num = True, False | ||
# | ||
elif s[begin] == '+' or s[begin] == '-': | ||
if s[begin - 1] != 'e': | ||
return False | ||
else: | ||
return False | ||
begin += 1 | ||
return num |
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,28 @@ | ||
# Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words. | ||
|
||
# For example, given | ||
# s = "leetcode", | ||
# dict = ["leet", "code"]. | ||
|
||
# Return true because "leetcode" can be segmented as "leet code". | ||
class Solution(object): | ||
def wordBreak(self, s, wordDict): | ||
""" | ||
:type s: str | ||
:type wordDict: List[str] | ||
:rtype: bool | ||
""" | ||
if not s: | ||
return True | ||
|
||
# init | ||
dp = [False for _ in range(len(s)+1)] | ||
dp[0] = True | ||
|
||
for i in range(1,len(s)+1): | ||
for j in range(i): | ||
if dp[j] and s[j:i] in wordDict: | ||
dp[i] = True | ||
print dp | ||
return dp[-1] | ||
|
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,43 @@ | ||
# Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words. | ||
|
||
# Return all such possible sentences. | ||
|
||
# For example, given | ||
# s = "catsanddog", | ||
# dict = ["cat", "cats", "and", "sand", "dog"]. | ||
|
||
# A solution is ["cats and dog", "cat sand dog"]. | ||
class Solution(object): | ||
def wordBreak(self, s, wordDict): | ||
""" | ||
:type s: str | ||
:type wordDict: Set[str] | ||
:rtype: List[str] | ||
""" | ||
ans = [] | ||
if self.check(s, wordDict): | ||
self.dfs(0, len(s), '', s, ans, wordDict) | ||
return ans | ||
|
||
def check(self, s, wordDict): | ||
dp = [True] + [False] * len(s) | ||
n = len(s) | ||
for i in xrange(n): | ||
for j in xrange(i + 1): | ||
if dp[j] and s[j:i + 1] in wordDict: | ||
dp[i + 1] = True | ||
break | ||
return dp[n] | ||
|
||
def dfs(self, cur, n, path, s, ans, wordDict): | ||
if cur == n: | ||
#print path | ||
ans.append(path) | ||
return | ||
|
||
for i in xrange(cur, n): | ||
if s[cur:i + 1] in wordDict and self.check(s[i + 1:n], wordDict): | ||
if path: | ||
self.dfs(i + 1, n, path + ' ' + s[cur:i + 1], s, ans, wordDict) | ||
else: | ||
self.dfs(i + 1, n, s[cur:i + 1], s, ans, wordDict) |
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,21 @@ | ||
# Given an input string, reverse the string word by word. | ||
|
||
# For example, | ||
# Given s = "the sky is blue", | ||
# return "blue is sky the". | ||
|
||
class Solution(object): | ||
def reverseWords(self, s): | ||
""" | ||
O(n) | ||
O(n) | ||
:type s: str | ||
:rtype: str | ||
""" | ||
if not s: | ||
return '' | ||
li = s.strip().split() | ||
res = [] | ||
for each in range(len(li)-1,-1,-1): | ||
res.append(li[each]) | ||
return ' '.join(res) |
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,24 @@ | ||
class Solution(object): | ||
def reverseWords(self, s): | ||
""" | ||
:type s: a list of 1 length strings (List[str]) | ||
:rtype: nothing | ||
""" | ||
def reverse(s, begin, end): | ||
for i in xrange((end - begin) / 2): | ||
s[begin + i], s[end - 1 - i] = s[end - 1 - i], s[begin + i] | ||
|
||
reverse(s, 0, len(s)) | ||
print s | ||
i = 0 | ||
for j in xrange(len(s) + 1): | ||
if j == len(s) or s[j] == ' ': | ||
reverse(s, i, j) | ||
i = j + 1 | ||
|
||
|
||
if __name__ == '__main__': | ||
s = "the sky is blue" | ||
b= list(s) | ||
Solution().reverseWords(b) | ||
print "".join(b) |
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,19 @@ | ||
# Find the contiguous subarray within an array (containing at least one number) which has the largest product. | ||
|
||
# For example, given the array [2,3,-2,4], | ||
# the contiguous subarray [2,3] has the largest product = 6. | ||
class Solution(object): | ||
def maxProduct(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
maxSoFar = nums[0] | ||
maxEndingHere = nums[0] | ||
minEndingHere = nums[0] | ||
for i in range(1, len(nums)): | ||
maxTemp = maxEndingHere | ||
maxEndingHere = max(maxEndingHere * nums[i], nums[i], minEndingHere * nums[i]) | ||
minEndingHere = min(minEndingHere * nums[i], nums[i], maxTemp * nums[i]) | ||
maxSoFar = max(maxSoFar, maxEndingHere) | ||
return maxSoFar |
26 changes: 26 additions & 0 deletions
26
Wepay and OfferUp/153. Find Minimum in Rotated Sorted Array.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,26 @@ | ||
# Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. | ||
|
||
# (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). | ||
|
||
# Find the minimum element. | ||
|
||
# You may assume no duplicate exists in the array. | ||
class Solution(object): | ||
def findMin(self, nums): | ||
""" | ||
O(logn) | ||
O(1) | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
left = 0 | ||
right = len(nums)-1 | ||
while left < right and nums[left]> nums[right]: | ||
middle = left + (right-left)/2 | ||
if nums[middle] > nums[right]: | ||
# left-middle is increasing | ||
left = middle + 1 | ||
else: | ||
right = middle | ||
return nums[left] | ||
|
Oops, something went wrong.