Skip to content

Commit 36f31a2

Browse files
author
Partho Biswas
committed
1089_Duplicate_Zeros
1 parent b6993f0 commit 36f31a2

7 files changed

+180
-59
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ I have solved quite a number of problems from several topics. See the below tabl
167167
|56| **[849. Maximize Distance to Closest Person](https://tinyurl.com/v44bs88)** | [Python](https://tinyurl.com/wu6rdaw/849_Maximize_Distance_to_Closest_Person.py), [Swift](https://tinyurl.com/wuja3c4/849_Maximize_Distance_to_Closest_Person.swift) | [Art 1](https://tinyurl.com/qsenvnd) | Easy | Not so easy and intuitive. Can be solve is a variety of way. I have used Binary search(A bit over engineered solution). Can be simplifies |
168168
|57| **[283. Move Zeroes](https://tinyurl.com/y5kxjuvc)** | [Python](https://tinyurl.com/wu6rdaw/283_Move_Zeroes.py), [Swift](https://tinyurl.com/wuja3c4/283_Move_Zeroes.swift) | --- | Easy | Not so easy and intuitive. Uses fast and slow pointer |
169169
|58| **[163. Missing Ranges](https://tinyurl.com/s297jm7)** | [Python](https://tinyurl.com/wu6rdaw/163_Missing_Ranges.py), [Swift](https://tinyurl.com/wuja3c4/163_Missing_Ranges.swift) | --- | Medium | Man, it's a very trick problem |
170+
|59| **[1089. Duplicate Zeros](https://tinyurl.com/vdlwjwa)** | [Python](https://tinyurl.com/wu6rdaw/1089_Duplicate_Zeros.py), [Swift](https://tinyurl.com/wuja3c4/1089_Duplicate_Zeros.swift) | [Art 1](https://tinyurl.com/y3b2ynd4) | Easy | Not so easy and intuitive. Check again |
170171

171172

172173
</p>
@@ -575,7 +576,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
575576
| --- | --- | --- | --- | --- | --- |
576577
|01| **[1055. Shortest Way to Form String](https://leetcode.com/problems/shortest-way-to-form-string/)**| [Python](https://tinyurl.com/wu6rdaw/1055_Shortest_Way_to_Form_String.py)| [Art 1](https://tinyurl.com/t6xap4c), [Art 2](https://leetcode.com/problems/shortest-way-to-form-string/discuss/330938/Accept-is-not-enough-to-get-a-hire.-Interviewee-4-follow-up) | Medium | 📌 **[Check BS approach again](https://tinyurl.com/t6xap4c)** |
577578
|02| [1057. Campus Bikes](https://leetcode.com/problems/campus-bikes/)| [Python](https://tinyurl.com/wu6rdaw/1057_Campus_Bikes.py)| [Video 01](https://www.youtube.com/watch?v=tG7GFge4-fQ), [Article 01](https://leetcode.com/problems/campus-bikes/discuss/371604/Python-Solution-Using-Dictionary-With-Explanation)| Medium | 📌 Solve [1066. Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/) using [DP](https://leetcode.com/problems/campus-bikes-ii/discuss/303471/Python-DP-vs-Backtracking-with-comments), [DFS](https://leetcode.com/problems/campus-bikes-ii/discuss/303383/Python-DFS-with-memorization) and [Priority Queue](https://leetcode.com/problems/campus-bikes-ii/discuss/303422/Python-Priority-Queue) |
578-
|03| [1007. Minimum Domino Rotations For Equal Row](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/)| [Python](https://tinyurl.com/wu6rdaw/Minimum_Domino_Rotations_For_Equal_Row.py)| [Official](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/solution/), [Article 01](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/discuss/252242/JavaC%2B%2BPython-Different-Ideas)| Medium | 📌 Hard to spot if it is a Greedy |
579+
|03| **[1007. Minimum Domino Rotations For Equal Row](https://tinyurl.com/y6qgt5fk)** | [Python](https://tinyurl.com/wu6rdaw/Minimum_Domino_Rotations_For_Equal_Row.py)| [Vid 1](https://tinyurl.com/uobop48), [Vid 2](https://tinyurl.com/qkqlcmv),[Art 1](https://tinyurl.com/ufvb3kg),[Art 2](https://tinyurl.com/u85uegn),[Art 3](https://tinyurl.com/yxq7q9v6) | Medium | 📌 Hard to spot if it is a Greedy. Very Important |
579580
|04| [406. Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/)| [Python](https://tinyurl.com/wu6rdaw/406_Queue_Reconstruction_by_Height.py)| [Article 1](https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89345/Easy-concept-with-PythonC%2B%2BJava-Solution), [Article 2](https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89359/Explanation-of-the-neat-Sort%2BInsert-solution) | Medium | 📌 Fundamentals |
580581
|05| [621. Task Scheduler](https://leetcode.com/problems/task-scheduler/)| [Python](https://tinyurl.com/wu6rdaw/621_Task_Scheduler.py)| [Ex 1](https://leetcode.com/problems/task-scheduler/discuss/190390/Can-anyone-explain-to-me-what-the-problem-is-asking), [Ex 2](https://leetcode.com/problems/task-scheduler/discuss/130786/Python-solution-with-detailed-explanation), [Vid 1](https://www.youtube.com/watch?v=hVhOeaONg1Y), [Vid 2](https://www.youtube.com/watch?v=zPtI8q9gvX8), [Vid 3](https://www.youtube.com/watch?v=TV9VlHUR-Is), [Vid 4](https://www.youtube.com/watch?v=cr6Ip0J9izc&t=22s) | Medium | 📌 Extremely tricky. Classic problem |
581582
|06| [392. Is Subsequence](https://leetcode.com/problems/is-subsequence/)| [Python](https://tinyurl.com/wu6rdaw/392_Is_Subsequence.py)| [Ex 1](https://leetcode.com/problems/is-subsequence/discuss/87264/Easy-to-understand-binary-search-solution), [Ex 3](https://leetcode.com/problems/is-subsequence/discuss/87302/Binary-search-solution-for-follow-up-with-detailed-comments/144323) | Easy | 📌 |

algoexpert.io/python/Bubble_Sort.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11

22
# Best: O(n) time | O(1) space
3-
# Avarage: O(n^2) time | O(1) space
3+
# Average: O(n^2) time | O(1) space
44
# Worst: O(n^2) time | O(1) space
5-
def bubblesort(input_array):
6-
is_sorted = False
7-
counter = 0
8-
while not is_sorted:
9-
is_sorted = True
10-
for i in range(len(input_array) - 1 - counter):
11-
if input_array[i] > input_array[i + 1]:
12-
swap(i, i + 1, input_array)
13-
is_sorted = False
14-
counter += 1
15-
return input_array
16-
5+
def bubbleSort(array):
6+
isSorted = False
7+
counter = 0
8+
while not isSorted:
9+
isSorted = True
10+
for i in range(len(array) - 1 - counter):
11+
if array[i] > array[i + 1]:
12+
swap(i, i + 1, array)
13+
isSorted = False
14+
counter += 1
15+
return array
1716

1817
def swap(i, j, array):
19-
array[i], array[j] = array[j], array[i]
20-
18+
array[i], array[j] = array[j], array[i]
2119

algoexpert.io/python/Find_Loop.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11

22
# O(n) time | O(1) space
3+
class LinkedList:
4+
def __init__(self, value):
5+
self.value = value
6+
self.next = None
7+
38
def findLoop(head):
4-
first = head.next
5-
second = head.next.next
6-
while first != second:
7-
first = first.next
8-
second = second.next.next
9-
first = head
10-
while first != second:
11-
first = first.next
12-
second = second.next
13-
return first
9+
if not head and not head.next:
10+
return None
11+
slowPtr, fastPtr = head.next, head.next.next
12+
while slowPtr != fastPtr:
13+
slowPtr = slowPtr.next
14+
fastPtr = fastPtr.next.next
15+
fastPtr = head
16+
while fastPtr != slowPtr:
17+
slowPtr = slowPtr.next
18+
fastPtr = fastPtr.next
19+
return fastPtr

algoexpert.io/python/Insertion_Sort.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33

44
# Best: O(n) time | O(1) space
5-
# Avarage: O(n^2) time | O(1) space
5+
# Average: O(n^2) time | O(1) space
66
# Worst: O(n^2) time | O(1) space
7-
def insersion_sort(array):
8-
for i in range(1, len(array)):
9-
j = i
10-
while j > 0 and array[j] < array[j - 1]:
11-
array[j], array[j - 1] = array[j - 1], array[j]
12-
j -= 1
13-
return array
7+
def insertionSort(array):
8+
for i in range(1, len(array)):
9+
j = i
10+
while j > 0 and array[j] < array[j - 1]:
11+
swap(j, j - 1, array)
12+
j -= 1
13+
return array
14+
def swap(i, j, array):
15+
array[i], array[j] = array[j], array[i]
1416

1517

1618
input_array = [8, 5, 2, 9, 5, 6, 3]
17-
sorted_array = insersion_sort(input_array)
19+
sorted_array = insertionSort(input_array)
1820
print("Sorted Array: ", sorted_array)
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
2-
3-
4-
51
def maxSumIncreasingSubsequence(array):
6-
sequences = [None for x in array]
7-
sums = [num for num in array]
8-
maxSumIdx = 0
9-
for i in range(len(array)):
10-
currentNum = array[i]
11-
for j in range(0, i):
12-
otherNum = array[j]
13-
if otherNum < currentNum and sums[j] + currentNum >= sums[i]:
14-
sums[i] = sums[j] + currentNum
15-
sequences[i] = j
16-
if sums[i] >= sums[maxSumIdx]:
17-
maxSumIdx = i
18-
return [sums[maxSumIdx], buildSequence(array, sequences, maxSumIdx)]
19-
2+
sequeces = [None for _ in array]
3+
sums = [num for num in array]
4+
maxSumIdx = 0
5+
for i in range(len(array)):
6+
currentum = array[i]
7+
for j in range(0, i):
8+
otherum = array[j]
9+
if otherum < currentum and sums[j] + currentum >= sums[i]:
10+
sums[i] = sums[j] + currentum
11+
sequeces[i] = j
12+
if sums[maxSumIdx] <= sums[i]:
13+
maxSumIdx = i
14+
increasigSequence = buildSequece(array, sequeces, maxSumIdx)
15+
return [sums[maxSumIdx], increasigSequence]
2016

21-
def buildSequence(array, sequences, currentIdx):
22-
sequence = []
23-
while currentIdx is not None:
24-
sequence.append(array[currentIdx])
25-
currentIdx = sequences[currentIdx]
26-
return list(reversed(sequence))
17+
def buildSequece(array, sequeces, curIdx):
18+
sequen = []
19+
while curIdx is not None:
20+
sequen.append(array[curIdx])
21+
curIdx = sequeces[curIdx]
22+
return list(reversed(sequen))
2723

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Source: https://tinyurl.com/v3zqer7
2+
# Approach 1
3+
class Solution(object):
4+
def minDominoRotations(self, A, B):
5+
"""
6+
:type A: List[int]
7+
:type B: List[int]
8+
:rtype: int
9+
"""
10+
result = float("inf")
11+
for domino in range(1, 7): # Since each domino can have only 1 to 6 values. So check all values if we can make it
12+
isPossible = True
13+
topRorationCount, bottomRotationCount = 0, 0
14+
for a, b in zip(A, B):
15+
if domino != a and domino != b: #
16+
isPossible = False
17+
break
18+
if domino == a and domino != b:
19+
bottomRotationCount += 1
20+
elif domino != a and domino == b:
21+
topRorationCount += 1
22+
if isPossible:
23+
result = min(result, min(topRorationCount, bottomRotationCount))
24+
return -1 if result == float("inf") else result
25+
26+
27+
28+
# Source: https://tinyurl.com/v3zqer7
29+
# Approach 2
30+
class Solution(object):
31+
def minDominoRotations(self, A, B):
32+
"""
33+
:type A: List[int]
34+
:type B: List[int]
35+
:rtype: int
36+
"""
37+
rotations = self.checkRotationFor(A, B, A[0])
38+
# If one could make all elements in A or B equal to A[0]
39+
if rotations != -1 or A[0] == B[0]:
40+
return rotations
41+
# If one could make all elements in A or B equal to B[0]
42+
else:
43+
return self.checkRotationFor(A, B, B[0])
44+
45+
def checkRotationFor(self, A, B, num):
46+
"""
47+
Return minimum number of swaps,
48+
if one could make all elements in A or B equal to 'num'.
49+
Else return -1
50+
"""
51+
# How many rotations should be done
52+
# to have all elements in A equal to 'num'
53+
# and to have all elements in B equal to 'num'
54+
length = len(A)
55+
rotations_A, rotations_B = 0, 0
56+
for i in range(length):
57+
if A[i] != num and B[i] != num:
58+
return -1
59+
elif A[i] != num:
60+
rotations_A += 1
61+
elif B[i] != num:
62+
rotations_B += 1
63+
return min(rotations_A, rotations_B)
64+
65+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# My initial solution. 14 / 30 test cases passed.
2+
class Solution(object):
3+
def duplicateZeros(self, arr):
4+
"""
5+
:type arr: List[int]
6+
:rtype: None Do not return anything, modify arr in-place instead.
7+
"""
8+
zeros = []
9+
for i in range(len(arr)):
10+
if arr[i] == 0:
11+
zeros.append(i)
12+
for i in zeros:
13+
if i + 2 < len(arr):
14+
prevNum = arr[i + 1]
15+
arr[i + 1] = 0
16+
for j in range(i + 2, len(arr)):
17+
currrentNum = arr[j]
18+
arr[j] = prevNum
19+
prevNum = currrentNum
20+
21+
# Vary inefficient solution. Does't respect the constraints.
22+
class Solution:
23+
def duplicateZeros(self, arr):
24+
i = 0
25+
n = len(arr)
26+
while(i<n):
27+
if arr[i] == 0:
28+
arr.pop()
29+
arr.insert(i,0)
30+
i+=1
31+
i+=1
32+
33+
# Optimised and good solution
34+
# Source: https://tinyurl.com/qnxv356
35+
class Solution:
36+
def duplicateZeros(self, arr):
37+
"""
38+
Do not return anything, modify arr in-place instead.
39+
"""
40+
shift = 0
41+
l = len(arr)
42+
for i in range(l):
43+
if arr[i] == 0:
44+
shift += 1
45+
for i in range(l-1, -1, -1):
46+
# put the shifted number in the right spot
47+
if i + shift < l:
48+
arr[i+shift] = arr[i]
49+
# if we meet a 0, we need to duplicate 0
50+
if arr[i] == 0:
51+
shift -= 1
52+
if i + shift < l:
53+
arr[i+shift] = 0

0 commit comments

Comments
 (0)