https://www.hackerrank.com/challenges/determining-dna-health/problem
주어진 DNA 종류의 개수와 DNA strands의 길이의 최댓값이 10의 5승까지 가능하므로 시간복잡도 O(NlogN) 안에 최대한 해결해야 함
from math import inf
from collections import defaultdict
from bisect import bisect_left, bisect_right
def getHealth(seq, first, last, longest):
h, ls = 0, len(seq)
for l in range(ls):
for k in range(1, longest+1):
if l+k > ls:
break
sub = seq[l:l+k]
if sub not in sub_d:
# if AA not in subs, AAA also not in subs
break
if sub not in gDict:
# if AA not in subs, AAA can be in gDict
continue
idxs, hs = gDict[sub]
right = hs[bisect_right(idxs, last)] # O(logN)
left = hs[bisect_left(idxs, first)] # O(logN)
h += (right - left)
return h
if __name__ == '__main__':
n = int(input().strip())
genes = input().rstrip().split()
health = list(map(int, input().rstrip().split()))
gDict = defaultdict(lambda : [[], [0]]) # gene_idx, health
sub_d = set() # substring of d
for i, gene in enumerate(genes):
gDict[gene][0].append(i)
# append gene index
for j in range(1, min(len(gene), 50)+1):
sub_d.add(gene[:j])
# add substrings to subs
for v in gDict.values():
for i, idx in enumerate(v[0]):
v[1].append(v[1][i]+health[idx])
# cumulative sum of health (first index = 0)
longest = max(list(map(len, genes))) # longest length of genes
hMin, hMax = inf, 0
s = int(input().strip())
for s_itr in range(s):
first_multiple_input = input().rstrip().split()
first = int(first_multiple_input[0])
last = int(first_multiple_input[1])
d = first_multiple_input[2]
h = getHealth(d, first, last, longest)
hMin, hMax = min(hMin,h), max(hMax,h)
print(hMin, hMax)
for j in range(1, min(len(gene), 50)+1): 로 substring의 길이를 최대 50까지 제한하는 줄이 있다.50이라는 기준을 500 -> 100 -> 50까지 줄여도 문제가 없어서 다른 사람들의 풀이 중 500이었던 숫자를 50까지 줄여서 사용했다.
In the [HackerRank] challenge "Determining DNA Health (Python)," participants analyze DNA strands to calculate their health based on gene sequences and their respective health values. This problem tests one's ability to efficiently process and match subsequences within a larger string, employing data structures and algorithms for optimal performance. Interestingly, while coding, some might encounter terms from unrelated fields, like ozempic face meaning a phrase referring to the facial changes observed in people using the weight loss drug Ozempic.