Skip to content

Instantly share code, notes, and snippets.

View yokolet's full-sized avatar
🏠
Doing Rails dev now

Yoko Harada yokolet

🏠
Doing Rails dev now
View GitHub Profile
@yokolet
yokolet / bst_iterator.py
Created October 2, 2018 19:43
Binary Search Tree Iterator
"""
Description:
Implement an iterator over a binary search tree (BST). Your iterator will be
initialized with the root node of a BST. Calling next() will return the next smallest
number in the BST. The methods, next() and hasNext(), should run in average O(1) time
and uses O(h) memory, where h is the height of the tree.
"""
class TreeNode:
def __init__(self, x):
@yokolet
yokolet / word_dictionary.py
Created October 2, 2018 19:27
Add and Search Word - Data Structure Design
"""
Description:
Design a data structure that supports the following two operations:
- void addWord(word)
- bool search(word)
search(word) can search a literal word or a regular expression string containing only
letters a-z or .. A . means it can represent any one letter. You may assume that all
words are consist of lowercase letters a-z.
Example:
@yokolet
yokolet / nested_iterator.py
Created October 2, 2018 18:31
Flatten Nested List Iterator
"""
Description:
Given a nested list of integers, implement an iterator to flatten it. Each element is
either an integer, or a list -- whose elements may also be integers or other lists.
Use API below to implement the class:
"""
#class NestedInteger:
# def isInteger(self):
# """
@yokolet
yokolet / codec.py
Created October 2, 2018 03:02
Serialize and Deserialize Binary Tree
"""
Description:
Design an algorithm to serialize and deserialize a binary tree. There is no restriction
on how your serialization/deserialization algorithm should work. You just need to
ensure that a binary tree can be serialized to a string and this string can be
deserialized to the original tree structure.
- Do not use class member/global/static variables to store states. Your serialize and
deserialize algorithms should be stateless.
Example:
@yokolet
yokolet / read_four_ii.py
Created September 30, 2018 20:09
Read N Characters Given Read4 II - Call multiple times
"""
Description:
The API: int read4(char *buf) reads 4 characters at a time from a file. The return
value is the actual number of characters read. For example, it returns 3 if there is
only 3 characters left in the file. By using the read4 API, implement the function int
read(char *buf, int n) that reads n characters from the file. The read function may be
called multiple times.
Examples:
Input: "abc" in a file
@yokolet
yokolet / read_four.py
Last active September 30, 2018 20:11
Read N Characters Given Read4
"""
Description:
The API: int read4(char *buf) reads 4 characters at a time from a file. The return
value is the actual number of characters read. For example, it returns 3 if there is
only 3 characters left in the file. By using the read4 API, implement the function int
read(char *buf, int n) that reads n characters from the file. The read function will be
called only once.
Examples:
Input: "abc" in a file, n = 4
@yokolet
yokolet / max_subarray_sum.py
Created September 29, 2018 04:14
Maximum Subarray
"""
Description:
Given an integer array nums, find the contiguous subarray (containing at least one
number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4]
Output: 6 -- [4, -1, 2, 1] => 6
"""
@yokolet
yokolet / climb_stairs.py
Created September 29, 2018 04:03
Climbing Stairs
"""
Description:
You are climbing a stair case. It takes n steps to reach to the top. Each time you can
either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
- Given n will be a positive integer.
Examples:
Input: 2
Output: 2 -- 1 + 1 and 2
@yokolet
yokolet / max_sum_of_three_subarrays.py
Created September 29, 2018 03:53
Maximum Sum of Non-Overlapping Subarrays
"""
Description:
In a given array nums of positive integers, find three non-overlapping subarrays with
maximum sum. Each subarray will be of size k, and we want to maximize the sum of all
3*k entries. Return the result as a list of indices representing the starting position
of each interval (0-indexed). If there are multiple answers, return the
lexicographically smallest one.
- nums length will be between 1 and 20000.
- nums[i] will be between 1 and 65535.
- k will be between 1 and floor(nums.length / 3).