Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
nhudinhtuan / inorder_traversal_iterating.py
Last active April 2, 2020 07:49
Tree traversal - Inorder iterative implementation
def inorder_traversal_iterating(root):
result = []
# using stack
stack = []
current = root
while stack or current:
if current:
# traverse the left subtree
stack.append(current)
current = current.left
@nhudinhtuan
nhudinhtuan / tree_traversal_inorder.py
Last active April 3, 2020 02:41
Tree traversal - Inorder
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# recursive version
def inorder_traversal_recursive(root):
result = []
@nhudinhtuan
nhudinhtuan / find_right_border.py
Last active April 2, 2020 14:19
Binary search - find the right border
def find_right_border(arr, target):
left = 0
right = len(arr) - 1
right_border = -1 # keep track of the latest larger number
while left <= right:
mid = left + (right - left) // 2
if arr[mid] <= target:
left = mid + 1
else:
@nhudinhtuan
nhudinhtuan / find_left_border.py
Last active June 9, 2021 16:40
Binary search - find the left border
def find_left_border(arr, target):
left = 0
right = len(arr) - 1
left_border = -1 # keep track of the latest smaller number
while left <= right:
mid = left + (right - left) // 2
if arr[mid] < target:
left_border = mid
left = mid + 1
@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
@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
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 / 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
@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 / 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