Compare commits

...

1 Commits

Author SHA1 Message Date
xuu
a5ebbe25d0
chore: save hacker rank 2024-06-10 21:26:09 -06:00
4 changed files with 113 additions and 0 deletions

109
hacker02/hacker.go Normal file
View File

@ -0,0 +1,109 @@
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
aoc "go.sour.is/advent-of-code"
)
/*
* Complete the 'cookies' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER k
* 2. INTEGER_ARRAY A
*/
func cookies(k int32, A []int32) int32 {
var i int32
// fmt.Println(" ", A)
// sort.Slice(A, func(i, j int) bool { return A[j] < A[i]})
// for A[len(A)-1] < k {
// if len(A) < 2 {
// return -1
// }
// // fmt.Println(" ", A[len(A)-1], " + 2x", A[len(A)-2], 2*A[len(A)-2] + A[len(A)-1])
// A[len(A)-2] = 2*A[len(A)-2] + A[len(A)-1]
// A = A[:len(A)-1]
// for j:=len(A)-1; j>0 && A[j] > A[j-1]; j-- {
// A[j], A[j-1] = A[j-1], A[j]
// }
// i++
// fmt.Println("A", len(A), i)
// }
pq := aoc.FibHeap(func(a, b *int32) bool { return *a < *b })
for i := range A {
pq.Insert(&A[i])
}
for !pq.IsEmpty() && *pq.GetMin() < k {
first := *pq.ExtractMin()
second := *pq.ExtractMin()
third := first + 2*second
pq.Insert(&third)
i++
}
return i
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16*1024*1024)
stdout, err := os.Create("/dev/stderr")
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16*1024*1024)
firstMultipleInput := strings.Split(strings.TrimSpace(readLine(reader)), " ")
nTemp, err := strconv.ParseInt(firstMultipleInput[0], 10, 64)
checkError(err)
n := int32(nTemp)
kTemp, err := strconv.ParseInt(firstMultipleInput[1], 10, 64)
checkError(err)
k := int32(kTemp)
ATemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
var A []int32
for i := 0; i < int(n); i++ {
AItemTemp, err := strconv.ParseInt(ATemp[i], 10, 64)
checkError(err)
AItem := int32(AItemTemp)
A = append(A, AItem)
}
result := cookies(k, A)
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)
}
}

2
hacker02/input.txt Normal file

File diff suppressed because one or more lines are too long

2
hacker02/other.txt Normal file
View File

@ -0,0 +1,2 @@
8 90
13 47 74 12 89 74 18 38

0
hacker02/output.txt Normal file
View File