Skip to content

Commit

Permalink
Line Ending
Browse files Browse the repository at this point in the history
  • Loading branch information
idf committed Dec 21, 2014
1 parent adfee35 commit d8248a9
Show file tree
Hide file tree
Showing 13 changed files with 699 additions and 696 deletions.
480 changes: 240 additions & 240 deletions .gitignore

Large diffs are not rendered by default.

58 changes: 29 additions & 29 deletions Backpack II.py
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]
148 changes: 74 additions & 74 deletions Backpack.py
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])
62 changes: 31 additions & 31 deletions Binary Search.py
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

42 changes: 21 additions & 21 deletions Compare Strings.py
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
58 changes: 29 additions & 29 deletions First Bad Version.py
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


Loading

0 comments on commit d8248a9

Please sign in to comment.