Skip to content

Instantly share code, notes, and snippets.

@freeeve
Created November 10, 2015 05:57
Show Gist options
  • Select an option

  • Save freeeve/b9d1046cf425fe7f27ff to your computer and use it in GitHub Desktop.

Select an option

Save freeeve/b9d1046cf425fe7f27ff to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
br := bufio.NewReader(os.Stdin)
line, _ := br.ReadString('\n')
line = strings.TrimSpace(line)
n, err := strconv.Atoi(line)
if err != nil {
panic(err)
}
lastCandy := 1
lastStudent := -1
candyCount := 0
downwardRun := 0
for i := 0; i < n; i++ {
line, _ = br.ReadString('\n')
line = strings.TrimSpace(line)
student, err := strconv.Atoi(line)
if err != nil {
panic(err)
}
slope := 0
if lastStudent != -1 {
if lastStudent > student {
slope = -1
} else if lastStudent < student {
slope = 1
}
}
if slope == -1 {
downwardRun++
candyCount += downwardRun
lastCandy = 1
} else if slope == 1 {
if downwardRun > 0 {
candyCount += downwardRun + 1
downwardRun = 0
} else {
lastCandy++
}
candyCount += lastCandy
} else {
if downwardRun > 0 {
candyCount += downwardRun + 1
downwardRun = 0
}
lastCandy = 1
candyCount += lastCandy
}
lastStudent = student
}
fmt.Println(candyCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment