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 / 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 / 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 / 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 / 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 / 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 / rotated_sorted_array.py
Last active November 30, 2022 19:24
Count number of times sorted array has been rotated
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]
@Per48edjes
Per48edjes / happy_number.py
Created November 29, 2022 03:03
Leetcode 202. Happy Number
def isHappy(n: int) -> bool:
"""
Leetcode 202. Happy number
>>> isHappy(7)
True
>>> isHappy(19)
True
>>> isHappy(2)
False
@Per48edjes
Per48edjes / count_array_inversions.py
Created November 26, 2022 21:48
Implement 'MergeSort + Count Inversions' algorithm on a 1-D array
#!/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)
@Per48edjes
Per48edjes / karatsuba_multiplication.py
Created November 26, 2022 05:05
Recursive Python implementation of Karatsuba multiplication
#!/usr/bin/env python3
def karatsuba_mul(m: int, n: int) -> int:
"""
Implementation of recursive Karatsuba multiplication
>>> karatsuba_mul(56, 12)
672
>>> karatsuba_mul(78, 34)
@Per48edjes
Per48edjes / catalan_numbers.py
Created November 3, 2022 03:35
Exploring recursive + closed form solutions to generating Catalan numbers
"""
Inspired by Problem 3, Lab 09 from Berkeley's CS 61A course to investigate and
implement the closed form solution via `catalan_nums` function
"""
from math import comb
def num_trees(n):
"""Returns the number of unique full binary trees with exactly n leaves. E.g.,