Skip to content

Commit 744b2cb

Browse files
committed
minor fixes
1 parent 910414a commit 744b2cb

File tree

7 files changed

+37
-62
lines changed

7 files changed

+37
-62
lines changed

Array/DiagonalTraverse2/solution.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import List
22

3+
34
def findDiagonalOrder(nums: List[List[int]]) -> List[int]:
45
# make a index sum pair list [0][0] => 0, [0][1] => 1, [1][0] => 1...
56
indexSumPairs = list()
@@ -8,26 +9,25 @@ def findDiagonalOrder(nums: List[List[int]]) -> List[int]:
89
indexSumPairs.append((i+j, nums[i][j]))
910
# sort by index sum reversed (so it starts with "bottom" of nums array)
1011
indexSumPairs.sort(key=lambda pair: pair[0], reverse=True)
11-
return [v for _,v in reversed(indexSumPairs)]
12-
12+
return [v for _, v in reversed(indexSumPairs)]
1313

1414

1515
# test driver:
16-
nums = [[1,2,3],[4,5,6],[7,8,9]]
16+
nums = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
1717
print("Input: nums =", nums)
1818
print("Output:", findDiagonalOrder(nums))
1919

2020
print()
21-
nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
21+
nums = [[1, 2, 3, 4, 5], [6, 7], [8], [9, 10, 11], [12, 13, 14, 15, 16]]
2222
print("Input: nums =", nums)
2323
print("Output:", findDiagonalOrder(nums))
2424

2525
print()
26-
nums = [[1,2,3],[4],[5,6,7],[8],[9,10,11]]
26+
nums = [[1, 2, 3], [4], [5, 6, 7], [8], [9, 10, 11]]
2727
print("Input: nums =", nums)
2828
print("Output:", findDiagonalOrder(nums))
2929

3030
print()
31-
nums = [[1,2,3,4,5,6]]
31+
nums = [[1, 2, 3, 4, 5, 6]]
3232
print("Input: nums =", nums)
33-
print("Output:", findDiagonalOrder(nums))
33+
print("Output:", findDiagonalOrder(nums))

Array/FindDuplicateNumber/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def findDuplicate(self, nums: List[int]) -> int:
6+
tortoise = nums[nums[0]]
7+
hare = nums[tortoise]
8+
print("tortoise: {}, hare: {}".format(tortoise, hare))
9+
while tortoise != hare:
10+
tortoise = nums[tortoise]
11+
hare = nums[nums[hare]]
12+
print("tortoise: {}, hare: {}".format(tortoise, hare))
13+
tortoise = nums[0]
14+
while tortoise != hare:
15+
tortoise = nums[tortoise]
16+
hare = nums[hare]
17+
print("tortoise: {}, hare: {}".format(tortoise, hare))
18+
return tortoise
19+
20+
21+
# [1,3,4,2,2] equivalent to
22+
# idx 0 (1) -> idx 1 (3) -> idx 3 (2) -> idx 2 (4) -> idx 4 (2) -> idx 2 (4) ... cycle
23+
#
24+
sol = Solution()
25+
nums = [1, 3, 4, 2, 2]
26+
print(sol.findDuplicate(nums))
Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,13 @@
11
from typing import List
22

33

4-
class MaxHeap():
5-
def __init__(self):
6-
self.heap = []
7-
8-
def insert(self, val):
9-
self.heap.append(val)
10-
self.heapify()
11-
12-
def getMax(self):
13-
return self.heap[0]
14-
15-
def popMax(self):
16-
maxVal = self.heap.pop(0)
17-
self.heapify()
18-
19-
def size(self):
20-
return len(self.heap)
21-
22-
def heapify(self):
23-
for i in range(len(self.heap)-1, 0, -1):
24-
parentIndex = (i-1) // 2
25-
if self.heap[i][0] > self.heap[parentIndex][0]:
26-
self.heap[i], self.heap[parentIndex] = self.heap[parentIndex], self.heap[i]
27-
28-
def __str__(self):
29-
return str(self.heap)
30-
31-
def __iter__(self):
32-
return (x for x in self.heap)
33-
34-
354
class Solution:
365
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
37-
closest = list(list())
38-
# find Euclidean distance and put them in key value pair
39-
distances = []
40-
for point in points:
41-
squareSum = point[0]**2 + point[1]**2
42-
distances.append((squareSum, point))
43-
print(distances)
44-
# use max heap of size K to only hold K closest pairs
45-
maxHeap = MaxHeap()
46-
for distance, point in distances:
47-
# print(distance, point)
48-
# if maxheap has space, then insert
49-
if maxHeap.size() < K:
50-
maxHeap.insert((distance, point))
51-
else:
52-
if distance < maxHeap.getMax()[0]:
53-
maxHeap.popMax()
54-
maxHeap.insert((distance, point))
55-
# return all K pairs in max heap
56-
print([pair[1] for pair in maxHeap])
57-
closest.extend([pair[1] for pair in maxHeap])
58-
return closest
6+
sortedPoints = sorted(points, key=lambda pair: pair[0]**2+pair[1]**2)
7+
return sortedPoints[:K]
598

609

6110
sol = Solution()
6211
points = [[3, 3], [5, -1], [-2, 4], [3, 4], [1, 5], [-2, -5], [0, 8]]
63-
K = 4
12+
K = 2
6413
print(sol.kClosest(points, K))
File renamed without changes.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Languages used: Java and Python
1111
### [Array](./Array)
1212

1313
- [Add to Array-Form of Integer](Array/AddToArrayFormInteger)
14-
- [Binary Prefix Divisible by 5](Array/BinaryPrefixDivBy5)
1514
- [Container With Most Water](Array/ContainerWithMostWater)
1615
- [Diagonal Traverse II](Array/DiagonalTraverse2)
1716
- [Divide Arrays In K Consecutive Numbers](Array/DivideArrayInKConsecNums)
@@ -54,6 +53,7 @@ Languages used: Java and Python
5453

5554
### [Bit Manipulation](./BitManipulation)
5655

56+
- [Binary Prefix Divisible by 5](Array/BinaryPrefixDivBy5)
5757
- [Single Number II](BitManipulation/SingleNumber2)
5858
- [Total Hamming Distance](BitManipulation/TotalHammingDistance)
5959

0 commit comments

Comments
 (0)