-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
๐ฌย ๋ฌธ์
https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/
๐ฌย Idea
- ์ฒ์์ ๊ต์ฅํ ๊ฐ๋จํ ๋ฌธ์ ๋ผ ์๊ฐํ์ง๋ง, ํจ์จ์ฑ ํ ์คํธ๋ฅผ ํต๊ณผํ์ง ๋ชปํด ๊ต์ฅํ ์ ๋ฅผ ์ผ๋ค.
- O(N^2)์ ํจ์จ์ฑ์ O(N + M)์ผ๋ก ๋์ด๊ธฐ ์ํด ๊ตฌ๊ฐํฉ์ ์ฌ์ฉํด์ฃผ์๋ค.
- a, c, g, t ๊ฐ๊ฐ์ ๋ฐฐ์ด์ ๋ง๋ค์ด (t๋ ์์ด๋ ๋จ) ์ํ๋ฒณ์ด ๋์จ ๊ตฌ๊ฐ์๋ ์ด์ ๊ฐ + 1์ ํด์ฃผ์ด ์ ์ฅํด์ค๋ค.
- ์ดํ P๋ฅผ ๋๋ฉด์ (P) ์์ (Q + 1) ๊น์ง์ ์ฐจ๊ฐ 0 ์ด์์ผ ๋ a,c,g,t ์ค ์กฐ๊ฑด์ ํด๋นํ๋ ๊ฐ์ ans์ appendํ๋ค.
- Q + 1 ๊น์ง์ธ์ด์ : ์ด์ ์ธ๋ฑ์ค์ ๋น๊ตํ์ฌ ํด๋น ๊ตฌ๊ฐ์์ ์ํ๋ฒณ์ด ๋ฑ์ฅํ๋์ง๋ฅผ ํ์ ํ๊ธฐ ์ํด ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ +1 ํด์ฃผ์๊ธฐ ๋๋ฌธ์ด๋ค.
๐ฌย ํ์ด
public func solution(_ S : inout String, _ P : inout [Int], _ Q : inout [Int]) -> [Int] {
var a = Array(repeating: 0, count: S.count + 1)
var c = Array(repeating: 0, count: S.count + 1)
var g = Array(repeating: 0, count: S.count + 1)
var t = Array(repeating: 0, count: S.count + 1)
var ans: [Int] = []
for (idx, i) in S.enumerated() {
a[idx + 1] = a[idx]
c[idx + 1] = c[idx]
g[idx + 1] = g[idx]
t[idx + 1] = t[idx]
switch i {
case "A":
a[idx + 1] += 1
case "C":
c[idx + 1] += 1
case "G":
g[idx + 1] += 1
default:
t[idx + 1] += 1
}
}
for i in 0..<P.count {
if a[Q[i] + 1] - a[P[i]] > 0 {
ans.append(1)
} else if c[Q[i] + 1] - c[P[i]] > 0 {
ans.append(2)
} else if g[Q[i] + 1] - g[P[i]] > 0 {
ans.append(3)
} else {
ans.append(4)
}
}
return ans
}
์์์๊ฐ
: 1์๊ฐ +
์๊ฐ ๋ณต์ก๋
: O(N + M)
ํ๊ฐํ
: https://app.codility.com/demo/results/training7V4PMC-ED3/