Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 138_1016_n_n.py
Last active October 16, 2017 20:31
[138. Copy List with Random Pointer] #leetcode
# 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):
@cixuuz
cixuuz / 309_1015.py
Created October 16, 2017 01:46
[309. Best Time to Buy and Sell Stock with Cooldown] #leetcode
class Solution(object):
# O(n) O(1)
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) < 2:
return 0
@cixuuz
cixuuz / 188_1015.py
Last active October 16, 2017 01:43
[188. Best Time to Buy and Sell Stock IV] #leetcode
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:
@cixuuz
cixuuz / 122_1015.py
Created October 15, 2017 22:02
[122. Best Time to Buy and Sell Stock II] #leetcode
class Solution(object):
# O(n) O(1)
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
# result placeholder
max_profit = 0
@cixuuz
cixuuz / 121_1015.py
Created October 15, 2017 21:50
[121. Best Time to Buy and Sell Stock] #leetcode
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
@cixuuz
cixuuz / 98_1015.java
Last active October 15, 2017 19:56
[98. Validate Binary Search Tree] #leetcode
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
@cixuuz
cixuuz / 692_1015.java
Last active October 15, 2017 19:34
[692. Top K Frequent Words] #leetcode
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()
@cixuuz
cixuuz / 283_1014.py
Last active October 15, 2017 17:38
[283. Move Zeroes] #leetcode
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
@cixuuz
cixuuz / 387_1014.py
Last active October 15, 2017 03:44
[387. First Unique Character in a String] #leetcode
# use dictionary
import collections
class Solution(object):
# O(n)
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
@cixuuz
cixuuz / 49_1014.py
Last active October 15, 2017 03:32
[49. Group Anagrams] #leetcode
class Solution(object):
# O(nlgn*k) O(n*k)
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
groups = dict()
res = []