-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 2f1070c
Showing
26 changed files
with
1,805 additions
and
0 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,102 @@ | ||
# Recursion, Slow, but accepted :) | ||
class Solution(object): | ||
def isMatch(self, s, p, p_pre=None): | ||
""" | ||
:type s: str | ||
:type p: str | ||
:rtype: bool | ||
""" | ||
print ("s='%s',p='%s',p_pre='%s'" % (str(s),str(p),str(p_pre))) | ||
if len(s)==0: | ||
if len(p)==0: | ||
return True | ||
elif len(p)>=1 and p[0] == '*': | ||
return self.isMatch(s[:],p[1:],p_pre=p_pre) | ||
elif len(p)>=2 and p[1] == '*': | ||
return self.isMatch(s[:],p[2:],p_pre=p_pre) | ||
else: | ||
return False | ||
else: | ||
if len(p)==0: | ||
return False | ||
else: | ||
pass | ||
# | ||
result = False | ||
# | ||
if (s[0] == p[0] or p[0] =='.'): | ||
result = self.isMatch(s[1:],p[1:],p_pre=p[0]) | ||
if result == True: | ||
return True | ||
# print '1' | ||
# | ||
if (s[0] == p_pre or p_pre =='.') and p[0] == '*': | ||
result = self.isMatch(s[1:],p[:],p_pre=p_pre) | ||
if result == True: | ||
return True | ||
# print '2' | ||
# | ||
if len(p)>=2 and p[1] == '*': | ||
result = self.isMatch(s[:],p[2:],p_pre=p_pre) | ||
if result == True: | ||
return True | ||
# print '3' | ||
# | ||
if p[0] == '*': | ||
result = self.isMatch(s[:],p[1:],p_pre=p_pre) | ||
if result == True: | ||
return True | ||
return result | ||
|
||
|
||
# Recursion standard anwser, also quite slow, but faster and cleaner than above solution | ||
class Solution(object): | ||
def isMatch(self, text, pattern): | ||
if not pattern: | ||
return not text | ||
|
||
first_match = bool(text) and pattern[0] in {text[0], '.'} | ||
|
||
if len(pattern) >= 2 and pattern[1] == '*': | ||
return (self.isMatch(text, pattern[2:]) or | ||
first_match and self.isMatch(text[1:], pattern)) | ||
else: | ||
return first_match and self.isMatch(text[1:], pattern[1:]) | ||
|
||
|
||
# dynamic programming solution | ||
class Solution(object): | ||
def isMatch(self, text, pattern): | ||
memo = {} | ||
def dp(i, j): | ||
if (i, j) not in memo: | ||
if j == len(pattern): | ||
ans = i == len(text) | ||
else: | ||
first_match = i < len(text) and pattern[j] in {text[i], '.'} | ||
if j+1 < len(pattern) and pattern[j+1] == '*': | ||
ans = dp(i, j+2) or first_match and dp(i+1, j) | ||
else: | ||
ans = first_match and dp(i+1, j+1) | ||
|
||
memo[i, j] = ans | ||
return memo[i, j] | ||
|
||
return dp(0, 0) | ||
|
||
|
||
|
||
a = Solution() | ||
|
||
|
||
|
||
a.isMatch('asd','asd') | ||
a.isMatch('aasd','a*sd') | ||
a.isMatch('aasd','.*sd') | ||
a.isMatch('aasd','.*') | ||
a.isMatch('sd','*sd') | ||
a.isMatch('','*') | ||
a.isMatch('','c*c*') | ||
a.isMatch('abbabaaaaaaacaa',"a*.*b.a.*c*b*a*c*") | ||
a.isMatch('','*c*b*a*c*') | ||
s='',p='*c*b*a*c*',p_pre='.' |
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,107 @@ | ||
# O(n), accepted, (Two Pointer Approach, code simplified) | ||
class Solution(object): | ||
def maxArea(self, height): | ||
""" | ||
:type height: List[int] | ||
:rtype: int | ||
""" | ||
max_vol = 0 | ||
length = len(height) | ||
left_index = 0 | ||
right_index = length - 1 | ||
while left_index <= right_index: | ||
max_vol = max(max_vol,(min(height[left_index],height[right_index])*(right_index-left_index))) | ||
if height[left_index] >= height[right_index]: | ||
right_index = right_index - 1 | ||
else: | ||
left_index = left_index + 1 | ||
return max_vol | ||
|
||
# O(n), accepted, (Two Pointer Approach) | ||
class Solution(object): | ||
def maxArea(self, height): | ||
""" | ||
:type height: List[int] | ||
:rtype: int | ||
""" | ||
max_vol = 0 | ||
length = len(height) | ||
left_index = 0 | ||
right_index = length - 1 | ||
left = height[left_index] | ||
right = height[right_index] | ||
while left_index <= right_index: | ||
max_vol = max(max_vol,(min(left,right)*(right_index-left_index))) | ||
if left >= right: | ||
right_index = right_index - 1 | ||
right = height[right_index] | ||
else: | ||
left_index = left_index + 1 | ||
left = height[left_index] | ||
return max_vol | ||
|
||
|
||
# O(n^2) with max_left & max_right optimisation, still too slow | ||
class Solution(object): | ||
def maxArea(self, height): | ||
""" | ||
:type height: List[int] | ||
:rtype: int | ||
""" | ||
max_vol = 0 | ||
max_left = 0 | ||
length = len(height) | ||
for i in range(0,length-1): | ||
if height[i] > max_left: | ||
max_left = height[i] | ||
max_right = 0 | ||
for j in reversed(range(i,length)): | ||
if height[j] > max_right: | ||
max_right = height[j] | ||
temp_vol = min(height[i],height[j])*(j-i) | ||
max_vol = max(max_vol,temp_vol) | ||
return max_vol | ||
|
||
|
||
# O(n^2) with max_left optimisation, still too slow | ||
class Solution(object): | ||
def maxArea(self, height): | ||
""" | ||
:type height: List[int] | ||
:rtype: int | ||
""" | ||
max_vol = 0 | ||
max_left = 0 | ||
length = len(height) | ||
for i in range(0,length-1): | ||
if height[i] > max_left: | ||
max_left = height[i] | ||
for j in range(i,length): | ||
temp_vol = min(height[i],height[j])*(j-i) | ||
max_vol = max(max_vol,temp_vol) | ||
return max_vol | ||
|
||
|
||
# O(n^2), too slow | ||
class Solution(object): | ||
def maxArea(self, height): | ||
""" | ||
:type height: List[int] | ||
:rtype: int | ||
""" | ||
max_vol = 0 | ||
length = len(height) | ||
for i in range(0,length-1): | ||
for j in range(i,length): | ||
temp_vol = min(height[i],height[j])*(j-i) | ||
max_vol = max(max_vol,temp_vol) | ||
return max_vol | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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 @@ | ||
class Solution(object): | ||
def defangIPaddr(self, address): | ||
""" | ||
:type address: str | ||
:rtype: str | ||
""" | ||
address = address.replace('.','[.]') | ||
return address |
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,97 @@ | ||
## O(n) | ||
class Solution: | ||
def corpFlightBookings(self, bookings, n): | ||
res = [0] * (n + 1) | ||
for start, end, seats in bookings: | ||
res[start-1] += seats | ||
res[end] -= seats | ||
s = 0 | ||
for index, i in enumerate(res): | ||
res[index] += s | ||
s += i | ||
return res[:-1] | ||
|
||
## Too Slow (Brute Force) | ||
class Solution(object): | ||
def corpFlightBookings(self, bookings, n): | ||
""" | ||
:type bookings: List[List[int]] | ||
:type n: int | ||
:rtype: List[int] | ||
""" | ||
# booking_final = [[0]*n for i in range(len(bookings))] | ||
booking_final2 = [0]*n | ||
for start,end,seat in bookings: | ||
for flight in range(start-1,end): | ||
booking_final2[flight]=booking_final2[flight]+seat | ||
return booking_final2 | ||
|
||
## Too Slow | ||
from operator import add | ||
class Solution(object): | ||
def corpFlightBookings(self, bookings, n): | ||
""" | ||
:type bookings: List[List[int]] | ||
:type n: int | ||
:rtype: List[int] | ||
""" | ||
# booking_final = [[0]*n for i in range(len(bookings))] | ||
booking_final2 = [0]*n | ||
for i in range(len(bookings)): | ||
booking = bookings[i] | ||
temp=[0]*(booking[0]-1)+[booking[2]]*(booking[1]-booking[0]+1)+[0]*(n-booking[1]) | ||
booking_final2 = list( map(add, booking_final2, temp )) | ||
return booking_final2 | ||
|
||
|
||
|
||
## Too Slow | ||
from operator import add | ||
class Solution(object): | ||
def corpFlightBookings(self, bookings, n): | ||
""" | ||
:type bookings: List[List[int]] | ||
:type n: int | ||
:rtype: List[int] | ||
""" | ||
# booking_final = [[0]*n for i in range(len(bookings))] | ||
booking_final2 = [0]*n | ||
for i in range(len(bookings)): | ||
booking = bookings[i] | ||
temp=[0]*(booking[0]-1)+[booking[2]]*(booking[1]-booking[0]+1)+[0]*(n-booking[1]) | ||
booking_final2 = list( map(add, booking_final2, temp )) | ||
return booking_final2 | ||
|
||
|
||
|
||
## Memory Exceeded | ||
from operator import add | ||
class Solution(object): | ||
def corpFlightBookings(self, bookings, n): | ||
""" | ||
:type bookings: List[List[int]] | ||
:type n: int | ||
:rtype: List[int] | ||
""" | ||
booking_final = [[0]*n for i in range(len(bookings))] | ||
booking_final2 = [0]*n | ||
for i in range(len(bookings)): | ||
booking = bookings[i] | ||
booking_final[i]=[0]*(booking[0]-1)+[booking[2]]*(booking[1]-booking[0]+1)+[0]*(n-booking[1]) | ||
for i in range(len(bookings)): | ||
booking_final2 = list( map(add, booking_final2, booking_final[i] )) | ||
return booking_final2 | ||
|
||
## Too Slow | ||
class Solution(object): | ||
def corpFlightBookings(self, bookings, n): | ||
""" | ||
:type bookings: List[List[int]] | ||
:type n: int | ||
:rtype: List[int] | ||
""" | ||
booking_final = [0]*n | ||
for booking in bookings: | ||
for flight in range(booking[0],booking[1]+1): | ||
booking_final[flight-1]=booking_final[flight-1]+booking[2] | ||
return booking_final |
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,39 @@ | ||
Symbol Value | ||
I 1 | ||
V 5 | ||
X 10 | ||
L 50 | ||
C 100 | ||
D 500 | ||
M 1000 | ||
|
||
I can be placed before V (5) and X (10) to make 4 and 9. | ||
X can be placed before L (50) and C (100) to make 40 and 90. | ||
C can be placed before D (500) and M (1000) to make 400 and 900. | ||
|
||
class Solution(object): | ||
def intToRoman(self, num): | ||
""" | ||
:type num: int | ||
:rtype: str | ||
""" | ||
translate_dict = { 'M' : 1000, | ||
'CM': 900, | ||
'D' : 500, | ||
'CD': 400, | ||
'C' : 100, | ||
'XC': 90, | ||
'L' : 50, | ||
'XL': 40, | ||
'X' : 10, | ||
'IX': 9, | ||
'V' : 5, | ||
'IV': 4, | ||
'I' : 1 | ||
} | ||
result='' | ||
for l in ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']: | ||
while num >= translate_dict[l]: | ||
result = result + l | ||
num = num - translate_dict[l] | ||
return result |
Oops, something went wrong.