SDE101 teaches coding fundamentals to help solve algorithms quickly. In just a few hours, gain skills to strategize solutions and conquer LeetCode problems with confidence.
Question
Do you know python only has 36 keywords? (import keyword; print(keyword.kwlist) will print ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'])
Key Topics
Each programming language (eg. python/java/typescript) has X keywords (eg. if/else/for)
Basic algorithm (eg. bfs/dfs/binary-search)
Clean coding style and IDE (Integrated Development Environment) (vscode/jerbrains)
Self service blocker resolver tools (stackoverflow/chatgpt)
Leetcode SOP (Standard Operating Procedure)
Interface, check input/output interface data contract and edge cases
Algorithm, think about data pattern and common algorithm template
Implementation, coding with helper functions
Review, review index bound, exit case, typo, think about unit test for the solution
defloop_test():s="hello"forcins:print(c)fori,cinenumerate(s():print(i,c)i=0whilei<len(s):print(s[i])i+=1# more exmaplesseen=set()seen.add(val)c1=collections.Counter(s1)fork,vinc1.items()c1[c]=(c1[c]or0)-1"".join(nums)fori,cinenumerate(s1)s1[i:]sorted(names,key=lambdas:len(s))ret.append(cur)ret.append(str(count))WAYS=[(0,1),(1,0),(0,-1),(-1,0)]ROW=len(M)COL=len(M[0])forrinrange(ROW):forcinrange(COL):fordx,dyinWAYS:
funcmain(){// string is a sequence of bytes not of a rune// rune type is an alias of int32s:="hello"fori,rune:=ranges{fmt.Println(i,string(rune),rune)}i:=0fori<len(s){fmt.Println(i,string(s[i]),s[i])i+=1}fori:=0;i<len(s);i+=1{fmt.Println(i,string(s[i]),s[i])}// 0 h 104// 1 e 101// 2 l 108// 3 l 108// 4 o 111}
# python does not have switch keyworddefifelse_test():forninrange(0,101):ifn<60:print("Fail")elifn<90:print("Pass")else:print("Great")a=10print(Trueifa%10==0elseFalse)# True
enumclassState{IDLE,RUNNING,FINISHED}funcases(obj:Any):String{returnwhen(obj){State.IDLE->"It's idle"State.RUNNING->"It's running"State.FINISHED->"It's finished"1->"One""Hello"->"Greeting"isLong->"Long"!isString->"Not a string"else->"Unknown"}}funmain(){println(cases(State.IDLE))println(cases(1L))}// It's idle// Long
"""01155-number-of-dice-rolls-with-target-sumYou have n dice, and each dice has k faces numbered from 1 to k."""classSolution:# An @lru_cache-decorated method represents a DP state.# The method should not update external state variables.# The method should return the result for that state.# The returned value becomes the cached value for that DP state.defnumRollsToTarget(self,n:int,k:int,target:int)->int:MOD=10**9+7@lru_cache(None)defdfs(cur,dice):ifdice<0:return0ifcur>target:return0ifdice==0:iftarget==cur:return1return0ret=0forvalinrange(1,k+1):ret+=dfs(cur+val,dice-1)returnret%MODreturndfs(0,n)classSolution:defnumRollsToTarget(self,d:int,f:int,target:int)->int:MOd=10**9+7dp=collections.defaultdict(int)dp[0]=1foriinrange(d):forjinrange(0,target+1)[::-1]:dp[j]=sum(dp[j-k-1]forkinrange(min(f,j)))%MOdreturndp[target]
Two Pointer
1 2 3 4 5 6 7 8 910111213141516171819
"""00141-linked-list-cycle"""# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = NoneclassSolution:defhasCycle(self,head:ListNode)->bool:slow,fast=head,headwhilefastandfast.next:fast=fast.next.nextslow=slow.nextifslow==fast:returnTruereturnFalse
"""00703-kth-largest-element-in-a-stream"""fromheapqimportheapify,heappop,heappushclassKthLargest:# Maintain a min-heap containing the largest k elements.# heapify() and heappush() modify the heap in-place and return None.# heappop() removes and returns the smallest element.def__init__(self,k:int,nums:list[int]):self.q=numsself.k=kheapify(self.q)self._keep_k_items()def_keep_k_items(self):whilelen(self.q)>self.k:heappop(self.q)defadd(self,val:int)->int:heappush(self.q,val)self._keep_k_items()returnself.q[0]# Your KthLargest object will be instantiated and called as such:# obj = KthLargest(k, nums)# param_1 = obj.add(val)