Skip to content

Commit

Permalink
Tree, Heap, Trie, Sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueimaginate committed Mar 4, 2021
1 parent 8451523 commit ce1feac
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Coding Test/105.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
if inorder:

index = inorder.index(preorder.pop(0))

node = TreeNode(inorder[index])
node.left = self.buildTree(preorder, inorder[:index])
node.right = self.buildTree(preorder, inorder[index+1:])

return node
19 changes: 19 additions & 0 deletions Coding Test/147.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
cur = parent = ListNode(None)


while head:
while cur.next and cur.next.val < head.val:
cur = cur.next

cur.next, head.next, head = head, cur.next, head.next

cur = parent

return cur.next
30 changes: 30 additions & 0 deletions Coding Test/148.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 and l2:
if l1.val > l2.val:
l1, l2 = l2, l1
l1.next = self.mergeTwoLists(l1.next, l2)

return l1 or l2

def sortList(self, head: ListNode) -> ListNode:
if not (head and head.next):
return head

half, slow, fast = None, head, head
while fast and fast.next:
half, slow, fast = slow, slow.next, fast.next.next
half.next = None


l1 = self.sortList(head)
l2 = self.sortList(slow)

return self.mergeTwoLists(l1, l2)


16 changes: 16 additions & 0 deletions Coding Test/179.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def to_swap(self, num1, num2):
return str(num1) + str(num2) < str(num2) + str(num1)


def largestNumber(self, nums: List[int]) -> str:
i = 1
while i < len(nums):
j = i
while j > 0 and self.to_swap(nums[j-1], nums[j]):
nums[j-1], nums[j] = nums[j], nums[j-1]
j -= 1
i += 1


return str(int(''.join(map(str, nums))))
40 changes: 40 additions & 0 deletions Coding Test/208.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import re
import collections
import itertools
import heapq


class TrieNode:
def __init__(self):
self.word = False
self.children = collections.defaultdict(TrieNode)


class Trie:
def __init__(self):
self.root = TrieNode()

def insert(self, word:str) -> None:
node = self.root
for char in word:
node = node.children[char]

node.word = True

def search(self, word:str) -> bool:
node = self.root
for char in word:
if char not in node.children:
return False
node = node.children[char]

return node.word

def startsWith(self, prefix:str) -> bool:
node = self.root
for char in prefix:
if char not in node.children:
return False
node = node.children[char]

return True
38 changes: 38 additions & 0 deletions Coding Test/215.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import re
import collections
import itertools
import heapq


class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums, reverse=True)[k-1]


class Solution2:
def findKthLargest(self, nums: List[int], k: int) -> int:

heap = []

for num in nums:
heapq.heappush(heap, -num)

for _ in range(k-1):
heapq.heappop(heap)

return -heapq.heappop(heap)


class Solution3:
def findKthLargest(self, nums: List[int], k: int) -> int:

heapq.heapify(nums)

for _ in range(len(nums) - k):
heapq.heappop(nums)

return heapq.heappop(nums)

class Solution4:
def findKthLargest(self, nums: List[int], k: int) -> int:
return heapq.nlargest(k, nums)[-1]
3 changes: 3 additions & 0 deletions Coding Test/242.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return sorted(s) == sorted(t)
13 changes: 13 additions & 0 deletions Coding Test/56.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort(key = lambda x : x[0])

merged = []

for i in intervals:
if merged and i[0] <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], i[1])
else:
merged += [i]

return merged
28 changes: 28 additions & 0 deletions Coding Test/973.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import re
import collections
import itertools
import heapq


class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:

heap = []

for (x, y) in points:
dist = x**2 + y ** 2
heapq.heappush(heap, (dist, x, y))

result = []
for _ in range(k):
(dist, x, y) = heapq.heappop(heap)
result.append((x, y))

return result


class Solution2:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
points.sort(key=lambda x: x[0]*x[0] + x[1]*x[1])

return points[:k]
13 changes: 13 additions & 0 deletions Coding Test/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def bubble_sort(nums: list):
for i in range(1, len(nums)):
for j in range(0, len(nums)-1):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]

arr = [9,8,7,6,5,4,3,2,1]
bubble_sort(arr)
print(arr)



# O(n^2)
43 changes: 43 additions & 0 deletions Coding Test/heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class BinaryHeap:
def __init__(self):
self.items = [None]

def __len__(self):
return len(self.items) - 1

def _percolate_up(self):
i = len(self)
parent = i // 2
while parent > 0:
if self.items[i] < self.items[parent]:
self.items[parent], self.items[i] = self.items[i], self.items[parent]
i = parent
parent = i // 2

def insert(self, k):
self.items.append(k)
self._percolate_up()


def _percolate_down(self, idx):
left = idx * 2
right = idx * 2 + 1
smallest = idx

if left <= len(self) and self.items[left] < self.items[smallest]:
smallest = left

if right <= len(self) and self.items[right] < self.items[smallest]:
smallest = right

if smallest != idx:
self.items[idx], self.items[smallest] = self.items[smallest], self.items[idx]
self._percolate_down(smallest)

def extract(self):
extracted = self.items[1]
self.items[1] = self.items[len(self)]
self.items.pop()
self._percolate_down(1)
return extracted

41 changes: 41 additions & 0 deletions Coding Test/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def merge(left_nums, right_nums):
result = []
i, j = 0,0

while i < len(left_nums) and j < len(right_nums):
if left_nums[i] < right_nums[j]:
result.append(left_nums[i])
i += 1
else:
result.append(right_nums[j])
j += 1

while i < len(left_nums):
result.append(left_nums[i])
i += 1

while j < len(right_nums):
result.append(right_nums[i])
j += 1

return result



def merge_sort(nums):
if len(nums) < 2:
return nums[:]
else:
mid = len(nums) // 2
left = merge_sort(nums[:mid])
right = merge_sort(nums[mid:])
return merge(left, right)




a = [9,8,7,6,5,4,3,2,1]

b = merge_sort(a)

print(b)
24 changes: 24 additions & 0 deletions Coding Test/quick_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def quick_sort(nums, lo, hi):

def partition(lo, hi):
pivot = nums[hi]
left = lo

for right in range(lo, hi):
if nums[right] < pivot:
nums[left], nums[right] = nums[right], nums[left]
left += 1

nums[left], nums[hi] = nums[hi], nums[left]
return left

if lo < hi:
pivot = partition(lo, hi)
quick_sort(nums, lo, pivot - 1)
quick_sort(nums, pivot+1, hi)


a = [9,8,7,6,5,5,4,3,2,1]

quick_sort(a, 0, len(a)-1)
print(a)
20 changes: 20 additions & 0 deletions Coding Test/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import re
import collections
import itertools
import heapq

class TrieNode:
def __init__(self):
self.word = False
self.children = collections.defaultdict(TrieNode)


a = collections.defaultdict(TrieNode)



a[3]
a[4]


print(a)
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
7. Shortest Path
> 743, 787
8. Tree
> 104, 543, 687, 226, 617, 297, 110, 310, 1038, 938, 738
> 104, 543, 687, 226, 617, 297, 110, 310, 1038, 938, 738, 105
9. Heap
> 215
10. Trie
> 208(Trie.py), 336(Too hard)
## 알고리즘

1. Sorting
> 148, 56, 147, 179, 242, 973
2. Brute Force
3. Binary Search
4. Greedy Algorithm
Expand Down

0 comments on commit ce1feac

Please sign in to comment.