-
Notifications
You must be signed in to change notification settings - Fork 90
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
13 changed files
with
699 additions
and
696 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,30 +1,30 @@ | ||
""" | ||
Given n items with size A[i] and value V[i], and a backpack with size m. What's the maximum value can you put into the | ||
backpack? | ||
""" | ||
__author__ = 'Danyang' | ||
class Solution: | ||
def backPackII(self, m, A, V): | ||
""" | ||
dp | ||
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]} | ||
to | ||
f[v]=max{f[v],f[v-c[i]+w[i]} | ||
NEED TO KEEP A COPY OF (i-1) STATE. | ||
:param m: An integer m denotes the size of a backpack | ||
:param A & V: Given n items with size A[i] and value V[i] | ||
:return: The maximum size | ||
""" | ||
n = len(A) | ||
f = [0 for _ in xrange(m+1)] # plus 1 for dummy | ||
for i in xrange(1, n+1): | ||
copy = list(f) | ||
for j in xrange(1, m+1): | ||
# decide whether to put A[i-1] | ||
if j-A[i-1]>=0: | ||
f[j] = max(copy[j], copy[j-A[i-1]]+V[i-1]) | ||
else: | ||
f[j] = copy[j] | ||
""" | ||
Given n items with size A[i] and value V[i], and a backpack with size m. What's the maximum value can you put into the | ||
backpack? | ||
""" | ||
__author__ = 'Danyang' | ||
class Solution: | ||
def backPackII(self, m, A, V): | ||
""" | ||
dp | ||
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]} | ||
to | ||
f[v]=max{f[v],f[v-c[i]+w[i]} | ||
NEED TO KEEP A COPY OF (i-1) STATE. | ||
:param m: An integer m denotes the size of a backpack | ||
:param A & V: Given n items with size A[i] and value V[i] | ||
:return: The maximum size | ||
""" | ||
n = len(A) | ||
f = [0 for _ in xrange(m+1)] # plus 1 for dummy | ||
for i in xrange(1, n+1): | ||
copy = list(f) | ||
for j in xrange(1, m+1): | ||
# decide whether to put A[i-1] | ||
if j-A[i-1]>=0: | ||
f[j] = max(copy[j], copy[j-A[i-1]]+V[i-1]) | ||
else: | ||
f[j] = copy[j] | ||
return f[m] |
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,75 +1,75 @@ | ||
""" | ||
Given n items with size A[i], an integer m denotes the size of a backpack. How full you can fill this backpack? | ||
""" | ||
__author__ = 'Danyang' | ||
class Solution_TLE: | ||
def backPack(self, m, A): | ||
""" | ||
search, brute force | ||
:param m: An integer m denotes the size of a backpack | ||
:param A: Given n items with size A[i] | ||
:return: The maximum size | ||
""" | ||
result = [0] | ||
self.dfs(A, 0, m, result) | ||
return result[0] | ||
|
||
def dfs(self, seq, cur, m, result): | ||
if cur>m: | ||
return | ||
|
||
result[0] = max(result[0], cur) | ||
if seq: | ||
self.dfs(seq[1:], cur+seq[0], m, result) | ||
self.dfs(seq[1:], cur, m, result) | ||
|
||
|
||
class Solution_MLE: | ||
def backPack(self, m, A): | ||
""" | ||
dp | ||
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]} | ||
:param m: An integer m denotes the size of a backpack | ||
:param A: Given n items with size A[i] | ||
:return: The maximum size | ||
""" | ||
n = len(A) | ||
f = [[0 for _ in xrange(m+1)] for _ in xrange(n+1)] # plus 1 for dummy | ||
for i in xrange(1, n+1): | ||
for j in xrange(1, m+1): | ||
# decide whether to put A[i-1] | ||
if j-A[i-1]>=0: | ||
f[i][j] = max(f[i-1][j], f[i-1][j-A[i-1]]+A[i-1]) | ||
else: | ||
f[i][j] = f[i-1][j] | ||
return f[n][m] | ||
|
||
class Solution: | ||
def backPack(self, m, A): | ||
""" | ||
dp | ||
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]} | ||
to | ||
f[v]=max{f[v],f[v-c[i]+w[i]} | ||
NEED TO KEEP A COPY OF (i-1) STATE. | ||
:param m: An integer m denotes the size of a backpack | ||
:param A: Given n items with size A[i] | ||
:return: The maximum size | ||
""" | ||
n = len(A) | ||
f = [0 for _ in xrange(m+1)] # plus 1 for dummy | ||
for i in xrange(1, n+1): | ||
copy = list(f) | ||
for j in xrange(1, m+1): | ||
# decide whether to put A[i-1] | ||
if j-A[i-1]>=0: | ||
f[j] = max(copy[j], copy[j-A[i-1]]+A[i-1]) | ||
else: | ||
f[j] = copy[j] | ||
return f[m] | ||
|
||
if __name__=="__main__": | ||
""" | ||
Given n items with size A[i], an integer m denotes the size of a backpack. How full you can fill this backpack? | ||
""" | ||
__author__ = 'Danyang' | ||
class Solution_TLE: | ||
def backPack(self, m, A): | ||
""" | ||
search, brute force | ||
:param m: An integer m denotes the size of a backpack | ||
:param A: Given n items with size A[i] | ||
:return: The maximum size | ||
""" | ||
result = [0] | ||
self.dfs(A, 0, m, result) | ||
return result[0] | ||
|
||
def dfs(self, seq, cur, m, result): | ||
if cur>m: | ||
return | ||
|
||
result[0] = max(result[0], cur) | ||
if seq: | ||
self.dfs(seq[1:], cur+seq[0], m, result) | ||
self.dfs(seq[1:], cur, m, result) | ||
|
||
|
||
class Solution_MLE: | ||
def backPack(self, m, A): | ||
""" | ||
dp | ||
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]} | ||
:param m: An integer m denotes the size of a backpack | ||
:param A: Given n items with size A[i] | ||
:return: The maximum size | ||
""" | ||
n = len(A) | ||
f = [[0 for _ in xrange(m+1)] for _ in xrange(n+1)] # plus 1 for dummy | ||
for i in xrange(1, n+1): | ||
for j in xrange(1, m+1): | ||
# decide whether to put A[i-1] | ||
if j-A[i-1]>=0: | ||
f[i][j] = max(f[i-1][j], f[i-1][j-A[i-1]]+A[i-1]) | ||
else: | ||
f[i][j] = f[i-1][j] | ||
return f[n][m] | ||
|
||
class Solution: | ||
def backPack(self, m, A): | ||
""" | ||
dp | ||
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]} | ||
to | ||
f[v]=max{f[v],f[v-c[i]+w[i]} | ||
NEED TO KEEP A COPY OF (i-1) STATE. | ||
:param m: An integer m denotes the size of a backpack | ||
:param A: Given n items with size A[i] | ||
:return: The maximum size | ||
""" | ||
n = len(A) | ||
f = [0 for _ in xrange(m+1)] # plus 1 for dummy | ||
for i in xrange(1, n+1): | ||
copy = list(f) | ||
for j in xrange(1, m+1): | ||
# decide whether to put A[i-1] | ||
if j-A[i-1]>=0: | ||
f[j] = max(copy[j], copy[j-A[i-1]]+A[i-1]) | ||
else: | ||
f[j] = copy[j] | ||
return f[m] | ||
|
||
if __name__=="__main__": | ||
print Solution().backPack(11, [2, 3, 5, 7]) |
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,31 +1,31 @@ | ||
""" | ||
Binary search is a famous question in algorithm. | ||
For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time | ||
complexity. | ||
If the target number does not exist in the array, return -1. | ||
""" | ||
__author__ = 'Danyang' | ||
class Solution: | ||
def binarySearch(self, nums, target): | ||
""" | ||
basics | ||
:param nums: The integer array | ||
:param target: Target number to find | ||
:return the first position of target in nums, position start from 0 | ||
""" | ||
l = 0 | ||
h = len(nums) | ||
while l<h: | ||
mid = (l+h)/2 | ||
if nums[mid]==target: | ||
while mid>=0 and nums[mid-1]==nums[mid]: mid -= 1 | ||
return mid | ||
elif nums[mid]<target: | ||
l = mid+1 | ||
else: | ||
h = mid | ||
return -1 | ||
""" | ||
Binary search is a famous question in algorithm. | ||
For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time | ||
complexity. | ||
If the target number does not exist in the array, return -1. | ||
""" | ||
__author__ = 'Danyang' | ||
class Solution: | ||
def binarySearch(self, nums, target): | ||
""" | ||
basics | ||
:param nums: The integer array | ||
:param target: Target number to find | ||
:return the first position of target in nums, position start from 0 | ||
""" | ||
l = 0 | ||
h = len(nums) | ||
while l<h: | ||
mid = (l+h)/2 | ||
if nums[mid]==target: | ||
while mid>=0 and nums[mid-1]==nums[mid]: mid -= 1 | ||
return mid | ||
elif nums[mid]<target: | ||
l = mid+1 | ||
else: | ||
h = mid | ||
return -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 |
---|---|---|
@@ -1,22 +1,22 @@ | ||
""" | ||
Compare two strings A and B, determine whether A contains all of the characters in B. | ||
""" | ||
__author__ = 'Danyang' | ||
class Solution: | ||
def compareStrings(self, A, B): | ||
""" | ||
:param A : A string includes Upper Case letters | ||
:param B : A string includes Upper Case letters | ||
:return : if string A contains all of the characters in B return True else return False | ||
""" | ||
cnt = [0 for _ in xrange(26)] | ||
for c in A: | ||
cnt[ord(c)-ord('A')] += 1 | ||
for c in B: | ||
cnt[ord(c)-ord('A')] -= 1 | ||
if cnt[ord(c)-ord('A')]<0: | ||
return False | ||
return True | ||
|
||
if __name__=="__main__": | ||
""" | ||
Compare two strings A and B, determine whether A contains all of the characters in B. | ||
""" | ||
__author__ = 'Danyang' | ||
class Solution: | ||
def compareStrings(self, A, B): | ||
""" | ||
:param A : A string includes Upper Case letters | ||
:param B : A string includes Upper Case letters | ||
:return : if string A contains all of the characters in B return True else return False | ||
""" | ||
cnt = [0 for _ in xrange(26)] | ||
for c in A: | ||
cnt[ord(c)-ord('A')] += 1 | ||
for c in B: | ||
cnt[ord(c)-ord('A')] -= 1 | ||
if cnt[ord(c)-ord('A')]<0: | ||
return False | ||
return True | ||
|
||
if __name__=="__main__": | ||
assert Solution().compareStrings("A", "")==True |
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,29 +1,29 @@ | ||
""" | ||
The code base version is an integer and start from 1 to n. One day, someone commit a bad version in the code case, so | ||
it caused itself and the following versions are all failed in the unit tests. | ||
""" | ||
__author__ = 'Danyang' | ||
class VersionControl: | ||
@classmethod | ||
def isBadVersion(cls, id): | ||
return True | ||
|
||
class Solution: | ||
def findFirstBadVersion(self, n): | ||
""" | ||
:param n: An integers. | ||
:return: An integer which is the first bad version. | ||
""" | ||
l = 1 | ||
h = n | ||
while l<=h: | ||
m = (l+h)/2 | ||
if not VersionControl.isBadVersion(m): | ||
l = m+1 | ||
else: | ||
h = m-1 | ||
|
||
return l | ||
|
||
""" | ||
The code base version is an integer and start from 1 to n. One day, someone commit a bad version in the code case, so | ||
it caused itself and the following versions are all failed in the unit tests. | ||
""" | ||
__author__ = 'Danyang' | ||
class VersionControl: | ||
@classmethod | ||
def isBadVersion(cls, id): | ||
return True | ||
|
||
class Solution: | ||
def findFirstBadVersion(self, n): | ||
""" | ||
:param n: An integers. | ||
:return: An integer which is the first bad version. | ||
""" | ||
l = 1 | ||
h = n | ||
while l<=h: | ||
m = (l+h)/2 | ||
if not VersionControl.isBadVersion(m): | ||
l = m+1 | ||
else: | ||
h = m-1 | ||
|
||
return l | ||
|
||
|
Oops, something went wrong.