LC824 Goat Latin

class Solution: # time O(N+2K^2) space(N) def toGoatLatin(self, S: str) -> str: if not S: return [] sen = S.split(' ') # O(N) vo = {'a','e','i','o','u'} res = list() for idx, word in enumerate(sen): if word[0].lower() not in vo: # edge case: Capical vowel word = word[1:] + word[0] #... [Read More]
Tags: Sort Greedy

LC451 Sort Characters By Frequency

ituition solution class Solution: # time O(lgN) space O(N) # main idea: Hash Table + Sort def frequencySort(self, s: str) -> str: d = dict() for i in range(len(s)): if s[i] not in d: d[s[i]] = 1 else: d[s[i]] += 1 tups = sorted(d.items(), key=lambda k : k[1], reverse= True... [Read More]
Tags: HashTable Heap

LC1103 Distribute Candies to People

intuition solution class Solution: # time (N/M * M) space O(1) if not consider output space def distributeCandies(self, candies: int, num_people: int) -> List[int]: res = [0] * num_people ptr = 0 round = 0 gives = 0 cnt = 0#round * num_people + index # termination: candies - cnt... [Read More]
Tags: Math

LC56 Merge Intervals

class Solution: def merge(self, intervals: List[List[int]]) -> List[List[int]]: """ tc O(NlgN) sc O(1) main idea: find overlapping pair ==> previous end is bigger than current start time 1. sort by start 2. for each time pair, check if overlap with stack top 3. if overlap, update stack top, otherwise push... [Read More]
Tags: Array Sort Greedy

LC435 Non-overlapping Intervals

class Solution: def eraseOverlapIntervals(self, A: List[List[int]]) -> int: """ tc O(NlgN) sc O(1) greedy,get earliest end time as possible to spare more space to get more potential non-overlaps 1. sort by end 2. loop intervals, check if current start >= prev end time. if yes, update previous valid end time... [Read More]
Tags: Greedy