post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2846874/Python-oror-DP%2BMemoization-oror-Explained
class Solution: def beautifulPartitions(self, s: str, k: int, min_len: int) -> int: n=len(s) mod=10**9+7 prime=set(['2','3','5','7']) if s[0] not in prime or s[-1] in prime: return 0 arr=[] last,i=0,1 while i<n: if (i==n-1) or (s[i] not in prime and s[i+1] in prime): arr.append(i-last+1) last=i+1 i+=2 else: i+=1 pre_sum={-1:0} m=len(arr) next_idx=[-1]*m for i,val in enumerate(arr): pre_sum[i]=pre_sum[i-1]+val for i in range(m): for j in range(i+1,m+1): if pre_sum[j-1]-pre_sum[i-1]>=min_len: next_idx[i]=j-1 break @cache def dp(idx,length,started): if idx==m: return 1 if (length==k and not started) else 0 ans=0 if started: #end here ans+=dp(idx+1,length+1,0) #don't end here ans+=dp(idx+1,length,1) else: #start from here if next_idx[idx]!=-1: ans+=dp(next_idx[idx],length,1) return ans%mod return dp(0,0,0)
number-of-beautiful-partitions
Python || DP+Memoization || Explained
ketan_raut
0
5
number of beautiful partitions
2,478
0.294
Hard
34,000
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2835340/My-Python3-solution
class Solution: def beautifulPartitions(self, s: str, k: int, minLength: int) -> int: n, primes, mod = len(s), set('2357'), (10 ** 9) + 7 @cache def dp(i, at_start, k): if i == n: return int(k == 0) if i > n or k == 0 or s[i] not in primes and at_start: return 0 if s[i] in primes: if at_start: return dp(i + minLength - 1, False, k) else: return dp(i + 1, False, k) return (dp(i + 1, True, k - 1) + dp(i + 1, False, k)) % mod return dp(0, True, k)
number-of-beautiful-partitions
My Python3 solution
Chiki1601
0
7
number of beautiful partitions
2,478
0.294
Hard
34,001
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2834324/Python3-bottom-up-dp
class Solution: def beautifulPartitions(self, s: str, k: int, minLength: int) -> int: prime = "2357" dp = [[0]*(len(s)+1) for _ in range(k)] if s[0] in prime and s[-1] not in prime: for j in range(len(s)+1): dp[0][j] = 1 for i in range(1, k): for j in range(len(s)-1, -1, -1): dp[i][j] = dp[i][j+1] if minLength <= j <= len(s)-minLength and s[j-1] not in prime and s[j] in prime: dp[i][j] = (dp[i][j] + dp[i-1][j+minLength]) % 1_000_000_007 return dp[-1][0]
number-of-beautiful-partitions
[Python3] bottom-up dp
ye15
0
14
number of beautiful partitions
2,478
0.294
Hard
34,002
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2832507/Over-optimized-O(nk)-dp-solution-~400-ms
class Solution: def beautifulPartitions(self, s: str, k: int, minLength: int) -> int: mod = 10 ** 9 + 7 n = len(s) prime = {'2', '3', '5', '7'} if s[0] in prime and s[-1] not in prime: op = [] for i in range(1, n - 1): if s[i] not in prime and s[i + 1] in prime: op.append(i + 1) op.append(n) m = len(op) dp = [[] for _ in range(m)] pos = 0 j = 0 while j < m and op[j] - pos < minLength: j += 1 if j < m: dp[j] = [0, 1] for i, pos in enumerate(op): if i: length = len(dp[i - 1]) dp[i].extend([0] * (length - len(dp[i]))) for index in range(1, length): dp[i][index] += dp[i - 1][index] dp[i][index] %= mod while j < m and op[j] - pos < minLength: j += 1 if j < m: length = min(k + 1, len(dp[i]) + 1) dp[j].extend([0] * (length - len(dp[j]))) for index in range(1, length): dp[j][index] += dp[i][index - 1] dp[j][index] %= mod return dp[-1][-1] if len(dp[-1]) == (k + 1) else 0 else: return 0
number-of-beautiful-partitions
Over-optimized O(nk) dp solution, ~400 ms
chuan-chih
0
21
number of beautiful partitions
2,478
0.294
Hard
34,003
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2832418/python3-DP-%2B-accumulative-sum-O(n*k)
class Solution: def beautifulPartitions(self, s: str, k: int, minLength: int) -> int: primes = {'2','3','5','7'} # prunning if len(s) < k*minLength or s[0] not in primes or s[-1] in primes: return 0 dd_int = functools.partial(defaultdict, int) dp = defaultdict(dd_int) dp[-1][0] = 1 stack = [] MOD = 10**9+7 accSum = [0] * (k+1) j = 0 for i in range(len(s)): if s[i] in primes: if s[i-1] not in primes: # prunning stack.append(i) continue while j < len(stack): if i-stack[j]+1 < minLength: break for u in range(k+1): accSum[u] = (accSum[u] + dp[stack[j]-1][u]) % MOD j += 1 for u in range(1, k+1): dp[i][u] = accSum[u-1] return dp[len(s)-1][k]
number-of-beautiful-partitions
[python3] DP + accumulative sum O(n*k)
hieuvpm
0
18
number of beautiful partitions
2,478
0.294
Hard
34,004
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2832064/O(nk)-time-complexity-O(n)-space-complexity-DP
class Solution: def beautifulPartitions(self, s: str, k: int, minLength: int) -> int: n = len(s) mod = 10 ** 9 + 7 if k * minLength > n: return 0 dp = [[0] * (2) for _ in range(n+1)] dp[0][0] = 1 prime_indices = [i + 1 for i in range(n) if s[i] in '2357'] m = len(prime_indices) for j in range(1, k + 1): prefix_sum = 0 left = 0 for i in range(n + 1): if i < j * minLength: dp[i][j%2] = 0 continue if s[i - 1] in '2357': dp[i][j%2] = 0 continue while left < m and i - prime_indices[left] + 1 >= minLength: prefix_sum += dp[prime_indices[left] - 1][(j - 1)%2] left += 1 dp[i][j%2] = prefix_sum dp[i][j%2] %= mod return dp[-1][k%2] % mod
number-of-beautiful-partitions
O(nk) time complexity, O(n) space complexity DP
zhanghaotian19
0
20
number of beautiful partitions
2,478
0.294
Hard
34,005
https://leetcode.com/problems/number-of-beautiful-partitions/discuss/2831967/Fastest-Python-solution-so-far
class Solution: def beautifulPartitions(self, s: str, k: int, minLength: int) -> int: s=list(s) l0=len(s) prime={'2', '3', '5', '7'} for i in range(l0): s[i]=s[i] in prime if not s[0]: return 0 if s[-1]: return 0 p=10**9+7 stops=[0] for i in range(l0-1): if (not s[i]) and s[i+1]: stops.append(i+1) l1=len(stops) def bp(i, k): if l0-stops[i]<k*minLength: return 0 if k==1: return 1 i1=i+1 while i1<l1 and stops[i1]<stops[i]+minLength: i1+=1 return sm[i1]%p for j in range(1, k+1): table=[bp(i, j) for i in range(l1)] sm=[] tmp=0 for t in table: sm.append(tmp) tmp+=t sm.append(tmp) for i in range(l1+1): sm[i]=tmp-sm[i] return table[0]
number-of-beautiful-partitions
Fastest Python solution so far
mbeceanu
0
40
number of beautiful partitions
2,478
0.294
Hard
34,006