There are N stacks of coins, numbered from 0 to N−1. The Kth stack has A[K] coins. In one move we can pick two coins from any stack we choose, put the first coin away and place the second coin on the adjacent stack (either to the left or to the right of the original stack).
What is the maximum number of coins that can be accumulated in a single stack?
Write a function:
func Solution(A []int) int
that, given an array A of N integers, recording the heights of the stacks, returns the maximum number of coins that can be accumulated in one stack after performing any number of moves as described above.
Examples:
1. Given A = [2, 3, 1, 3], the function should return 5. A possible sequence of moves is: [2, 3, 1, 3] → [0, 4, 1, 3] → [0, 4, 2, 1] → [0, 5, 0, 1].
2. Given A = [3, 7, 0, 5], the function should return 9. A possible sequence of moves is: [3, 7, 0, 5] → [1, 8, 0, 5] → [1, 8, 1, 3] → [1, 8, 2, 1] → [1, 9, 0, 1].
3. Given A = [1, 1, 1, 1, 1], the function should return 1. No move can be performed.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..200,000];
- each element of array A is an integer within the range [0..100,000,000].
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func solution(stacks []int) int {
size := len(stacks)
moveRight := make([]int, size, size)
moveLeft := make([]int, size, size)
copy(moveRight, stacks)
copy(moveLeft, stacks)
for i := 1; i < size; i++ {
reverseIndex := size - 1 - i
moveRight[i] += (moveRight[i-1]) / 2
moveLeft[reverseIndex] += (moveLeft[reverseIndex+1]) / 2
}
highestStack := 0
for i := 0; i < size; i++ {
stack := stacks[i]
if i > 0 {
stack += moveRight[i-1] / 2
}
if i < size-1 {
stack += moveLeft[i+1] / 2
}
if stack > highestStack {
highestStack = stack
}
}
return highestStack
}
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func solution(stacks []int) int {
size := len(stacks)
moveRight := make([]int, size, size)
moveLeft := make([]int, size, size)
copy(moveRight, stacks)
copy(moveLeft, stacks)
for i := 1; i < size; i++ {
reverseIndex := size - 1 - i
moveRight[i] += (moveRight[i-1]) / 2
moveLeft[reverseIndex] += (moveLeft[reverseIndex+1]) / 2
}
highestStack := 0
for i := 0; i < size; i++ {
stack := stacks[i]
if i > 0 {
stack += moveRight[i-1] / 2
}
if i < size-1 {
stack += moveLeft[i+1] / 2
}
if stack > highestStack {
highestStack = stack
}
}
return highestStack
}
# main workspace/src/main/exec.go:6: imported and not used: "solution" workspace/src/main/exec.go:157: undefined: Solution
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func Solution(stacks []int) int {
size := len(stacks)
moveRight := make([]int, size, size)
moveLeft := make([]int, size, size)
copy(moveRight, stacks)
copy(moveLeft, stacks)
for i := 1; i < size; i++ {
reverseIndex := size - 1 - i
moveRight[i] += (moveRight[i-1]) / 2
moveLeft[reverseIndex] += (moveLeft[reverseIndex+1]) / 2
}
highestStack := 0
for i := 0; i < size; i++ {
stack := stacks[i]
if i > 0 {
stack += moveRight[i-1] / 2
}
if i < size-1 {
stack += moveLeft[i+1] / 2
}
if stack > highestStack {
highestStack = stack
}
}
return highestStack
}
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func Solution(stacks []int) int {
size := len(stacks)
moveRight := make([]int, size, size)
moveLeft := make([]int, size, size)
copy(moveRight, stacks)
copy(moveLeft, stacks)
for i := 1; i < size; i++ {
reverseIndex := size - 1 - i
moveRight[i] += (moveRight[i-1]) / 2
moveLeft[reverseIndex] += (moveLeft[reverseIndex+1]) / 2
}
highestStack := 0
for i := 0; i < size; i++ {
stack := stacks[i]
if i > 0 {
stack += moveRight[i-1] / 2
}
if i < size-1 {
stack += moveLeft[i+1] / 2
}
if stack > highestStack {
highestStack = stack
}
}
return highestStack
}
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func Solution(stacks []int) int {
size := len(stacks)
moveRight := make([]int, size, size)
moveLeft := make([]int, size, size)
copy(moveRight, stacks)
copy(moveLeft, stacks)
for i := 1; i < size; i++ {
reverseIndex := size - 1 - i
moveRight[i] += (moveRight[i-1]) / 2
moveLeft[reverseIndex] += (moveLeft[reverseIndex+1]) / 2
}
highestStack := 0
for i := 0; i < size; i++ {
stack := stacks[i]
if i > 0 {
stack += moveRight[i-1] / 2
}
if i < size-1 {
stack += moveLeft[i+1] / 2
}
if stack > highestStack {
highestStack = stack
}
}
return highestStack
}
The solution obtained perfect score.