leetcode

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

208. Implement Trie (Prefix Tree)

Description: Solution: We make a TrieNode() class. In TrieNode(), we have two attribute: children, endOfWord In insert(), if c is not in cur.children, we put the c in it. And mark the last character as endOfWord. In search(), if c is not in cur.children, we return False. In startsWith(), it is same with search function. But, we use prefix in this case.

LeetCode ๐Ÿ”๏ธ/Tree

235. Lowest Common Ancestor of a Binary Search Tree

Description: Solution: Start from root. If both p, q is less than root, we are gonna see the left portion of root. elif both p, q is more than root, we are gonna see the right portion of root. Other cases have a root as a LCA node in the Tree. Time Complextiy: O(logn) Space Complexity: O(1)

LeetCode ๐Ÿ”๏ธ/Tree

230. Kth Smallest Element in a BST

Description: Solution: First, we keep going left in tree. We put the values in stack. Pop the value from the stack. => From most smallest value. Everytime we pop, increase n to check if there is kth smallest value. If it doensn't exist, now we can go right at current node to see the next.

LeetCode ๐Ÿ”๏ธ/Tree

297. Serialize and Deserialize Binary Tree

Description: Solution: serialize() if node is None --> append('n') if node exist --> append(str(node.val)) We should search from left! ','.join(res) => ['a', 'b', 'c'] --> "a,b,c" deserialize() data.split(",") => "a,b,c" --> ['a', 'b', 'c'] self.i is the global variable 'n' means null, so return null. Don't forgetself.i += 1 We are gonna append TreeNode object. '(val)' is the string form, so we ..

LeetCode ๐Ÿ”๏ธ/Tree

105. Construct Binary Tree from Preorder and Inorder Traversal

Description: Solution: In the recursion, we set the root and mid, and then divide two portion: right & left. root is always the first value of preorder[] mid is always the index of root of inorder[] left portion: preorder[1: mid+1] and inorder[:mid]. right portion: preorder[mid+1:] and inorder[mid+1:].

LeetCode ๐Ÿ”๏ธ/Array

11. Container With Most Water

Description: Solution: Brute force method is not adapted as a solution. We are gonna make the pointer end of both array. Shift l if height[l] is smaller than height[r]. Else: shift r to right.

LeetCode ๐Ÿ”๏ธ/Array

15. 3Sum

Description: Solution: We need to nums.sort() before moving pointer. i is a count, a is the value (enumerate) if i > 0 and a == nums[i-1] => if i is bigger than 0, we make sure not using same value l, r pointers to find the two sum for 0 of three sum. Since we sort the values, if threeSum is bigger than 0, we should move r pointer to left. elif threeSum is smaller than 0, we should move l pointe..

LeetCode ๐Ÿ”๏ธ/Array

33. Search in Rotated Sorted Array

Description: Solution: We are gonna set l, r, m pointers to find in O(logn). If l pointer value is less than m pointer value: target should be between l and m or between m and r. if target is more than m value or target is less than l value, we are gonna see right portion, except left portion. else we are gonna see left portion, except right portion. If r value is less than m value: target can b..

LeetCode ๐Ÿ”๏ธ/Array

153. Find Minimum in Rotated Sorted Array

Description: Solution: I set the right and left pointers to the end of the array. m is the pivot(first set the middle of the array). If m is more than l, then we can throw out the left portion. Now, the new l is the next value of m. Else, we can throw out the right portion, so the new r is the previous value of m. When we get the nums[l] is less than nums[r], we return the res.

LeetCode ๐Ÿ”๏ธ/Array

152. Maximum Product Subarray

Description: Solution: tmp will be used for curMin. Example: [-1, -2, -3] nums[i]= -2: tmp = 2, curMax = 2, curMin = -2 -> res = 2 nums[i] = -3: tmp = -6, curMax = 6, curMin = -6 -> res = 6 โ€ป Think about all negative integers array case!

KB0129
'leetcode' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก (4 Page)