-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8451523
commit ce1feac
Showing
15 changed files
with
350 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters