Skip to content

Commit fe2430f

Browse files
committed
remove sensitive question description
1 parent ff17f4e commit fe2430f

File tree

858 files changed

+825
-18126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

858 files changed

+825
-18126
lines changed

Python/01-matrix.py

+1-37
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,13 @@
11
# Time: O(m * n)
22
# Space: O(m * n)
33

4-
# Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
5-
# The distance between two adjacent cells is 1.
6-
#
7-
# Example 1:
8-
#
9-
# Input:
10-
# 0 0 0
11-
# 0 1 0
12-
# 0 0 0
13-
#
14-
# Output:
15-
# 0 0 0
16-
# 0 1 0
17-
# 0 0 0
18-
#
19-
# Example 2:
20-
#
21-
# Input:
22-
# 0 0 0
23-
# 0 1 0
24-
# 1 1 1
25-
#
26-
# Output:
27-
# 0 0 0
28-
# 0 1 0
29-
# 1 2 1
30-
#
31-
# Note:
32-
# The number of elements of the given matrix will not exceed 10,000.
33-
# There are at least one 0 in the given matrix.
34-
# The cells are adjacent in only four directions: up, down, left and right.
35-
364
import collections
375

386
try:
397
xrange # Python 2
408
except NameError:
419
xrange = range # Python 3
4210

43-
4411
class Solution(object):
4512
def updateMatrix(self, matrix):
4613
"""
@@ -68,10 +35,6 @@ def updateMatrix(self, matrix):
6835

6936
return matrix
7037

71-
72-
# Time: O(m * n)
73-
# Space: O(m * n)
74-
# dp solution
7538
class Solution2(object):
7639
def updateMatrix(self, matrix):
7740
"""
@@ -98,3 +61,4 @@ def updateMatrix(self, matrix):
9861
if j < len(matrix[i])-1:
9962
dp[i][j] = min(dp[i][j], dp[i][j+1]+1)
10063
return dp
64+

Python/1-bit-and-2-bit-characters.py

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,11 @@
11
# Time: O(n)
22
# Space: O(1)
33

4-
# We have two special characters. The first character can be represented
5-
# by one bit 0.
6-
# The second character can be represented by two bits (10 or 11).
7-
#
8-
# Now given a string represented by several bits.
9-
# Return whether the last character must
10-
# be a one-bit character or not. The given string will always end with a zero.
11-
#
12-
# Example 1:
13-
# Input:
14-
# bits = [1, 0, 0]
15-
# Output: True
16-
# Explanation:
17-
# The only way to decode it is two-bit character and one-bit character.
18-
# So the last character is one-bit character.
19-
#
20-
# Example 2:
21-
# Input:
22-
# bits = [1, 1, 1, 0]
23-
# Output: False
24-
# Explanation:
25-
# The only way to decode it is two-bit character and two-bit character.
26-
# So the last character is NOT one-bit character.
27-
# Note:
28-
#
29-
# 1 <= len(bits) <= 1000.
30-
# bits[i] is always 0 or 1.
31-
324
try:
335
xrange # Python 2
346
except NameError:
357
xrange = range # Python 3
368

37-
389
class Solution(object):
3910
def isOneBitCharacter(self, bits):
4011
"""
@@ -47,3 +18,4 @@ def isOneBitCharacter(self, bits):
4718
break
4819
parity ^= bits[i]
4920
return parity == 0
21+

Python/132-pattern.py

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,11 @@
11
# Time: O(n)
22
# Space: O(n)
33

4-
# Given a sequence of n integers a1, a2, ..., an,
5-
# a 132 pattern is a subsequence ai, aj, ak such that i < j < k and
6-
# ai < ak < aj. Design an algorithm that takes a list of n numbers as
7-
# input and checks whether there is a 132 pattern in the list.
8-
#
9-
# Note: n will be less than 15,000.
10-
#
11-
# Example 1:
12-
# Input: [1, 2, 3, 4]
13-
#
14-
# Output: False
15-
#
16-
# Explanation: There is no 132 pattern in the sequence.
17-
# Example 2:
18-
# Input: [3, 1, 4, 2]
19-
#
20-
# Output: True
21-
#
22-
# Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
23-
# Example 3:
24-
# Input: [-1, 3, 2, 0]
25-
#
26-
# Output: True
27-
#
28-
# Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
29-
304
try:
315
xrange # Python 2
326
except NameError:
337
xrange = range # Python 3
348

35-
369
class Solution(object):
3710
def find132pattern(self, nums):
3811
"""
@@ -49,3 +22,4 @@ def find132pattern(self, nums):
4922
ak = st.pop()
5023
st.append(nums[i])
5124
return False
25+

Python/2-keys-keyboard.py

+1-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
11
# Time: O(sqrt(n))
22
# Space: O(1)
33

