Skip to content

Instantly share code, notes, and snippets.

@jovianlin
jovianlin / Trie
Created March 20, 2019 09:54
Trie Python
#!/usr/bin/env python
class Node( object ):
def __init__( self, end_node = False ):
self.end_node = end_node
self.prefix_count = 0
self.children = {}
@jovianlin
jovianlin / LeetCode_JumpGameII.py
Created January 22, 2019 07:51
leetcode-jump-game-ii
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums or len(nums) == 1:
return 0
@jovianlin
jovianlin / daily_coding_problem_57.py
Created January 14, 2019 04:03
Daily Coding Problem: Problem #57
"""
Good morning! Here's your coding interview problem for today.
This problem was asked by Amazon.
Given a string s and an integer k, break up the string into multiple texts such that each text has a length of k or less.
You must break it up so that words don't break across lines. If there's no way to break the text up, then return null.
You can assume that there are no spaces at the ends of the string and that there is exactly one space between each word.
@jovianlin
jovianlin / n-queens-ii.py
Created January 4, 2019 07:47
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
class Solution(object):
def totalNQueens(self, n):
"""
:type n: int
:rtype: int
"""
positions = [None]*n
_, count = self.solve(n, 0, positions, count=0)
return count
@jovianlin
jovianlin / MinHeap_and_MaxHeap.py
Created December 21, 2018 10:16
Python3 MinHeap and MaxHeap
class MaxHeapObj:
def __init__(self,val): self.val = val
def __lt__(self,other): return self.val > other.val
def __eq__(self,other): return self.val == other.val
def __str__(self): return str(self.val)
class MinHeap:
def __init__(self): self.h = []
def heappush(self,x): heapq.heappush(self.h,x)
def heappop(self): return heapq.heappop(self.h)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def kthSmallest(self, root, k):
"""
@jovianlin
jovianlin / LeetCode_RemoveNthNodeFromEndList.py
Created December 14, 2018 02:34
Remove Nth Node From End of List (in one pass)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeNthFromEnd(self, head, k):
"""
:type head: ListNode
class Solution:
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
pointerS, pointerT = len(S)-1, len(T)-1
skipS, skipT = 0, 0
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getLength(self, node):
length = 0
while node:
class TrieNode:
def __init__(self):
self.children = {}
self.isWord = False
class WordDictionary:
def __init__(self):
self.root = TrieNode()