Skip to content

Instantly share code, notes, and snippets.

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if not prices:
return 0
s = 0
for i in range(1, len(prices)):
import re
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
t = ''
s = s.lower()
for c in s:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def swapPairs(self, head: 'ListNode') -> 'ListNode':
if not head or not head.next:
return head
class Solution:
def merge(self, nums1: 'List[int]', m: 'int', nums2: 'List[int]', n: 'int') -> 'None':
"""
Do not return anything, modify nums1 in-place instead.
"""
r = len(nums1) - 1
i = m - 1
j = len(nums2) - 1
while i >= 0 and j >= 0:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: 'ListNode') -> 'ListNode':
cur = head
while cur and cur.next:
class Solution:
def groupAnagrams(self, strs: 'List[str]') -> 'List[List[str]]':
from collections import Counter
d = {}
for s in strs:
c = ''.join(sorted(s))
if c in d:
d[c].append(s)
else:
d[c] = [s]
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# 2 3 6 8 10 12 14 20 22 28 30
class Solution(object):
def to_bst(self, nums):
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrderBottom(self, root: 'TreeNode') -> 'List[List[int]]':
if not root:
@amraks
amraks / TopK.py
Created February 11, 2019 21:26
Top K frequent elements
# Leetcode https://leetcode.com/problems/top-k-frequent-elements/
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
from collections import Counter
d = Counter()
@amraks
amraks / OverlappingMeetings.java
Created June 10, 2018 07:48
Merge overlapping meetings
package code;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Meeting {
public int start;