Skip to content

Instantly share code, notes, and snippets.

View codecakes's full-sized avatar
💭
I may be slow to respond.

codecakes codecakes

💭
I may be slow to respond.
View GitHub Profile
@codecakes
codecakes / minimum_coin_total.py
Created October 4, 2021 22:00
Minimum sum in array
"""
https://www.algoexpert.io/questions/Non-Constructible%20Change
"""
def nonConstructibleChange(coins):
if not coins:
return 1
minimum = 1
total = 0
for num in sorted(coins):
@codecakes
codecakes / minimum_hops.py
Created October 4, 2021 21:59
jumping on clouds
"""
https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem
"""
def jumpingOnClouds(c, idx = 0, cache = None):
n = len(c)
jumps = 0
for idx in iter(lambda: idx, -1):
if c[idx] == 0:
if idx + 2 < n and c[idx+2] == 0:
@codecakes
codecakes / longest_common_subsequence.py
Created October 1, 2021 23:18
Given two strings s1 and s2 return the length of the longest common subsequence of characters between the two strings.
"""
Input:
s1 = "ADC"
s2 = "ABCD"
Output: 2
Explanation:
"ADC"
"ABCD"
Both strings share the subsequence "A", "D".
@codecakes
codecakes / matrix_rotate_clockwise.py
Last active September 21, 2021 10:55
clockwise rotate 2d matrix
class Solution:
def rotate(self, matrix):
'''
:type matrix: list of list of int
:rtype: list of list of int
'''
# assuming nxm
ROWS = len(matrix)
COLS = len(matrix[0])
@codecakes
codecakes / asyncio_loop.py
Created September 13, 2021 12:32
asyncio 101
import contextlib
import asyncio
@contextlib.asynccontextmanager
async def manage_sleep():
try:
print('going to sleep...')
yield asyncio.sleep(5)
finally:
print("sleep is over!!")
@codecakes
codecakes / three_pointer_sort_array.py
Created September 10, 2021 22:18
You are given an array nums containing only 0 , 1 and 2. You need to sort the array in ascending order.
"""
Example 1:
Input: [0,1,2,1,2,2,2,1,0]
Output: [0,0,1,1,1,2,2,2,2]
Example 2:
Input: [2,2,1,1,1,0,0,0,0]
@codecakes
codecakes / base_change.py
Created September 8, 2021 16:11
Given an integer (represented as a string) under base b1, convert it to base b2.
"""
Given an integer (represented as a string) under base b1, convert it to base b2.
Example 1:
Input: ("12", 10, 2)
Output: "1100"
Explanation: We are converting "12" which is under base 10 (decimal) to base 2 (binary)
@codecakes
codecakes / time_diff_str.py
Created September 8, 2021 08:42
Find Minimum Time Difference
class Solution:
"""Given an array of time strings times, return the smallest difference between any two times in minutes.
Example:
Input: ["00:03", "23:59", "12:03"]
Output: 4
Input: The closest 2 times are "00:03" and "23:59" (by wrap-around), and they differ by 4 minutes.
@codecakes
codecakes / carry_over_addition.py
Created September 7, 2021 16:52
some shitty way to convert str to numbers and then carry on addition
class Solution:
def addStrings(self, s1, s2):
'''
:type s1: str
:type s2: str
:rtype: str
'''
i = s1_len = len(s1) - 1
j = s2_len = len(s2) - 1
@codecakes
codecakes / reverse_bits.py
Created September 7, 2021 15:02
reverse integer bits in binary
import math
class Solution:
def reverseBits(self, bin_int):
'''
:type bin_int: int
:rtype: int
'''
if bin_int <= 1: return bin_int