-
Notifications
You must be signed in to change notification settings - Fork 29
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
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
Leetcode/Algorithm/python/Check If Array Pairs Are Divisible by k.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,17 @@ | ||
from collections import defaultdict | ||
|
||
class Solution(object): | ||
def canArrange(self, arr, k): | ||
n = len(arr) | ||
assert n % 2 == 0 | ||
d = defaultdict(list) | ||
for num in arr: | ||
d[num % k].append(num) | ||
if len(d[0]) % 2: | ||
return False | ||
if k % 2 == 0 and len(d[k / 2]) % 2: | ||
return False | ||
for i in xrange(1, (k + 1) / 2): | ||
if len(d[i]) != len(d[k - i]): | ||
return False | ||
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,16 @@ | ||
INF = 10 ** 10 | ||
|
||
class Solution(object): | ||
def findMaxValueOfEquation(self, points, k): | ||
st = [] | ||
res = -INF | ||
for i, (x, y) in enumerate(points): | ||
while st and x - points[st[0]][0] > k: | ||
st.pop(0) | ||
if st: | ||
res = max(res, x + y + points[st[0]][1] - points[st[0]][0]) | ||
while st and points[st[-1]][1] - points[st[-1]][0] <= y - x: | ||
st.pop() | ||
st.append(i) | ||
return res | ||
|
26 changes: 26 additions & 0 deletions
26
Leetcode/Algorithm/python/Number of Subsequences That Satisfy the Given Sum Condition.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,26 @@ | ||
MOD = (10 ** 9) + 7 | ||
|
||
class Solution(object): | ||
def numSubseq(self, nums, target): | ||
n = len(nums) | ||
res = 0 | ||
nums.sort() | ||
p, q = 0, n - 1 | ||
|
||
while q >= 0: | ||
if nums[q] * 2 <= target: | ||
res += 1 | ||
p = min(p, q - 1) | ||
p = max(0, p) | ||
while p < q and nums[p] + nums[q] <= target: | ||
p += 1 | ||
res += self.calc(q) - self.calc(q - p) | ||
res = (res % MOD + MOD) % MOD | ||
q -= 1 | ||
return res | ||
|
||
def calc(self, n): | ||
if n < 0: | ||
return 0 | ||
return (pow(2, n, MOD) - 1 + MOD) % MOD | ||
|
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 @@ | ||
class Solution(object): | ||
def isPathCrossing(self, path): | ||
d = { | ||
'N': (0, -1), | ||
'E': (1, 0), | ||
'S': (0, 1), | ||
'W': (-1, 0) | ||
} | ||
|
||
cur = (0, 0) | ||
visited = set([cur]) | ||
for p in path: | ||
y, x = cur | ||
my, mx = d[p] | ||
ny, nx = y + my, x + mx | ||
cur = (ny, nx) | ||
if cur in visited: | ||
return True | ||
visited.add(cur) | ||
return False |