Skip to content

Instantly share code, notes, and snippets.

@thiagozs
Created August 19, 2022 21:36
Show Gist options
  • Save thiagozs/d60ee5d9f777b3edb454ff679de8f726 to your computer and use it in GitHub Desktop.
Save thiagozs/d60ee5d9f777b3edb454ff679de8f726 to your computer and use it in GitHub Desktop.
HackerRank birthdayCakeCandles
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"sort"
)
/*
* Complete the 'birthdayCakeCandles' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY candles as parameter.
*/
func birthdayCakeCandles(candles []int32) int32 {
// Write your code here
var highestNumbers []int32
highestNumber := int32(0)
sort.Slice(candles, func(i, j int) bool { return candles[i] < candles[j] })
for i := len(candles) - 1; i >= 0; i-- {
if candles[i] >= highestNumber {
highestNumber = candles[i]
highestNumbers = append(highestNumbers, candles[i])
}
}
return int32(len(highestNumbers))
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024)
candlesCount, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
candlesTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
var candles []int32
for i := 0; i < int(candlesCount); i++ {
candlesItemTemp, err := strconv.ParseInt(candlesTemp[i], 10, 64)
checkError(err)
candlesItem := int32(candlesItemTemp)
candles = append(candles, candlesItem)
}
result := birthdayCakeCandles(candles)
fmt.Fprintf(writer, "%d\n", result)
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment