我最近发现了谦卑,我正在进行演示培训。我写了这个解决方案的基因组范围查询问题,它工作良好,解决方案提供了动态规划,但它只得到87%,而不是100%的预期。
有人知道吗?
在这里,您可以找到问题,它在前缀部分。只需启动一个测试来查看问题描述!谦逊训练
谢谢!
def solution(S, P, Q):
# write your code in Python 2.6
S = list(S)
sol = [[0]*len(S),[0]*len(S),[0]*len(S),[0]*len(S)]
mapping = {"A":1, "C":2, "G":3, "T":4}
for i in range(0,len(S)):
if S[i] == 'A':
sol[0][i]+= 1
elif S[i] == 'C':
sol[1][i] += 1
elif S[i] == 'G':
sol[2][i] += 1
elif S[i] == 'T':
sol[3][i] += 1
if i < len(S)-1:
sol[0][i+1] = sol[0][i]
sol[1][i+1] = sol[1][i]
sol[2][i+1] = sol[2][i]
sol[3][i+1] = sol[3][i]
for n in range(0, len(P)):
l = P[n]
r = Q[n]
pre_sum = [0,0,0,0]
if l > 0:
pre_sum = [sol[0][l],sol[1][l],sol[2][l],sol[3][l]]
post_sum = [sol[0][r],sol[1][r],sol[2][r],sol[3][r]]
if post_sum[0]-pre_sum[0] > 0:
P[n] = 1
elif post_sum[1]-pre_sum[1] > 0:
P[n] = 2
elif post_sum[2]-pre_sum[2] > 0:
P[n] = 3
elif post_sum[3]-pre_sum[3] > 0:
P[n] = 4
else:
P[n] = mapping[S[P[n]]];
return P
pass发布于 2014-04-10 21:47:05
啊,我也在做同样的事情,我花了很长的~时间去调试,但最终还是得到了100/100。
例如,当S='AGT'和P=[1]、Q=[2]时,函数应该返回G的3,但是您的(和我的)返回4表示T
我想这能解决这个问题:
`if l > 0: pre_sum = [sol[0][l-1],sol[1][l-1],sol[2][l-1],sol[3][l-1]]`发布于 2019-10-20 18:36:30
这也是100/100的工作
def solution(S, P, Q):
res = []
for i in range(len(P)):
if 'A' in S[P[i]:Q[i]+1]:
res.append(1)
elif 'C' in S[P[i]:Q[i]+1]:
res.append(2)
elif 'G' in S[P[i]:Q[i]+1]:
res.append(3)
else:
res.append(4)
return res发布于 2020-11-20 08:57:33
得分100/100 O(N+M)算法,没有任何技巧与语言实现的in或contains运算符:
Lets define prefix as:
* last index of particular nucleone before on in current position. If no prev occcurance put -1.
*
*
* indexes: 0 1 2 3 4 5 6
* factors: 2 1 3 2 2 4 1
* C A G C C T A
*
* prefix : A -1 1 1 1 1 1 6
* C 0 0 0 3 4 4 4
* G -1 -1 2 2 2 2 2
* T -1 -1 -1 -1 -1 5 5
*
* Having such defined prefix let us easily calculate answer question of minimal factor in following way:
* subsequence S[p]S[p+1]...S[q-1]S[q] has the lowest factor:
* 1 if prefix index [A][q] >= p
* 2 if prefix index [C][q] >= p
* 3 if prefix index [G][q] >= p
* 4 if prefix index [T][q] >= p我的落实这一想法
https://stackoverflow.com/questions/22836576
复制相似问题