Skip to content

Commit be2e252

Browse files
committed
2020-06-29
1 parent e8c42e2 commit be2e252

File tree

8 files changed

+180
-0
lines changed

8 files changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution(object):
2+
def average(self, salary):
3+
"""
4+
:type salary: List[int]
5+
:rtype: float
6+
"""
7+
return (sum(salary) - max(salary) - min(salary)) * 1.0 / (len(salary) - 2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def kthFactor(self, n, k):
3+
"""
4+
:type n: int
5+
:type k: int
6+
:rtype: int
7+
"""
8+
l = []
9+
for i in range(1, int(n ** 0.5) + 1):
10+
if n % i == 0:
11+
l.append(i)
12+
total = len(l) * 2 if l[-1] ** 2 != n else len(l) * 2 - 1
13+
if total < k:
14+
return -1
15+
16+
if k - 1 < len(l):
17+
return l[k - 1]
18+
else:
19+
idx = (len(l) - 1) * 2 - k
20+
return n // l[idx] if total % 2 else n // l[idx + 2]
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def longestSubarray(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if sum(nums) == len(nums):
8+
return len(nums) - 1
9+
zeroCnt = 0
10+
left = 0
11+
res = 0
12+
for right in range(len(nums)):
13+
if nums[right] == 0:
14+
zeroCnt += 1
15+
while zeroCnt > 1:
16+
if nums[left] == 0:
17+
zeroCnt -= 1
18+
left += 1
19+
res = max(res, right - left + 1 - zeroCnt)
20+
return res
21+
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution(object):
2+
def minNumberOfSemesters(self, n, dependencies, k):
3+
"""
4+
:type n: int
5+
:type dependencies: List[List[int]]
6+
:type k: int
7+
:rtype: int
8+
"""
9+
from collections import defaultdict
10+
inDegree = defaultdict(int)
11+
outDegree = defaultdict(int)
12+
children = defaultdict(set)
13+
14+
for src, dec in dependencies: # 建图
15+
inDegree[dec] += 1
16+
outDegree[src] += 1
17+
children[src].add(dec)
18+
19+
queue = []
20+
for i in range(1, n + 1):
21+
if inDegree[i] == 0: # 入度为0(没有先修课了)的课入队
22+
heappush(queue, (-outDegree[i], i, -1)) # 出度越大(以这门课作为先修课的课越多),优先级越高
23+
24+
semesterCnt = 0
25+
while queue:
26+
semesterCnt += 1
27+
nextSemesterCourses = [] # 存放这个学期不能上的课
28+
courseCnt = 0
29+
while courseCnt < k and queue: # 每个学期最多上 k 门课
30+
priority, node, preFinishedSemester = heappop(queue)
31+
32+
if preFinishedSemester >= semesterCnt: # 当前学期不能上这门课
33+
nextSemesterCourses.append((priority, node, preFinishedSemester))
34+
continue
35+
36+
for child in children[node]: # 这门课可以学,学它,然后处理孩子课的入度
37+
inDegree[child] -= 1
38+
39+
if inDegree[child] == 0: # 孩子课的先修课全上完了
40+
heappush(queue, (-outDegree[child], child, semesterCnt))
41+
courseCnt += 1
42+
43+
for item in nextSemesterCourses: # 把之前存起来的本学期不能上的课再重新入队
44+
heappush(queue, item)
45+
46+
return semesterCnt
47+
48+
49+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def isPathCrossing(self, path):
3+
"""
4+
:type path: str
5+
:rtype: bool
6+
"""
7+
s = set([(0, 0)])
8+
x, y = 0, 0
9+
for d in path:
10+
if d == "N":
11+
y += 1
12+
elif d == "S":
13+
y -= 1
14+
elif d == "E":
15+
x += 1
16+
elif d == "W":
17+
x -= 1
18+
if (x, y) in s:
19+
return True
20+
s.add((x, y))
21+
return False
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def canArrange(self, arr, k):
3+
"""
4+
:type arr: List[int]
5+
:type k: int
6+
:rtype: bool
7+
"""
8+
from collections import defaultdict
9+
dic = defaultdict(int)
10+
for num in arr:
11+
mod = num % k
12+
if mod:
13+
dic[mod] += 1
14+
for key in dic.keys():
15+
if key * 2 != k and dic[key] != dic[k - key]:
16+
return False
17+
if key * 2 == k and dic[key] % 2 != 0:
18+
return False
19+
return True
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def numSubseq(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: int
7+
"""
8+
nums.sort()
9+
left, right = 0, len(nums) - 1
10+
res = 0
11+
MOD = 10 ** 9 + 7
12+
while left <= right:
13+
s = nums[left] + nums[right]
14+
l = right - left
15+
if s <= target:
16+
res = (res + (1 << l)) % MOD
17+
left += 1
18+
else:
19+
right -= 1
20+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def findMaxValueOfEquation(self, points, k):
3+
"""
4+
:type points: List[List[int]]
5+
:type k: int
6+
:rtype: int
7+
"""
8+
from collections import deque
9+
queue = deque([(points[0][0], points[0][0] - points[0][1])])
10+
res = float("-inf")
11+
for yi, yj in points[1:]:
12+
while queue and queue[0][0] < yi - k:
13+
queue.popleft()
14+
if queue:
15+
res = max(res, -queue[0][1] + yi + yj)
16+
sub = yi - yj
17+
while queue and queue[-1][1] > sub:
18+
queue.pop()
19+
queue.append((yi, sub))
20+
return res

0 commit comments

Comments
 (0)