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
""" | |
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): |
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
""" | |
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: |
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
""" | |
Input: | |
s1 = "ADC" | |
s2 = "ABCD" | |
Output: 2 | |
Explanation: | |
"ADC" | |
"ABCD" | |
Both strings share the subsequence "A", "D". |
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
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]) |
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
import contextlib | |
import asyncio | |
@contextlib.asynccontextmanager | |
async def manage_sleep(): | |
try: | |
print('going to sleep...') | |
yield asyncio.sleep(5) | |
finally: | |
print("sleep is over!!") |
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
""" | |
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] |
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
""" | |
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) | |
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
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. |
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
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 |
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
import math | |
class Solution: | |
def reverseBits(self, bin_int): | |
''' | |
:type bin_int: int | |
:rtype: int | |
''' | |
if bin_int <= 1: return bin_int |