4-
# Initially on a notepad only one character 'A' is present.
5-
# You can perform two operations on this notepad for each step:
6-
#
7-
# Copy All: You can copy all the characters present on the notepad
8-
# (partial copy is not allowed).
9-
# Paste: You can paste the characters which are copied last time.
10-
# Given a number n.
11-
# You have to get exactly n 'A' on the notepad by performing the minimum
12-
# number of steps permitted.
13-
# Output the minimum number of steps to get n 'A'.
14-
#
15-
# Example 1:
16-
# Input: 3
17-
# Output: 3
18-
# Explanation:
19-
# Intitally, we have one character 'A'.
20-
# In step 1, we use Copy All operation.
21-
# In step 2, we use Paste operation to get 'AA'.
22-
# In step 3, we use Paste operation to get 'AAA'.
23-
# Note:
24-
# The n will be in the range [1, 1000].
25-
26-
274
class Solution(object):
285
def minSteps(self, n):
296
"""
@@ -41,3 +18,4 @@ def minSteps(self, n):
4118
if n > 1:
4219
result += n
4320
return result
21+

Python/24-game.py

+1-24
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
# Time: O(n^3 * 4^n) = O(1), n = 4
22
# Space: O(n^2) = O(1)
33

4-
# You have 4 cards each containing a number from 1 to 9.
5-
# You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.
6-
#
7-
# Example 1:
8-
# Input: [4, 1, 8, 7]
9-
# Output: True
10-
# Explanation: (8-4) * (7-1) = 24
11-
# Example 2:
12-
# Input: [1, 2, 1, 2]
13-
# Output: False
14-
# Note:
15-
# The division operator / represents real division, not integer division.
16-
# For example, 4 / (1 - 2/3) = 12.
17-
# Every operation done is between two numbers. In particular,
18-
# we cannot use - as a unary operator.
19-
# For example, with [1, 1, 1, 1] as input,
20-
# the expression -1 - 1 - 1 - 1 is not allowed.
21-
# You cannot concatenate numbers together. For example,
22-
# if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.
23-
244
from operator import add, sub, mul, truediv
255
from fractions import Fraction
266

@@ -29,7 +9,6 @@
299
except NameError:
3010
xrange = range # Python 3
3111

32-
3312
class Solution(object):
3413
def judgePoint24(self, nums):
3514
"""
@@ -54,9 +33,6 @@ def judgePoint24(self, nums):
5433
next_nums.pop()
5534
return False
5635

57-
58-
# Time: O(n^3 * 4^n) = O(1), n = 4
59-
# Space: O(n^2) = O(1)
6036
class Solution2(object):
6137
def judgePoint24(self, nums):
6238
"""
@@ -84,3 +60,4 @@ def dfs(nums):
8460
return False
8561

8662
return dfs(map(Fraction, nums))
63+

Python/3sum-closest.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
# Time: O(n^2)
22
# Space: O(1)
33

4-
# Given an array S of n integers,
5-
# find three integers in S such that the sum is closest to a given number,
6-
# target.
7-
# Return the sum of the three integers.
8-
# You may assume that each input would have exactly one solution.
9-
#
10-
# For example, given array S = {-1 2 1 -4}, and target = 1.
11-
#
12-
# The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
13-
14-
154
class Solution(object):
165
def threeSumClosest(self, nums, target):
176
"""
@@ -36,3 +25,4 @@ def threeSumClosest(self, nums, target):
3625
return target
3726
i += 1
3827
return result
28+

Python/3sum-smaller.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Time: O(n^2)
22
# Space: O(1)
33

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

2423
return count
24+

Python/3sum.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
# Time: O(n^2)
22
# Space: O(1)
33

4-
# Given an array S of n integers,
5-
# are there elements a, b, c in S such that a + b + c = 0?
6-
# Find all unique triplets in the array which gives the sum of zero.
7-
#
8-
# Note:
9-
# Elements in a triplet (a,b,c) must be in non-descending order. (ie, a <= b <= c)
10-
# The solution set must not contain duplicate triplets.
11-
# For example, given array S = {-1 0 1 2 -1 -4},
12-
#
13-
# A solution set is:
14-
# (-1, 0, 1)
15-
# (-1, -1, 2)
16-
174
import collections
185

19-
206
class Solution(object):
217
def threeSum(self, nums):
228
"""
@@ -62,3 +48,4 @@ def threeSum2(self, nums):
6248
if sorted([j, y, 0 - j - y]) not in rtn:
6349
rtn.append(sorted([j, y, 0 - j - y]))
6450
return rtn
51+

Python/4-keys-keyboard.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
except NameError:
77
xrange = range # Python 3
88

9-
109
class Solution(object):
1110
def maxA(self, N):
1211
"""
@@ -26,9 +25,6 @@ def maxA(self, N):
2625
n4 = n - n3
2726
return 3**n3 * 4**n4
2827

29-
30-
# Time: O(n)
31-
# Space: O(1)
3228
class Solution2(object):
3329
def maxA(self, N):
3430
"""
@@ -41,3 +37,4 @@ def maxA(self, N):
4137
for i in xrange(7, N+1):
4238
dp[i % 6] = max(dp[(i-4) % 6]*3, dp[(i-5) % 6]*4)
4339
return dp[N % 6]
40+

Python/4sum-ii.py

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
11
# Time: O(n^2)
22
# Space: O(n^2)
33

4-
# Given four lists A, B, C, D of integer values,
5-
# compute how many tuples (i, j, k, l) there are
6-
# such that A[i] + B[j] + C[k] + D[l] is zero.
7-
#
8-
# To make problem a bit easier, all A, B, C, D have same length of N
9-
# where 0 <= N <= 500.
10-
# All integers are in the range of -228 to 228 - 1 and the result
11-
# is guaranteed to be at most 231 - 1.
12-
#
13-
# Example:
14-
#
15-
# Input:
16-
# A = [ 1, 2]
17-
# B = [-2,-1]
18-
# C = [-1, 2]
19-
# D = [ 0, 2]
20-
#
21-
# Output:
22-
# 2
23-
#
24-
# Explanation:
25-
# The two tuples are:
26-
# 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
27-
# 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
28-
294
import collections
305

31-
326
class Solution(object):
337
def fourSumCount(self, A, B, C, D):
348
"""
@@ -40,3 +14,4 @@ def fourSumCount(self, A, B, C, D):
4014
"""
4115
A_B_sum = collections.Counter(a+b for a in A for b in B)
4216
return sum(A_B_sum[-c-d] for c in C for d in D)
17+

0 commit comments

Comments
 (0)