Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
nhudinhtuan / atoi.py
Created March 29, 2020 03:59
String to Integer (atoi)
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
def in_range(num):
if num > 2 ** 31 - 1: return 2 ** 31 - 1
if num < -2**31: return -2**31
@nhudinhtuan
nhudinhtuan / is_valid_number.py
Created March 29, 2020 08:16
is valid number
class Solution(object):
def isNumber(self, s):
"""
:type s: str
:rtype: bool
"""
#define state transition tables
states = [
# no State (0)
@nhudinhtuan
nhudinhtuan / highest_sum_sliding_window.py
Last active December 28, 2021 02:15
the highest sum of any k consecutive elements in the array
def highestSum(arr, k):
highest_sum = float('-inf')
n = len(arr)
# n must not be smaller than k
if n < k:
return -1
# Compute sum of first window of size k
window_sum = sum([arr[i] for i in range(k)])
@nhudinhtuan
nhudinhtuan / highest_sum_brute_force.py
Last active March 30, 2020 05:51
the highest sum of any k consecutive elements in the array
def highestSum(arr, k):
highest_sum = float('-inf')
n = len(arr)
# n must not be smaller than k
if n < k:
return -1
# subarray starts at i
for i in range(n - k + 1):
@nhudinhtuan
nhudinhtuan / highest_sum_sliding_pointer.py
Last active March 30, 2020 08:39
the highest sum of any k consecutive elements in the array
def highestSum(arr, k):
highest_sum = float('-inf')
n = len(arr)
# n must be not smaller than k
if n < k:
return -1
left = 0
right = 0
@nhudinhtuan
nhudinhtuan / longest_unique_substring_brute_force.py
Created March 30, 2020 06:49
length of longest unique substring
def longest_unique_substring(s):
# function to check if the characters in the substring are all unique or not
def is_all_unique(start_index, end_index):
character_set = set()
for z in range(start_index, end_index):
if s[z] in character_set:
return False
character_set.add(s[z])
return True
@nhudinhtuan
nhudinhtuan / longest_unique_substring_sliding_window.py
Created March 30, 2020 08:55
length of longest unique substring
from collections import defaultdict
def longest_unique_substring(s):
longest_length = float('-inf')
n = len(s)
# empty string has the longest length is 0
if n == 0:
return 0
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
@nhudinhtuan
nhudinhtuan / binary_search_first_position.py
Last active April 1, 2020 10:22
Binary search - find the first position
def find_first_pos(arr, target):
left = 0
right = len(arr) - 1
first_position = -1 # keep track of the latest valid mid position
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
first_position = mid
right = mid - 1 # continue searching to the left
@nhudinhtuan
nhudinhtuan / binary_search_last_position.py
Created April 1, 2020 10:22
Binary search - find the last position
def find_last_pos(arr, target):
left = 0
right = len(arr) - 1
last_position = -1 # keep track of the latest valid mid position
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
last_position = mid
left = mid + 1 # continue searching to the right