This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for singly-linked list with a random pointer. | |
# class RandomListNode(object): | |
# def __init__(self, x): | |
# self.label = x | |
# self.next = None | |
# self.random = None | |
# O(n) O(n) | |
class Solution(object): | |
def copyRandomList(self, head): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
# O(n) O(1) | |
def maxProfit(self, prices): | |
""" | |
:type prices: List[int] | |
:rtype: int | |
""" | |
if len(prices) < 2: | |
return 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
# O(n*k) O(n*k) | |
def maxProfit(self, k, prices): | |
""" | |
:type k: int | |
:type prices: List[int] | |
:rtype: int | |
""" | |
# corner case | |
if not prices or k == 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
# O(n) O(1) | |
def maxProfit(self, prices): | |
""" | |
:type prices: List[int] | |
:rtype: int | |
""" | |
# result placeholder | |
max_profit = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def maxProfit(self, prices): | |
""" | |
:type prices: List[int] | |
:rtype: int | |
""" | |
# O(n) O(1) | |
# for each price points, we want to know the lowest price before and | |
# then we can get the max profit | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<String> topKFrequent(String[] words, int k) { | |
// hashmap stores counts of each words | |
HashMap<String, Integer> map = new HashMap<>(); | |
for(String s : words){ | |
map.put(s, map.getOrDefault(s, 0) + 1); | |
} | |
// PQ to get top k elements | |
PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>( | |
(a, b) -> a.getValue() == b.getValue() ? b.getKey().compareTo(a.getKey()): a.getValue() - b.getValue() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
# O(n) O(1) | |
def moveZeroes(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: void Do not return anything, modify nums in-place instead. | |
""" | |
i = 0 | |
j = 0 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use dictionary | |
import collections | |
class Solution(object): | |
# O(n) | |
def firstUniqChar(self, s): | |
""" | |
:type s: str | |
:rtype: int | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
# O(nlgn*k) O(n*k) | |
def groupAnagrams(self, strs): | |
""" | |
:type strs: List[str] | |
:rtype: List[List[str]] | |
""" | |
groups = dict() | |
res = [] | |