Skip to content

Instantly share code, notes, and snippets.

View Per48edjes's full-sized avatar
👉
This is a good point.

Ravi Dayabhai Per48edjes

👉
This is a good point.
View GitHub Profile
@Per48edjes
Per48edjes / reverse_linked_list.py
Created November 29, 2022 19:51
LeetCode 206. Reverse Linked List
from typing import Optional
"""
LeetCode 206. Reverse Linked List
"""
class ListNode:
def __init__(self, val, next=None):
@Per48edjes
Per48edjes / linked_list_cycle_detection.py
Created November 30, 2022 02:32
LeetCode 141. Linked List Cycle
from typing import Optional
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
def hasCycle(head: Optional[ListNode]) -> bool:
@Per48edjes
Per48edjes / cis194_hw1.hs
Created March 8, 2023 21:47
UPenn CIS 194 HW 1
main :: IO ()
main = undefined
toDigitsRev :: Integer -> [Integer]
toDigitsRev n
| n <= 0 = []
| otherwise = (n `mod` 10) : toDigitsRev (n `div` 10)
@Per48edjes
Per48edjes / randomized_quicksort.py
Created April 14, 2023 20:17
Leetcode 912. Sort an Array (Randomized QuickSort)
# 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]
@Per48edjes
Per48edjes / randomized_quickselect.py
Last active April 18, 2023 03:03
Leetcode 215. Kth Largest Element in Array
# 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
@Per48edjes
Per48edjes / minheap_pq.py
Last active May 19, 2023 03:17
Python implementation of MinHeap priority queue
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:
@Per48edjes
Per48edjes / init.lua
Created September 13, 2023 20:10
Debugging HLS/Neovim/InsertLeave/FoldRange issue
-- 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.
@Per48edjes
Per48edjes / super_egg_drop.ipynb
Last active November 14, 2023 20:19
Journey optimizing my solution to Leetcode 887. Super Egg Drop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Per48edjes
Per48edjes / lc_2870_explainer.md
Last active January 4, 2024 18:14
Leetcode 2870 explainer (for Recurse Center)
class Solution:

    def minOperations(self, nums: List[int]) -> int:

        def opMin(freq: int) -> int:
            assert freq > 1, f"{opMin.__name__} only takes freq > 1"
            call_stack = [(freq, 0)]
            while call_stack:
 k, ops = call_stack.pop()
@Per48edjes
Per48edjes / lc_907_explainer.md
Last active February 9, 2024 18:21
Leetcode 907 explainer (for Recurse Center)
import math


class Solution:
    def sumSubarrayMins(self, arr: List[int]) -> int:
        n = len(arr)
        spanned_indices = [[1,1] for _ in range(n + 1)]
        mono_i_stack = []
 for i, num in enumerate(arr + [-math.inf]):