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 / plus_one.py
Created October 18, 2018 19:29
Plus One
"""
Description:
Given a non-empty array of digits representing a non-negative integer, plus one to the
integer. The digits are stored such that the most significant digit is at the head of
the list, and each element in the array contain a single digit. You may assume the
integer does not contain any leading zero, except the number 0 itself.
Examples:
Input: [1,2,3]
Output: [1,2,4]
@yokolet
yokolet / spiral_order.py
Created October 18, 2018 19:11
Spiral Matrix
"""
Description:
Given a matrix of m x n elements (m rows, n columns), return all elements of the
matrix in spiral order.
Examples:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
@yokolet
yokolet / alien_order.py
Created October 18, 2018 02:28
Alien Dictionary
"""
Description:
There is a new alien language which uses the latin alphabet. However, the order among
letters are unknown to you. You receive a list of non-empty words from the dictionary,
where words are sorted lexicographically by the rules of this new language. Derive the
order of letters in this language.
- You may assume all letters are in lowercase.
- You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
- If the order is invalid, return an empty string.
- There may be multiple valid order of letters, return any one of them is fine.
@yokolet
yokolet / num_islands_ii.py
Created October 17, 2018 21:29
Number of Islands II
"""
Description:
A 2d grid map of m rows and n columns is initially filled with water. We may perform
an addLand operation which turns the water at position (row, col) into a land. Given
a list of positions to operate, count the number of islands after each addLand
operation. An island is surrounded by water and is formed by connecting adjacent lands
horizontally or vertically. You may assume all four edges of the grid are all
surrounded by water.
Example:
@yokolet
yokolet / single_number.py
Created October 17, 2018 18:59
Single Number
"""
Description:
Given a non-empty array of integers, every element appears twice except for one. Find
that single one. The algorithm should have a linear runtime complexity.
Examples:
Input: [2,2,1]
Output: 1
Input: [4,1,2,1,2]
@yokolet
yokolet / palindrome_pairs.py
Created October 17, 2018 18:49
Palindrome Pairs
"""
Description:
Given a list of unique words, find all pairs of distinct indices (i, j) in the given
list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.
Examples:
Input: ["abcd","dcba","lls","s","sssll"]
Output: [[0,1],[1,0],[3,2],[2,4]]
parlindromes are: ["dcbaabcd","abcddcba","slls","llssssll"]
@yokolet
yokolet / min_window.py
Created October 4, 2018 20:55
Minimum Window Substring
"""
Description:
Given a string S and a string T, find the minimum window in S which will contain all
the characters in T in complexity O(n).
- If there is no such window in S that covers all characters in T, return the empty string "".
- If there is such window, you are guaranteed that there will always be only one unique
minimum window in S.
Examples:
Input: S = "ADOBECODEBANC", T = "ABC"
@yokolet
yokolet / number_to_words.py
Created October 4, 2018 20:34
Integer to English Words
"""
Description:
Convert a non-negative integer to its english words representation. Given input is
guaranteed to be less than 2**31 - 1.
Examples:
Input: 123
Output: "One Hundred Twenty Three"
Input: 12345
@yokolet
yokolet / least_interval.py
Created October 4, 2018 20:10
Task Scheduler
"""
Given a char array representing tasks CPU need to do. It contains capital letters A to
Z where different letters represent different tasks.Tasks could be done without
original order. Each task could be done in one interval. For each interval, CPU could
finish one task or just be idle. However, there is a non-negative cooling interval n
that means between two same tasks, there must be at least n intervals that CPU are
doing different tasks or just be idle.
You need to return the least number of intervals the CPU will take to finish all the
given tasks.
- The number of tasks is in the range [1, 10000].
@yokolet
yokolet / celebrity_finder.py
Created October 4, 2018 04:36
Find the Celebrity
"""
Description:
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them,
there may exist one celebrity. The definition of a celebrity is that all the other
n - 1 people know him/her but he/she does not know any of them.
Now you want to find out who the celebrity is or verify that there is not one. The only
thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get
information of whether A knows B. You need to find out the celebrity (or verify there
is not one) by asking as few questions as possible (in the asymptotic sense).