Skip to content

Commit

Permalink
remove sensitive question description
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Oct 12, 2018
1 parent ff17f4e commit fe2430f
Show file tree
Hide file tree
Showing 858 changed files with 825 additions and 18,126 deletions.
38 changes: 1 addition & 37 deletions Python/01-matrix.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
# Time: O(m * n)
# Space: O(m * n)

# Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
# The distance between two adjacent cells is 1.
#
# Example 1:
#
# Input:
# 0 0 0
# 0 1 0
# 0 0 0
#
# Output:
# 0 0 0
# 0 1 0
# 0 0 0
#
# Example 2:
#
# Input:
# 0 0 0
# 0 1 0
# 1 1 1
#
# Output:
# 0 0 0
# 0 1 0
# 1 2 1
#
# Note:
# The number of elements of the given matrix will not exceed 10,000.
# There are at least one 0 in the given matrix.
# The cells are adjacent in only four directions: up, down, left and right.

import collections

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


class Solution(object):
def updateMatrix(self, matrix):
"""
Expand Down Expand Up @@ -68,10 +35,6 @@ def updateMatrix(self, matrix):

return matrix


# Time: O(m * n)
# Space: O(m * n)
# dp solution
class Solution2(object):
def updateMatrix(self, matrix):
"""
Expand All @@ -98,3 +61,4 @@ def updateMatrix(self, matrix):
if j < len(matrix[i])-1:
dp[i][j] = min(dp[i][j], dp[i][j+1]+1)
return dp

30 changes: 1 addition & 29 deletions Python/1-bit-and-2-bit-characters.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
# Time: O(n)
# Space: O(1)

# We have two special characters. The first character can be represented
# by one bit 0.
# The second character can be represented by two bits (10 or 11).
#
# Now given a string represented by several bits.
# Return whether the last character must
# be a one-bit character or not. The given string will always end with a zero.
#
# Example 1:
# Input:
# bits = [1, 0, 0]
# Output: True
# Explanation:
# The only way to decode it is two-bit character and one-bit character.
# So the last character is one-bit character.
#
# Example 2:
# Input:
# bits = [1, 1, 1, 0]
# Output: False
# Explanation:
# The only way to decode it is two-bit character and two-bit character.
# So the last character is NOT one-bit character.
# Note:
#
# 1 <= len(bits) <= 1000.
# bits[i] is always 0 or 1.

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


class Solution(object):
def isOneBitCharacter(self, bits):
"""
Expand All @@ -47,3 +18,4 @@ def isOneBitCharacter(self, bits):
break
parity ^= bits[i]
return parity == 0

28 changes: 1 addition & 27 deletions Python/132-pattern.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
# Time: O(n)
# Space: O(n)

# Given a sequence of n integers a1, a2, ..., an,
# a 132 pattern is a subsequence ai, aj, ak such that i < j < k and
# ai < ak < aj. Design an algorithm that takes a list of n numbers as
# input and checks whether there is a 132 pattern in the list.
#
# Note: n will be less than 15,000.
#
# Example 1:
# Input: [1, 2, 3, 4]
#
# Output: False
#
# Explanation: There is no 132 pattern in the sequence.
# Example 2:
# Input: [3, 1, 4, 2]
#
# Output: True
#
# Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
# Example 3:
# Input: [-1, 3, 2, 0]
#
# Output: True
#
# Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


class Solution(object):
def find132pattern(self, nums):
"""
Expand All @@ -49,3 +22,4 @@ def find132pattern(self, nums):
ak = st.pop()
st.append(nums[i])
return False

24 changes: 1 addition & 23 deletions Python/2-keys-keyboard.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
# Time: O(sqrt(n))
# Space: O(1)

# Initially on a notepad only one character 'A' is present.
# You can perform two operations on this notepad for each step:
#
# Copy All: You can copy all the characters present on the notepad
# (partial copy is not allowed).
# Paste: You can paste the characters which are copied last time.
# Given a number n.
# You have to get exactly n 'A' on the notepad by performing the minimum
# number of steps permitted.
# Output the minimum number of steps to get n 'A'.
#
# Example 1:
# Input: 3
# Output: 3
# Explanation:
# Intitally, we have one character 'A'.
# In step 1, we use Copy All operation.
# In step 2, we use Paste operation to get 'AA'.
# In step 3, we use Paste operation to get 'AAA'.
# Note:
# The n will be in the range [1, 1000].


