On Sun, 6 May 2018 22:00:06 +0200
Valentin Vidić <[email protected]> wrote:
> Can you try converting this Go code to Python to see if it helps with the
> speed?
>
> package main
>
> import (
> "bufio"
> "fmt"
> "os"
> )
>
> func build2(a []int) int {
> s := []int{0}
>
> for i := 0; i < len(a); i++ {
> for j := len(s) - 1; j >= 0; j-- {
> if s[j] <= a[i] * 6 {
> w := s[j] + a[i]
>
> if j == len(s) - 1 {
> s = append(s, w)
> } else if s[j+1] > w {
> s[j+1] = w
> }
> }
> }
> }
>
> return len(s) - 1
> }
>
> func main() {
> in := bufio.NewReader(os.Stdin)
>
> var T int
> fmt.Fscan(in, &T)
>
> for t := 0; t < T; t++ {
> var n int
> fmt.Fscan(in, &n)
>
> a := make([]int, n)
>
> for i := 0; i < n; i++ {
> fmt.Fscan(in, &a[i])
> }
>
> res := build2(a)
>
> fmt.Printf("Case #%d: %d\n", t + 1, res)
> }
> }
Hi!
The irony is that henrikrueping's original solution is roughly twice
faster than your Go code when it is run by PyPy instead of CPython.
$ time go run ant-stack-vvidic2.go < C-large.in > /dev/null
real 0m1.006s
user 0m1.030s
sys 0m0.040s
$ time pypy ant-stack-henrikrueping.py < C-large.in > /dev/null
real 0m0.556s
user 0m0.510s
sys 0m0.020s
$ time python ant-stack-henrikrueping.py < C-large.in > /dev/null
real 0m31.194s
user 0m31.160s
sys 0m0.030s
--
Best regards,
Siarhei Siamashka aka 'ssvb'
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/20180507201749.76a1d4aa%40i7.
For more options, visit https://groups.google.com/d/optout.