diff --git a/solution/053.Maximum Subarray/Solution.py b/solution/053.Maximum Subarray/Solution.py new file mode 100644 index 0000000000000..603a808519ead --- /dev/null +++ b/solution/053.Maximum Subarray/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def maxSubArray(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + n=len(nums) + if n == 1: + return nums[0] + res=[0]*n + res[0]=nums[0] + max0=nums[0] + for i in range(1,n): + res[i]=max(res[i-1]+nums[i],nums[i]) + max0=max(res[i],max0) + return max0 \ No newline at end of file diff --git a/solution/062.Unique Paths/Solution.py b/solution/062.Unique Paths/Solution.py new file mode 100644 index 0000000000000..a0805d3d68a9c --- /dev/null +++ b/solution/062.Unique Paths/Solution.py @@ -0,0 +1,15 @@ +class Solution: + def uniquePaths(self, m, n): + """ + :type m: int + :type n: int + :rtype: int + """ + res = [[0]*m]*n + for i in range(n): + for j in range(m): + if i == 0 or j==0: + res[i][j] = 1 + else: + res[i][j] = res[i][j-1]+res[i-1][j] + return res[n-1][m-1] \ No newline at end of file diff --git a/solution/063.Unique Paths II/Solution.py b/solution/063.Unique Paths II/Solution.py new file mode 100644 index 0000000000000..1a007d3f53c2a --- /dev/null +++ b/solution/063.Unique Paths II/Solution.py @@ -0,0 +1,22 @@ +class Solution(object): + def uniquePathsWithObstacles(self, obstacleGrid): + """ + :type obstacleGrid: List[List[int]] + :rtype: int + """ + m, n = len(obstacleGrid), len(obstacleGrid[0]) + martix = [[0] * n] * m + for i in range(m): + for j in range(n): + if obstacleGrid[i][j] == 1: + martix[i][j] = 0 + else: + if i == 0 and j == 0: + martix[i][j] = 1 + elif i == 0: + martix[i][j] = martix[i][j - 1] + elif j == 0: + martix[i][j] = martix[i - 1][j] + else: + martix[i][j] = martix[i - 1][j] + martix[i][j - 1] + return martix[m - 1][n - 1] \ No newline at end of file diff --git a/solution/070.Climbing Stairs/Solution.py b/solution/070.Climbing Stairs/Solution.py new file mode 100644 index 0000000000000..6175378fff60d --- /dev/null +++ b/solution/070.Climbing Stairs/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def climbStairs(self, n): + """ + :type n: int + :rtype: int + """ + if n<3: + return n + res=[None]*(n+1) + res[1]=1 + res[2]=2 + for i in range(3,n+1): + res[i]=res[i-1]+res[i-2] + return res[n] \ No newline at end of file diff --git a/solution/153.Find Minimum in Rotated Sorted Array/Solution.py b/solution/153.Find Minimum in Rotated Sorted Array/Solution.py new file mode 100644 index 0000000000000..e1633d97956da --- /dev/null +++ b/solution/153.Find Minimum in Rotated Sorted Array/Solution.py @@ -0,0 +1,30 @@ +class Solution: + def findMin(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + return min(nums) + +class Solution: + def findMin(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + length=len(nums) + if length == 1: + return nums[0] + l=0 + r=length-1 + m=r//2 + + while lnums[m]: + r=m + else: + l=m+1 + m=(l+r)//2 + return nums[l] \ No newline at end of file diff --git a/solution/198.House Robber/Solution.py b/solution/198.House Robber/Solution.py new file mode 100644 index 0000000000000..52c288c3ec6ee --- /dev/null +++ b/solution/198.House Robber/Solution.py @@ -0,0 +1,19 @@ +class Solution: + def rob(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + length=len(nums) + if length == 0: + return 0 + if length == 1: + return nums[0] + res=[0]*length + res[0]=nums[0] + res[1]=max(nums[0],nums[1]) + + for i in range(2,length): + res[i]=max(nums[i]+res[i-2],res[i-1]) + + return res[-1] \ No newline at end of file