Skip to content

Commit db3c07f

Browse files
committedOct 15, 2020
Added leetcode
1 parent 2b51d48 commit db3c07f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
 
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.
3+
4+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
5+
6+
Example 1:
7+
8+
Given nums = [1,1,2],
9+
10+
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
11+
12+
It doesn't matter what you leave beyond the returned length.
13+
Example 2:
14+
15+
Given nums = [0,0,1,1,1,2,2,3,3,4],
16+
17+
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
18+
19+
It doesn't matter what values are set beyond the returned length.
20+
Clarification:
21+
22+
Confused why the returned value is an integer but your answer is an array?
23+
24+
Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller as well. """
25+
26+
def removeDuplicates(self, nums) -> int:
27+
if len(nums) == 1:
28+
return 1
29+
i = 1
30+
while i < len(nums):
31+
if nums[i] == nums[i - 1]:
32+
del nums[i]
33+
continue
34+
i += 1
35+
return len(nums)

‎Recursion/coin-change.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ def rec(target, coins, known):
55
known[target] = 1
66
return 1
77

8+
# condition if there isn't a way to make the desired target
9+
elif target < min(coins):
10+
return 0
11+
812
elif known[target] > 0:
913
return known[target]
1014

@@ -14,10 +18,15 @@ def rec(target, coins, known):
1418
if num < min_coins:
1519
min_coins = num
1620
known[target] = min_coins
21+
22+
# condition if there isn't a way to make the desired target
23+
if min_coins == 1:
24+
if target not in coins:
25+
return 0
1726
return min_coins
1827

1928
if __name__ == "__main__":
2029
target = 12
21-
coins = [1, 4, 8, 10]
30+
coins = [4, 7, 10]
2231
known = [0] * (target + 1)
2332
print(rec(target, coins, known))

‎test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def mincoins(target, coins, known):
1717
return res
1818

1919
if __name__ == "__main__":
20-
print(mincoins(10, [1, 3, 7, 8], [None]*11))
20+
print(mincoins(10, [5, 3], [None]*11))

0 commit comments

Comments
 (0)