class Solution(object):
def minSteps(self, n):
"""
Expand All @@ -41,3 +18,4 @@ def minSteps(self, n):
if n > 1:
result += n
return result

25 changes: 1 addition & 24 deletions Python/24-game.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
# Time: O(n^3 * 4^n) = O(1), n = 4
# Space: O(n^2) = O(1)

# You have 4 cards each containing a number from 1 to 9.
# You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.
#
# Example 1:
# Input: [4, 1, 8, 7]
# Output: True
# Explanation: (8-4) * (7-1) = 24
# Example 2:
# Input: [1, 2, 1, 2]
# Output: False
# Note:
# The division operator / represents real division, not integer division.
# For example, 4 / (1 - 2/3) = 12.
# Every operation done is between two numbers. In particular,
# we cannot use - as a unary operator.
# For example, with [1, 1, 1, 1] as input,
# the expression -1 - 1 - 1 - 1 is not allowed.
# You cannot concatenate numbers together. For example,
# if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.

from operator import add, sub, mul, truediv
from fractions import Fraction

Expand All @@ -29,7 +9,6 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def judgePoint24(self, nums):
"""
Expand All @@ -54,9 +33,6 @@ def judgePoint24(self, nums):
next_nums.pop()
return False


# Time: O(n^3 * 4^n) = O(1), n = 4
# Space: O(n^2) = O(1)
class Solution2(object):
def judgePoint24(self, nums):
"""
Expand Down Expand Up @@ -84,3 +60,4 @@ def dfs(nums):
return False

return dfs(map(Fraction, nums))

12 changes: 1 addition & 11 deletions Python/3sum-closest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
# Time: O(n^2)
# Space: O(1)

# Given an array S of n integers,
# find three integers in S such that the sum is closest to a given number,
# target.
# Return the sum of the three integers.
# You may assume that each input would have exactly one solution.
#
# For example, given array S = {-1 2 1 -4}, and target = 1.
#
# The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).


class Solution(object):
def threeSumClosest(self, nums, target):
"""
Expand All @@ -36,3 +25,4 @@ def threeSumClosest(self, nums, target):
return target
i += 1
return result

2 changes: 1 addition & 1 deletion Python/3sum-smaller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Time: O(n^2)
# Space: O(1)


class Solution:
# @param {integer[]} nums
# @param {integer} target
Expand All @@ -22,3 +21,4 @@ def threeSumSmaller(self, nums, target):
k += 1

return count

15 changes: 1 addition & 14 deletions Python/3sum.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
# Time: O(n^2)
# Space: O(1)

# Given an array S of n integers,
# are there elements a, b, c in S such that a + b + c = 0?
# Find all unique triplets in the array which gives the sum of zero.
#
# Note:
# Elements in a triplet (a,b,c) must be in non-descending order. (ie, a <= b <= c)
# The solution set must not contain duplicate triplets.
# For example, given array S = {-1 0 1 2 -1 -4},
#
# A solution set is:
# (-1, 0, 1)
# (-1, -1, 2)

import collections


class Solution(object):
def threeSum(self, nums):
"""
Expand Down Expand Up @@ -62,3 +48,4 @@ def threeSum2(self, nums):
if sorted([j, y, 0 - j - y]) not in rtn:
rtn.append(sorted([j, y, 0 - j - y]))
return rtn

5 changes: 1 addition & 4 deletions Python/4-keys-keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
except NameError:
xrange = range # Python 3


class Solution(object):
def maxA(self, N):
"""
Expand All @@ -26,9 +25,6 @@ def maxA(self, N):
n4 = n - n3
return 3**n3 * 4**n4


# Time: O(n)
# Space: O(1)
class Solution2(object):
def maxA(self, N):
"""
Expand All @@ -41,3 +37,4 @@ def maxA(self, N):
for i in xrange(7, N+1):
dp[i % 6] = max(dp[(i-4) % 6]*3, dp[(i-5) % 6]*4)
return dp[N % 6]

27 changes: 1 addition & 26 deletions Python/4sum-ii.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
# Time: O(n^2)
# Space: O(n^2)

# Given four lists A, B, C, D of integer values,
# compute how many tuples (i, j, k, l) there are
# such that A[i] + B[j] + C[k] + D[l] is zero.
#
# To make problem a bit easier, all A, B, C, D have same length of N
# where 0 <= N <= 500.
# All integers are in the range of -228 to 228 - 1 and the result
# is guaranteed to be at most 231 - 1.
#
# Example:
#
# Input:
# A = [ 1, 2]
# B = [-2,-1]
# C = [-1, 2]
# D = [ 0, 2]
#
# Output:
# 2
#
# Explanation:
# The two tuples are:
# 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
# 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

import collections


class Solution(object):
def fourSumCount(self, A, B, C, D):
"""
Expand All @@ -40,3 +14,4 @@ def fourSumCount(self, A, B, C, D):
"""
A_B_sum = collections.Counter(a+b for a in A for b in B)
return sum(A_B_sum[-c-d] for c in C for d in D)

Loading

0 comments on commit fe2430f

Please sign in to comment.