LC220 Contains Duplicate III

// time O(NlgK) space O(K) class Solution { public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { TreeSet<Long> set = new TreeSet<>(); for (int i = 0; i < nums.length; i++){ Long ceil = set.ceiling((long)nums[i]-(long)t); if(ceil != null && ceil <= ((long)nums[i]+(long)t)){ return true; } set.add((long)nums[i]); if (set.size() == k+1){... [Read More]

LC122 Best Time to Buy and Sell Stock II

class Solution: def maxProfit(self, prices: List[int]) -> int: total = 0 size = len(prices) l = h = -1 i = 0 while i < size -1: while i < size -1 and prices[i] >= prices[i+1]: i += 1 l = prices[i] while i < size -1 and prices[i] <... [Read More]
Tags: Array Greedy

LC949 Largest Time for Given Digits

class Solution: def largestTimeFromDigits(self, A: List[int]) -> str: #time O(1) since length is 4 space O(1) # main idea: use feature of time ==> hour <24 and minute <60 to filter out invalid time. then find max time by comparing time (converted to minutes) max_minut = -1 res = ""... [Read More]

LC131 Palindrome Partitioning

class Solution: def partition(self, s: str) -> List[List[str]]: # time O(N*2^N) space O(N) for path """ step1, initialize the res, and path step2, dfs, retrive a substring end from 1(be careful the slicing exclusive),loop the whole string step3, check if substring parlindrom, if yes, add substring to path. do recursion... [Read More]
Tags: Backtracking

LC450 Delete Node in a BST

# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: """ main idea: there 3 cases for deletion. (1)both left kid and right child is None (1) either child is... [Read More]
Tags: BST