https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when …
Monthly Archives: January 2016
Leetcode 109: Convert Sorted List to Binary Search Tree
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Code # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, …
Continue reading “Leetcode 109: Convert Sorted List to Binary Search Tree”
Leetcode 106: Construct Binary Tree from Inorder and Postorder Traversal
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Code # 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): …
Continue reading “Leetcode 106: Construct Binary Tree from Inorder and Postorder Traversal”
Leetcode 105: Construct Binary Tree from Preorder and Inorder Traversal
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Code # 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 …
Continue reading “Leetcode 105: Construct Binary Tree from Preorder and Inorder Traversal”
slicing in numpy array and list
For normal list in Python, slicing copies the references without copying underlying contents. (See the fact that`id(a[1])` and `id(b[0])` are identical below.) >>> a = [1,2,3] >>> b = a[1:3] >>> a [1, 2, 3] >>> b [2, 3] >>> id(a[1]) 25231680 >>> id(b[0]) 25231680 >>> b[0] = 999 >>> a [1, 2, 3] >>> …
Leetcode 123: Best Time to Buy and Sell Stock III
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you …
Continue reading “Leetcode 123: Best Time to Buy and Sell Stock III”
Leetcode 122: Best Time to Buy and Sell Stock II
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage …
Continue reading “Leetcode 122: Best Time to Buy and Sell Stock II”
Leetcode 121: Best Time to Buy and Sell Stock
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Code import sys class …
Continue reading “Leetcode 121: Best Time to Buy and Sell Stock”
How to understand Eigenvector/Eigenvalue?
This topic has been in my wish list for a long time. I will try to articulate my understanding of eigenvector and eigenvalue after digestion informative materials from different sources. First of all, we can consider multiplication between a square matrix and a vector as a transformation of such that is rotated and scaled. By …
Continue reading “How to understand Eigenvector/Eigenvalue?”
Leetcode 111: Minimum Depth of Binary Tree
https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Code # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None …
Continue reading “Leetcode 111: Minimum Depth of Binary Tree”