Skip to content

Commit

Permalink
Leetcode Weekly Contest 195
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizmann committed Sep 2, 2020
1 parent 102a4e7 commit c243677
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
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
16 changes: 16 additions & 0 deletions Leetcode/Algorithm/python/Max Value of Equation.py
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

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

20 changes: 20 additions & 0 deletions Leetcode/Algorithm/python/Path Crossing.py
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

0 comments on commit c243677

Please sign in to comment.