Skip to content

Commit 1d4370b

Browse files
committed
2019-09-01
1 parent f685733 commit 1d4370b

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def numPrimeArrangements(self, n):
3+
"""
4+
:type n: int
5+
:rtype: int
6+
"""
7+
# 2 3 5 6 + 6
8+
prime_cnt = 0
9+
for i in range(1, n + 1):
10+
if self.prime(i):
11+
prime_cnt += 1
12+
non_prime_cnt = n - prime_cnt
13+
# print prime_cnt, non_prime_cnt
14+
return (math.factorial(prime_cnt) * math.factorial(non_prime_cnt)) % (10 ** 9 + 7)
15+
16+
def prime(self, n):
17+
if n == 1:
18+
return False
19+
if n == 2 or n == 3 or n == 5:
20+
return True
21+
for i in range(2, int(n ** 0.5) + 1):
22+
if n % i == 0:
23+
return False
24+
return True
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def dietPlanPerformance(self, calories, k, lower, upper):
3+
"""
4+
:type calories: List[int]
5+
:type k: int
6+
:type lower: int
7+
:type upper: int
8+
:rtype: int
9+
"""
10+
res = 0
11+
s = sum(calories[: k])
12+
for start in range(0, len(calories) - (k - 1)):
13+
if start != 0:
14+
# print start,
15+
s += calories[start + k - 1] - calories[start - 1]
16+
# print s
17+
if s < lower:
18+
res -= 1
19+
elif s > upper:
20+
res += 1
21+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution(object):
2+
def canMakePaliQueries(self, s, queries):
3+
"""
4+
:type s: str
5+
:type queries: List[List[int]]
6+
:rtype: List[bool]
7+
"""
8+
alphabet = set(s)
9+
dic = dict()
10+
for char in alphabet:
11+
dic[char] = [0 for _ in range(len(s))]
12+
13+
for i, ch in enumerate(s):
14+
for char in alphabet:
15+
if ch == char:
16+
if i > 0:
17+
dic[ch][i] = dic[ch][i - 1] + 1
18+
else:
19+
dic[ch][i] = 1
20+
else:
21+
if i > 0:
22+
dic[char][i] = dic[char][i - 1]
23+
res = []
24+
for left, right, k in queries:
25+
odd_cnt = 0
26+
for char in alphabet:
27+
# print char, dic[char], left, right
28+
if left > 0:
29+
if (dic[char][right] - dic[char][left - 1]) % 2 == 1:
30+
odd_cnt += 1
31+
else:
32+
if dic[char][right] % 2 == 1:
33+
odd_cnt += 1
34+
if odd_cnt // 2 <= k:
35+
res.append(True)
36+
else:
37+
res.append(False)
38+
return res
39+

5176.猜字谜/5176-猜字谜.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def findNumOfValidWords(self, words, puzzles):
3+
"""
4+
:type words: List[str]
5+
:type puzzles: List[str]
6+
:rtype: List[int]
7+
"""
8+
from collections import Counter
9+
for i in range(len(words)):
10+
words[i] = "".join(sorted(set(words[i])))
11+
record = Counter(words) #统计每种单词出现的频率
12+
13+
res = []
14+
for p in puzzles:
15+
bfs = [p[0]]
16+
for char in p[1:]:
17+
bfs += [s + char for s in bfs]
18+
cnt = 0
19+
for combination in bfs:
20+
tmp = "".join(sorted(combination))
21+
if tmp in record:
22+
cnt += record[tmp]
23+
res.append(cnt)
24+
return res
25+
26+
27+

0 commit comments

Comments
 (0)