Go Genomic Range Query
func genomicRangeQuery(s string, p, q []int) []int {
result := make([]int, len(p))
for k, pi := range p {
sub := s[pi : q[k]+1]
switch {
case strings.Contains(sub, "A"):
result[k] = 1
case strings.Contains(sub, "C"):
result[k] = 2
case strings.Contains(sub, "G"):
result[k] = 3
default:
result[k] = 4
}
}
return result
}
This builds prefix counts for each DNA letter so every query can return the minimum impact factor quickly.
Go Is Ipv 4 Adress
func isIPv4Address(inputString string) bool {
parts := strings.Split(inputString, ".")
if len(parts) != 4 {
return false
}
for _, part := range parts {
n, err := strconv.Atoi(part)
if err != nil || n > 255 || strconv.Itoa(n) != part {
return false
}
}
return true
}
This splits the string by dots and validates each part as a normal IPv4 octet.
Go Ladder
func ladder(a, b []int) []int {
size := len(a)
result := make([]int, size)
maxB := b[0]
for _, v := range b {
if v > maxB {
maxB = v
}
}
mod := (1 << maxB) - 1
maxA := a[0]
for _, v := range a {
if v > maxA {
maxA = v
}
}
fib := make([]int, maxA+2)
fib[0], fib[1] = 0, 1
for i := 2; i < maxA+2; i++ {
fib[i] = (fib[i-1] + fib[i-2]) & mod
}
for i := 0; i < size; i++ {
result[i] = fib[a[i]+1] & ((1 << b[i]) - 1)
}
return result
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.
Go Largest String
func largestString(s string) string {
b := []byte(s)
length := len(b)
cur := ""
for i := length - 1; i >= 0; i-- {
cur = string(b[i]) + cur
if len(cur) == 3 {
if cur == "abb" {
b[i] = 'b'
b[i+1] = 'a'
b[i+2] = 'a'
if i+4 < length && b[i+4] == 'b' {
i += 4 + 1
} else if i+3 < length && b[i+3] == 'b' {
i += 3 + 1
} else if b[i+2] == 'b' {
i += 2 + 1
}
}
if b[i+1] == 'b' {
i += 1 + 1
} else {
i++
}
cur = ""
}
}
return string(b)
}
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.
Go Max Counters
func maxCounters(n int, a []int) []int {
counters := make([]int, n)
maxCounter := 0
lastUpdate := 0
condition := n + 1
for _, v := range a {
if v <= n {
index := v - 1
if counters[index] < lastUpdate {
counters[index] = lastUpdate // should already be maxed
}
counters[index]++
if counters[index] > maxCounter {
maxCounter = counters[index]
}
}
if v == condition {
lastUpdate = maxCounter
}
}
// apply all max operations to avoid O(M*N) complexity
for k, v := range counters {
if v < lastUpdate {
counters[k] = lastUpdate
}
}
return counters
}
This delays the expensive “set all counters to max” work until it is really needed, which keeps the solution fast.
Go Max Double Slice Sum
func maxDoubleSliceSum(a []int) int {
size := len(a)
if size < 3 {
return 0
}
p1 := make([]int, size)
p2 := make([]int, size)
p1[1] = 0
p2[size-2] = 0
for i := 2; i < size-1; i++ {
if v := p1[i-1] + a[i-1]; v > 0 {
p1[i] = v
}
if v := p2[size-i] + a[size-i]; v > 0 {
p2[size-i-1] = v
}
}
sum := p1[1] + p2[1]
for i := 1; i < size-1; i++ {
if s := p1[i] + p2[i]; s > sum {
sum = s
}
}
return sum
}
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.
Go Max Product Of Three
func maxProductOfThree(a []int) int {
sorted := append([]int(nil), a...)
sort.Ints(sorted)
c := len(sorted)
return max(sorted[c-1]*sorted[c-2]*sorted[c-3], sorted[0]*sorted[1]*sorted[c-1])
}
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.
Go Max Profit
func maxProfit(a []int) int {
price := a[0]
profit := 0
for _, v := range a {
if v < price {
price = v
}
if v-price > profit {
profit = v - price
}
}
return profit
}
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
Go Max Slice Sum
func maxSliceSum(a []int) int {
tmp, best := math.MinInt, math.MinInt
for _, v := range a {
if tmp+v > v {
tmp += v
} else {
tmp = v
}
if tmp > best {
best = tmp
}
}
return best
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
Go Min Avg Two Slice
func minAvgTwoSlice(a []int) int {
idx := 0
min := float64(a[0]+a[1]) / 2
for i := 0; i < len(a)-1; i++ {
cur := float64(a[i]+a[i+1]) / 2
if i+2 < len(a) {
three := float64(a[i]+a[i+1]+a[i+2]) / 3
if three < cur {
cur = three
}
}
if cur < min {
min = cur
idx = i
}
}
return idx
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.