A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor of nucleotides contained in a particular part of the given DNA sequence?
The DNA sequence is given as a non-empty string S = S[0]S[1]...S[N-1] consisting of N characters. There are M queries, which are given in non-empty arrays P and Q, each consisting of M integers. The K-th query (0 ≤ K < M) requires you to find the minimal impact factor of nucleotides contained in the DNA sequence between positions P[K] and Q[K] (inclusive).
For example, consider string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4 P[1] = 5 Q[1] = 5 P[2] = 0 Q[2] = 6The answers to these M = 3 queries are as follows:
- The part of the DNA between positions 2 and 4 contains nucleotides G and C (twice), whose impact factors are 3 and 2 respectively, so the answer is 2.
- The part between positions 5 and 5 contains a single nucleotide T, whose impact factor is 4, so the answer is 4.
- The part between positions 0 and 6 (the whole string) contains all nucleotides, in particular nucleotide A whose impact factor is 1, so the answer is 1.
Write a function:
object Solution { def solution(s: String, p: Array[Int], q: Array[Int]): Array[Int] }
that, given a non-empty string S consisting of N characters and two non-empty arrays P and Q consisting of M integers, returns an array consisting of M integers specifying the consecutive answers to all queries.
Result array should be returned as an array of integers.
For example, given the string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4 P[1] = 5 Q[1] = 5 P[2] = 0 Q[2] = 6the function should return the values [2, 4, 1], as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- M is an integer within the range [1..50,000];
- each element of arrays P and Q is an integer within the range [0..N - 1];
- P[K] ≤ Q[K], where 0 ≤ K < M;
- string S consists only of upper-case English letters A, C, G, T.
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
println(results.length)
results
}
}
3
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
val impacts = S map {
case 'A' => 1
case 'C' => 2
case 'G' => 3
case 'T' => 4
case _ => sys.error("Invalid input")
}
println(impacts)
results
}
}
Vector(2, 1, 3, 2, 2, 4, 1)
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
val impacts = S map {
case 'A' => 1
case 'C' => 2
case 'G' => 3
case 'T' => 4
case _ => sys.error("Invalid input")
}
(P zip Q).zipWithIndex foreach {
case ((p, q), i) =>
results(i) = p
}
results
}
}
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
@scala.annotation.tailrec
def makePrefixCounts(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size()) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
makePrefixCounts(check, index + 1, accum, nextValue)
}
}
def makePrefixCounts(check: Char): Array[Int] =
makePrefixCounts(check, 0, Array.fill(S.size())(0), 0)
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// val T = makePrefixCounts('T')
val hasInRange(a: Array[Int], p: Int, q: Int): Boolean = false
(P zip Q).zipWithIndex foreach {
case ((p, q), i) =>
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
result(i) = minValue
}
results
}
}
Solution.scala:13: error: Int does not take parameters if (index >= S.size()) accum ^ Solution.scala:24: error: Int does not take parameters makePrefixCounts(check, 0, Array.fill(S.size())(0), 0) ^ Solution.scala:31: error: not found: value hasInRange val hasInRange(a: Array[Int], p: Int, q: Int): Boolean = false ^ Solution.scala:36: error: not found: value hasInRange if (hasInRange(A, p, q)) 1 ^ Solution.scala:37: error: not found: value hasInRange else if (hasInRange(C, p, q)) 2 ^ Solution.scala:38: error: not found: value hasInRange else if (hasInRange(G, p, q)) 3 ^ Solution.scala:40: error: not found: value result result(i) = minValue ^ Solution.scala:23: error: method makePrefixCounts is defined twice conflicting symbols both originated in file '/tmp/Solution.scala' def makePrefixCounts(check: Char): Array[Int] = ^ 8 errors found
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
@scala.annotation.tailrec
def makePrefixCounts(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
makePrefixCounts(check, index + 1, accum, nextValue)
}
}
def makePrefixCounts(check: Char): Array[Int] =
makePrefixCounts(check, 0, Array.fill(S.size)(0), 0)
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// val T = makePrefixCounts('T')
val hasInRange(a: Array[Int], p: Int, q: Int): Boolean = false
(P zip Q).zipWithIndex foreach {
case ((p, q), i) =>
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
result(i) = minValue
}
results
}
}
Solution.scala:31: error: not found: value hasInRange val hasInRange(a: Array[Int], p: Int, q: Int): Boolean = false ^ Solution.scala:36: error: not found: value hasInRange if (hasInRange(A, p, q)) 1 ^ Solution.scala:37: error: not found: value hasInRange else if (hasInRange(C, p, q)) 2 ^ Solution.scala:38: error: not found: value hasInRange else if (hasInRange(G, p, q)) 3 ^ Solution.scala:40: error: not found: value result result(i) = minValue ^ Solution.scala:23: error: method makePrefixCounts is defined twice conflicting symbols both originated in file '/tmp/Solution.scala' def makePrefixCounts(check: Char): Array[Int] = ^ 6 errors found
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
@scala.annotation.tailrec
def makePrefixCounts(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
makePrefixCounts(check, index + 1, accum, nextValue)
}
}
def makePrefixCounts(check: Char): Array[Int] =
makePrefixCounts(check, 0, Array.fill(S.size)(0), 0)
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// val T = makePrefixCounts('T')
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = false
(P zip Q).zipWithIndex foreach {
case ((p, q), i) =>
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
result(i) = minValue
}
results
}
}
Solution.scala:40: error: not found: value result result(i) = minValue ^ Solution.scala:23: error: method makePrefixCounts is defined twice conflicting symbols both originated in file '/tmp/Solution.scala' def makePrefixCounts(check: Char): Array[Int] = ^ two errors found
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
def makePrefixCounts(check: Char): Array[Int] = {
@scala.annotation.tailrec
def go(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
makePrefixCounts(check, index + 1, accum, nextValue)
}
}
go(check, 0, Array.fill(S.size)(0), 0)
}
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// val T = makePrefixCounts('T')
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = false
(P zip Q).zipWithIndex foreach {
case ((p, q), i) =>
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
results(i) = minValue
}
results
}
}
Solution.scala:22: error: too many arguments for method makePrefixCounts: (check: Char)Array[Int] makePrefixCounts(check, index + 1, accum, nextValue) ^ one error found
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
def makePrefixCounts(check: Char): Array[Int] = {
@scala.annotation.tailrec
def go(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
go(check, index + 1, accum, nextValue)
}
}
go(check, 0, Array.fill(S.size)(0), 0)
}
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// val T = makePrefixCounts('T')
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = false
(P zip Q).zipWithIndex foreach {
case ((p, q), i) =>
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
results(i) = minValue
}
results
}
}
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
def makePrefixCounts(check: Char): Array[Int] = {
@scala.annotation.tailrec
def go(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
go(check, index + 1, accum, nextValue)
}
}
go(check, 0, Array.fill(S.size)(0), 0)
}
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// Not needed because if it's not the above 3, it must be T.
// val T = makePrefixCounts('T')
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = {
val atP = if (p == 0) 0 else a(p)
val atQ = a(q)
(atQ - atP) > 0
}
(P zip Q).zipWithIndex foreach {
case ((p, q), i) =>
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
results(i) = minValue
}
results
}
}
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
val M = P.length
val results = Array.fill(M)(0)
def makePrefixCounts(check: Char): Array[Int] = {
@scala.annotation.tailrec
def go(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
go(check, index + 1, accum, nextValue)
}
}
go(check, 0, Array.fill(S.size)(0), 0)
}
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// Not needed because if it's not the above 3, it must be T.
// val T = makePrefixCounts('T')
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = {
val atP = if (p == 0) 0 else a(p - 1)
val atQ = a(q)
(atQ - atP) > 0
}
(P zip Q).zipWithIndex foreach {
case ((p, q), i) =>
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
results(i) = minValue
}
results
}
}
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
def makePrefixCounts(check: Char): Array[Int] = {
@scala.annotation.tailrec
def go(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
go(check, index + 1, accum, nextValue)
}
}
go(check, 0, Array.fill(S.size)(0), 0)
}
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// Not needed because if it's not the above 3, it must be T.
// val T = makePrefixCounts('T')
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = {
val atP = if (p == 0) 0 else a(p - 1)
val atQ = a(q)
(atQ - atP) > 0
}
// val results = Array.fill(P.length)(0)
// (P zip Q).zipWithIndex foreach {
// case ((p, q), i) =>
// results(i) = minValue
// }
@scala.annotation.tailrec
def loop(index: Int, result: Array[Int]): Array[Int] = {
if (index >= P.length) result
else {
val p = P(index)
val q = Q(index)
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
result(index) = minValue
loop(index + 1, result)
}
}
}
}
Solution.scala:58: error: type mismatch; found : Unit required: Array[Int] } ^ one error found
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
def makePrefixCounts(check: Char): Array[Int] = {
@scala.annotation.tailrec
def go(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
go(check, index + 1, accum, nextValue)
}
}
go(check, 0, Array.fill(S.size)(0), 0)
}
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// Not needed because if it's not the above 3, it must be T.
// val T = makePrefixCounts('T')
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = {
val atP = if (p == 0) 0 else a(p - 1)
val atQ = a(q)
(atQ - atP) > 0
}
// val results = Array.fill(P.length)(0)
// (P zip Q).zipWithIndex foreach {
// case ((p, q), i) =>
// results(i) = minValue
// }
val K = P.length
@scala.annotation.tailrec
def loop(index: Int, result: Array[Int]): Array[Int] = {
if (index >= K) result
else {
val p = P(index)
val q = Q(index)
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
result(index) = minValue
loop(index + 1, result)
}
}
loop(K, Array.fill(K)(0))
}
}
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
def makePrefixCounts(check: Char): Array[Int] = {
@scala.annotation.tailrec
def go(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
go(check, index + 1, accum, nextValue)
}
}
go(check, 0, Array.fill(S.size)(0), 0)
}
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// Not needed because if it's not the above 3, it must be T.
// val T = makePrefixCounts('T')
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = {
val atP = if (p == 0) 0 else a(p - 1)
val atQ = a(q)
(atQ - atP) > 0
}
// val results = Array.fill(P.length)(0)
// (P zip Q).zipWithIndex foreach {
// case ((p, q), i) =>
// results(i) = minValue
// }
val K = P.length
@scala.annotation.tailrec
def loop(index: Int, result: Array[Int]): Array[Int] = {
if (index >= K) result
else {
val p = P(index)
val q = Q(index)
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
result(index) = minValue
loop(index + 1, result)
}
}
loop(0, Array.fill(K)(0))
}
}
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
def makePrefixCounts(check: Char): Array[Int] = {
@scala.annotation.tailrec
def go(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
go(check, index + 1, accum, nextValue)
}
}
go(check, 0, Array.fill(S.size)(0), 0)
}
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// Don't need counts for `T` because if it's none of the other 3, it must be `T`.
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = {
val atP = if (p == 0) 0 else a(p - 1)
val atQ = a(q)
(atQ - atP) > 0
}
val K = P.length
@scala.annotation.tailrec
def loop(index: Int, result: Array[Int]): Array[Int] = {
if (index >= K) result
else {
val p = P(index)
val q = Q(index)
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
result(index) = minValue
loop(index + 1, result)
}
}
loop(0, Array.fill(K)(0))
}
}
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(S: String, P: Array[Int], Q: Array[Int]): Array[Int] = {
def makePrefixCounts(check: Char): Array[Int] = {
@scala.annotation.tailrec
def go(check: Char, index: Int, accum: Array[Int], last: Int): Array[Int] = {
if (index >= S.size) accum
else {
val nextValue =
if (S.charAt(index) == check) last + 1
else last
accum(index) = nextValue
go(check, index + 1, accum, nextValue)
}
}
go(check, 0, Array.fill(S.size)(0), 0)
}
val A = makePrefixCounts('A')
val C = makePrefixCounts('C')
val G = makePrefixCounts('G')
// Don't need counts for `T` because if it's none of the other 3, it must be `T`.
def hasInRange(a: Array[Int], p: Int, q: Int): Boolean = {
val atP = if (p == 0) 0 else a(p - 1)
val atQ = a(q)
(atQ - atP) > 0
}
val K = P.length
@scala.annotation.tailrec
def loop(index: Int, result: Array[Int]): Array[Int] = {
if (index >= K) result
else {
val p = P(index)
val q = Q(index)
val minValue =
if (hasInRange(A, p, q)) 1
else if (hasInRange(C, p, q)) 2
else if (hasInRange(G, p, q)) 3
else 4
result(index) = minValue
loop(index + 1, result)
}
}
loop(0, Array.fill(K)(0))
}
}
The solution obtained perfect score.