Skip to content

Commit c544dd1

Browse files
authored
+6 Solutions (2 Arrays + 1 BinarySearch + 3 DP) (#34)
* August Monthly Leetcode Day-1 * Added 6 Solutions (2 Array + 1 BinarySearch + 3 DP)
1 parent 3413989 commit c544dd1

7 files changed

+128
-1
lines changed

Diff for: Python/best-time-to-buy-and-sell-stock-ii.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Speed: 99.14%
3+
Memory:
4+
Time Complexity: O(n)
5+
'''
6+
class Solution:
7+
def solve(self, prices):
8+
# Write your code here
9+
if len(prices)<2:return 0
10+
buy = prices[0]
11+
profit = 0
12+
for i in range(1,len(prices)):
13+
if prices[i]<prices[i-1]:
14+
profit+=prices[i-1]-buy
15+
buy = prices[i]
16+
profit+=prices[-1]-buy
17+
return profit

Diff for: Python/decode-ways.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Speed: 93.43%
3+
Memory: 38.43%
4+
Time Complexity: O(n)
5+
'''
6+
class Solution:
7+
def numDecodings(self, s: str) -> int:
8+
if not s or s[0]=='0':
9+
return 0
10+
dp = [0]*(len(s)+1)
11+
dp[0],dp[1] = 1,1
12+
for j in range(2,len(s)+1):
13+
i = j-1
14+
a = int(s[i])
15+
b = int(s[i-1:i+1])
16+
if 0<a<10:
17+
dp[j]=dp[j-1]
18+
if s[i-1]!='0' and 0<b<27:
19+
dp[j]+=dp[j-2]
20+
return dp[-1]
21+
22+
23+
##Improved
24+
'''
25+
Speed: 93.14%
26+
Memory: 72.58%
27+
'''
28+
class Solution:
29+
def numDecodings(self, s: str) -> int:
30+
if not s or s[0]=='0':
31+
return 0
32+
p,q = 1,1
33+
for i in range(1,len(s)):
34+
a = int(s[i])
35+
y = 0
36+
if 0<a<10:
37+
y=q
38+
if s[i-1]!='0' and 0<int(s[i-1:i+1])<27:
39+
y+=p
40+
p,q = q,y
41+
return q

Diff for: Python/divisor-game.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'''
2+
Speed: 5.11%
3+
Memory: 9.88%
4+
Time Complexity: O(N^2)
5+
'''
6+
class Solution:
7+
def divisorGame(self, N: int) -> bool:
8+
dp = [False]*(N+1)
9+
for i in range(N+1):
10+
for j in range(1,i):
11+
if i%j==0 and dp[i-j]==False:
12+
dp[i] = True
13+
return dp[N]

Diff for: Python/edit-distance.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Speed: 99.48%
3+
Memory: 81.78%
4+
Time Complexity: O(N*M) where N = len(s1) and M = len(s2)
5+
'''
6+
class Solution:
7+
def minDistance(self, s1: str, s2: str) -> int:
8+
@lru_cache(None)
9+
def editdistance(i,j,s1=s1,s2=s2):
10+
if not i:
11+
return j
12+
if not j:
13+
return i
14+
if s1[i-1]==s2[j-1]:
15+
return editdistance(i-1,j-1)
16+
else:
17+
return 1 + min(editdistance(i-1,j),editdistance(i,j-1),editdistance(i-1,j-1))
18+
# Insert,Delete,replace
19+
return editdistance(len(s1),len(s2),s1,s2)

Diff for: Python/find-minimum-in-rotated-sorted-array.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Speed: 37.59%
3+
Memory: 20.90%
4+
Time Complexity: O(logn)
5+
'''
6+
class Solution:
7+
def findMin(self, nums: List[int]) -> int:
8+
if nums[0]<=nums[-1]:
9+
return nums[0]
10+
l,h = 0,len(nums)-1
11+
while l<=h:
12+
mid = h - (h-l)//2
13+
if mid>0 and nums[mid]<nums[mid-1]:
14+
return nums[mid]
15+
elif nums[mid]>nums[0]:
16+
l = mid+1
17+
else:
18+
h = mid-1

Diff for: Python/pascals-triangle-ii.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'''
2+
Speed: 77.11%
3+
Memory: 82.57%
4+
Time Complexity: O(n)
5+
'''
6+
class Solution:
7+
def getRow(self, k: int) -> List[int]:
8+
res = [1]*(k+1)
9+
#res[0]=1
10+
for i in range(1,k):
11+
for j in range(i,0,-1):
12+
res[j]+=res[j-1]
13+
return res

Diff for: README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
8282

8383
# Array
8484

85-
| # | Title | Solution | Time | Space | Difficulty | Video Explaination | Note |
85+
| # | Title | Solution | Time | Space | Difficulty | Note | Video Explaination |
8686
| --- | ---------------------------------------------------------------------- | ---------------------------------------------- | ---------- | ------ | ---------- | --------- | ---- |
8787
| 56 | [Merge Intervals](https://leetcode.com/problems/merge-intervals) | [Python](./Python/56_MergeIntervals.py) | _O(nlogn)_ | _O(n)_ | Medium | Intervals | |
8888
| 268 | [Missing Number](https://leetcode.com/problems/missing-number) | [Java](./Java/missing-number.java) | _O(n)_ | _O(1)_ | Easy | Array | [Tutorial](https://youtu.be/VwvGEE_OGss) |
8989
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | [Java](./Java/Degree-of-an-Array.java) | _O(n)_ | _O(n)_ | Easy | Array | |
9090
| 1089 | [Duplicate Zeroes](https://leetcode.com/problems/duplicate-zeros/) | [JavaScript](./JavaScript/Duplicate-Zeroes.js) | _O(n)_ | _O(n)_ | Easy | Array | |
9191
| 1502 | [Can Make Arithmetic Progression From Sequence](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/) | [Java](./Java/can-make-arithmetic-progression-from-sequence.java) | _O(n)_ | _O(1)_ | Easy | Array | |
92+
| 122 | [Best Time to buy and sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [Python](./Python/best-time-to-buy-and-sell-stock-ii.py) | _O(N)_ | _O(1)_ | Medium | Stocks | |
93+
| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | [Python](./Python/pascals-triangle-ii.py) | _O(N^2)_ | _O(K)_ | Easy | | |
9294

9395

9496
<br/>
@@ -248,6 +250,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
248250
| 343 | [Integer Break](https://leetcode.com/problems/integer-break/) | [C++](./C++/Integer-Break.cpp) | _O(n^2)_ | _O(n)_ | Medium | |
249251
| 139 | [Word Break](https://leetcode.com/problems/word-break/) | [Python](./Python/word-break-1.py) | _O(n^3)_ | _O(n)_ | Medium | DP | |
250252
| 1092 | [Shortest Common Supersequence](https://leetcode.com/problems/shortest-common-supersequence/) | [C++](./C++/Shortest-Common-Supersequence.cpp) | _O(n^2)_ | _O(n^2)_ | Hard | DP | |
253+
| 72 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | [Python](./Python/edit-distance.py) | _O(N*M)_ | _O(n^2)_ | Medium | Levenshtein Distance | |
254+
| 91 | [Decode ways](https://leetcode.com/problems/decode-ways/) | [Python](./Python/decode-ways.py) | _O(N)_ | _O(N)_ | Easy | DP | |
255+
| 1025| [Divisor Game](https://leetcode.com/problems/divisor-game/) | [Python](./Python/divisor-game.py) | _O(N^2)_ | _O(N)_ | Easy | DP | |
251256

252257

253258
<br/>
@@ -263,6 +268,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
263268
| 035 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Python](./Python/35.SearchInsertPosition.py) | _O(logn)_ | _O(1)_ | Easy | | Binary Search |
264269
| 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [Java](./Java/May-LeetCoding-Challenge/Day-1-First-Bad-Version.java) <br> [JavaScript](./JavaScript/First-Bad-Version.js) | _O(logn)_ | _O(1)_ | Easy | | Binary Search |
265270
| 033 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Python](./Python/search-in-rotated-sorted-array.py) | _O(logn)_ | _O(1)_ | Medium | | Binary Search |
271+
| 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [Python](./Python/find-minimum-in-rotated-sorted-array.py) | _O(logn)_ | _O(1)_ | Medium | | Binary Search |
266272

267273
<br/>
268274
<div align="right">

0 commit comments

Comments
 (0)