forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
191 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
algorithms/python/1-bitAnd2-bitCharacters/isOneBitCharacter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def isOneBitCharacter(self, bits): | ||
i = 0 | ||
while i < len(bits): | ||
if bits[i] == 0: | ||
i += 1 | ||
if i >= len(bits): return True | ||
if bits[i] == 1: | ||
i += 2 | ||
if i >= len(bits): return False | ||
if i == len(bits) - 1: return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# the same as linked list cycle problem | ||
def findDuplicate(self, nums): | ||
if len(nums) <= 1: return -1 | ||
slow, fast = nums[0], nums[nums[0]] | ||
while slow != fast: | ||
slow = nums[slow] | ||
fast = nums[nums[fast]] | ||
|
||
fast = 0 | ||
while slow != fast: | ||
slow = nums[slow] | ||
fast = nums[fast] | ||
return slow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def imageSmoother(self, M): | ||
from copy import deepcopy | ||
|
||
if not M or not M[0]: return [[]] | ||
row, col = len(M), len(M[0]) | ||
res = deepcopy(M) | ||
for x in range(row): | ||
for y in range(col): | ||
temp = [M[i][j] for i in [x - 1, x, x + 1] for j in [y - 1, y, y + 1] if \ | ||
0 <= i < row and 0 <= j < col] | ||
res[x][y] = sum(temp) // len(temp) | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def findPairs(self, nums, k): | ||
if k > 0: | ||
return len(set(nums) & set(a + k for a in nums)) | ||
elif k == 0: | ||
return sum(v > 1 for v in collections.Counter(nums).values()) | ||
return 0 |
6 changes: 6 additions & 0 deletions
6
algorithms/python/LargestNumberAtLeastTwiceOfOthers/dominantIndex.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def dominantIndex(self, nums): | ||
i = nums.index(max(nums)) | ||
l = nums[i] | ||
del nums[i] | ||
if not nums: return 0 | ||
return i if l >= 2 * max(nums) else -1 |
11 changes: 11 additions & 0 deletions
11
algorithms/python/LongestContinuousIncreasingSubsequence/findLengthOfLCIS.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def findLengthOfLCIS(self, nums): | ||
if not nums: return 0 | ||
res = 1 | ||
temp = 1 | ||
for i in range(1, len(nums)): | ||
if nums[i] > nums[i - 1]: | ||
temp += 1 | ||
else: | ||
res = max(temp, res) | ||
temp = 1 | ||
return max(temp, res) |
16 changes: 16 additions & 0 deletions
16
algorithms/python/MaximizeDistanceToClosestPerson/maxDistToClosest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def maxDistToClosest(self, seats): | ||
first = seats.index(1) | ||
last = 0 | ||
for i in range(len(seats) - 1, -1, -1): | ||
if seats[i]: | ||
last = i | ||
break | ||
res = 0 | ||
temp = 0 | ||
for i in range(first, last + 1): | ||
if seats[i] == 1: | ||
res = max(temp, res) | ||
temp = 0 | ||
else: | ||
temp += 1 | ||
return max(first, len(seats) - last - 1, (res + 1) // 2) |
22 changes: 22 additions & 0 deletions
22
algorithms/python/MaximumAverageSubarrayI/findMaxAverage.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Method 1: sliding window | ||
|
||
def findMaxAverage(self, nums, k): | ||
total = 0 | ||
temp = float('-inf') | ||
for i, n in enumerate(nums): | ||
total += n | ||
if i >= k: total -= nums[i- k] | ||
if i >= k - 1: | ||
temp = max(temp, total) | ||
return temp / k | ||
|
||
|
||
|
||
# Method 2: prefix sum | ||
def findMaxAverage(self, nums, k): | ||
temp = [0] | ||
for n in nums: | ||
temp.append(temp[-1] + n) | ||
|
||
res = max(temp[i + k] - temp[i] for i in range(len(nums) - k + 1)) | ||
return res / k |
11 changes: 11 additions & 0 deletions
11
algorithms/python/MaximumProductOfThreeNumbers/maximumProduct.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# simply find the three largest and two smallest | ||
# Method 1: sort | ||
def maximumProduct(self, nums): | ||
nums.sort() | ||
return max(nums[0] * nums[1] * nums[-1], nums[-1] * nums[-2] * nums[-3]) | ||
|
||
# Method 2: using heapq, O(n) time | ||
def maximumProduct(self, nums): | ||
import heapq | ||
a, b = heapq.nlargest(3, nums), heapq.nsmallest(2, nums) | ||
return max(a[0] * a[1] * a[2], a[0] * b[0] * b[1]) |
5 changes: 5 additions & 0 deletions
5
algorithms/python/MinCostClimbingStairs/minCostClimbingStairs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def minCostClimbingStairs(self, cost): | ||
temp = [0, cost[0]] | ||
for i in range(1, len(cost)): | ||
temp[0], temp[1] = temp[1], min(temp) + cost[i] | ||
return min(temp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def checkPossibility(self, nums): | ||
count = 0 | ||
for i in range(1, len(nums)): | ||
if nums[i] < nums[i - 1]: | ||
count += 1 | ||
if i == 1 or nums[i - 2] <= nums[i]: nums[i - 1] = nums[i] | ||
else: nums[i] = nums[i - 1] | ||
|
||
if count >= 2: return False | ||
return True |
13 changes: 13 additions & 0 deletions
13
algorithms/python/PositionsOfLargeGroups/largeGroupPositions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def largeGroupPositions(self, S): | ||
res = [] | ||
i = 0 | ||
while i < len(S) - 2: | ||
if S[i] == S[i + 1] and S[i] == S[i + 2]: | ||
val = S[i] | ||
index = i | ||
while i < len(S) and S[i] == val: | ||
i += 1 | ||
res.append([index, i - 1]) | ||
i -= 1 | ||
i += 1 | ||
return res |
3 changes: 3 additions & 0 deletions
3
algorithms/python/ShortestUnsortedContinuousSubarray/findUnsortedSubarray.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def findUnsortedSubarray(self, nums): | ||
same = [a == b for a, b in zip(nums, sorted(nums))] | ||
return 0 if all(same) else len(nums) - same.index(False) - same[::-1].index(False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def sortedSquares(self, A): | ||
return list(sorted([a ** 2 for a in A])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def thirdMax(self, nums): | ||
if len(set(nums)) < 3: return max(nums) | ||
first = second = third = float('-inf') | ||
for n in nums: | ||
if n > first: | ||
third = second | ||
second = first | ||
first = n | ||
elif second < n < first: | ||
third = second | ||
second = n | ||
elif third < n < second: | ||
third = n | ||
return third |
20 changes: 20 additions & 0 deletions
20
algorithms/python/ValidMountainArray/validMountainArray.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Method 1: using index find the max first, and then process | ||
|
||
def validMountainArray(self, A): | ||
if len(A) < 3: return False | ||
index = A.index(max(A)) | ||
if index == 0 or index == len(A) -1: return False | ||
for i in range(1, len(A)): | ||
if i <= index: | ||
if A[i] <= A[i - 1]: return False | ||
else: | ||
if A[i] >= A[i - 1]: return False | ||
return True | ||
|
||
|
||
# Method 2: one pass, using two pointers trace from the begining and end | ||
def validMountainArray(self, A): | ||
i, j = 0, len(A) - 1 | ||
while i < len(A) - 1 and A[i] < A[i + 1]: i += 1 | ||
while j > 0 and A[j - 1] > A[j]: j -= 1 | ||
return 0 < i == j < len(A) - 1 |