This file contains hidden or 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
-- Minimal nvim config with lazy | |
-- Assumes a directory in $NVIM_DATA_MINIMAL | |
-- Start with | |
-- | |
-- export NVIM_DATA_MINIMAL=$(mktemp -d) | |
-- export NVIM_APP_NAME="nvim-ht-minimal" | |
-- nvim -u minimal.lua | |
-- | |
-- Then exit out of neovim and start again. |
This file contains hidden or 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
from typing import Optional | |
class MinHeap: | |
""" | |
A MinHeap is a complete binary tree where the value of each node is less than or equal | |
to the value of its children. (The root node is the minimum element in the heap.) | |
This implementation supports the following operations: |
This file contains hidden or 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
# This is the same thing as finding the (len(nums) - (k-1))th order statistic. | |
# - Note: We need to transform "Kth Largest, 1-indexed" into "Kth Smallest, 0-indexed" | |
# Idea: Use randomized quickselect! It has O(n) expected time | |
import random | |
class Solution: | |
# O(n) time, O(1) space |
This file contains hidden or 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
# Let's implement Randomized QuickSort! | |
import random | |
class Solution: | |
# O(n) | |
def partition(self, nums: List[int], l: int, r: int) -> int: | |
# Choose random pivot, put it first in the array | |
random_idx = random.choice(range(l, r + 1)) | |
nums[l], nums[random_idx] = nums[random_idx], nums[l] |
This file contains hidden or 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
main :: IO () | |
main = undefined | |
toDigitsRev :: Integer -> [Integer] | |
toDigitsRev n | |
| n <= 0 = [] | |
| otherwise = (n `mod` 10) : toDigitsRev (n `div` 10) | |
This file contains hidden or 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
from typing import Optional | |
class ListNode: | |
def __init__(self, val, next=None): | |
self.val = val | |
self.next = next | |
def hasCycle(head: Optional[ListNode]) -> bool: |
This file contains hidden or 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
from typing import Optional | |
""" | |
LeetCode 206. Reverse Linked List | |
""" | |
class ListNode: | |
def __init__(self, val, next=None): |
This file contains hidden or 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
def num_rotations(nums: list) -> int: | |
""" | |
Returns the number of times a sorted array has been rotated to the right | |
with the numbers wrapping to the beginning when they are rotated at the end | |
of the list | |
>>> nums = [3, 4, 5, 1, 2] | |
>>> num_rotations(nums) | |
3 | |
>>> nums2 = [1, 2, 3, 4, 5] |
This file contains hidden or 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
def isHappy(n: int) -> bool: | |
""" | |
Leetcode 202. Happy number | |
>>> isHappy(7) | |
True | |
>>> isHappy(19) | |
True | |
>>> isHappy(2) | |
False |
This file contains hidden or 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
#!/usr/bin/env python3 | |
def merge_count_split_inversions(left_sorted: list, right_sorted: list, lst_len: int) -> tuple: | |
""" | |
Perform merge of two sorted lists and count inversions while merging | |
>>> merge_count_split_inversions([2], [1], 2) | |
([1, 2], 1) | |
>>> merge_count_split_inversions([1, 3, 5], [2, 4, 6], 6) |