Horse Racing Duals puzzle discussion

Any tricks for dart. I pass everything except timeouts on the many horses.

Maybe the sort function for an array is too slow in dart? or am I missing something.

1 Like

Hello everyone,

I tried this exercise which is very interesting to learn some basicsā€¦
I tried many ways to achieve the last test and yet, i still exceed the execution time.

I tried using arrays, then linked list but still doesnā€™t workā€¦

Anyone can share some ideas/clues to succeed the last test.

Thanks in advance :wink:

On the cover page of the puzzle, there is a hint (I donā€™t mean the data structure, I mean the other one). Does your code implement that?

If your talking about finding the best complexity, iā€™m working on it.
I was reading everything about the problem yesterday after sending my messageā€¦^^
And i found that maybe iā€™m missing this part, so iā€™m working on it.

Thanks :wink:

It wasnā€™t yesterday, but 2 days ago, i had a problem posting the messageā€¦^^

If you are coding in C++ on this puzzle and have issues with the test using a high number of horses, I finally found a trick! My code kept saying that it took too long, even when sorting before comparing par by pair. As it turns out, it was my debugging tests that caused issues! Using the ā€œcerrā€ to display error messages adds a lot of time to the program, so if youā€™re using it try running the program without and it will go much quicker!

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

n = int(input())
low = 0
for i in range(n):
    pi = int(input())
    st = []
    st.append(pi)
    print(st)
    for j in range(n):
        print(j)
        temp = low
        low = st[i] - st[j]
        if temp > low:
            answer = temp
            print(answer)

# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)

On my j loop I am getting a index out of bounds and I donā€™t understand why. It should only loop through n and for the first test thatā€™s 3.

The ā€œfor jā€ loop is nested inside the ā€œfor iā€ loop, so the ā€œfor jā€ loop tries to compare numbers when the ā€œfor iā€ loop has not really read all the input numbers yet.

Thank you for the quick reply! I think I get it now. I would have to run the ā€œfor iā€ loop first by its self, then send that through 2 nested for loops to check each index?

That may work for the cases with fewer input numbers. The approach may not be efficient enough for the cases with more numbers.

Hello KomiSerloot,

Thanks for your message but debugging isnā€™t my problem.
In fact I code on my side then i put my code into Codingame (without ā€œcerrā€ so the debugging part).

My only problem must be the way i code, i donā€™t see any other reasonsā€¦

Thanks again :wink:

Hi,
I donā€™t understand, the IDE donā€™t give me the right N (for the 1st test, N should be 4 but mine is 3)ā€¦
Do you have the same problem ?

No. You may try copying out your code, resetting to the default code, and running the default code. You should be able to see the right N. Then figure out how to fix your code.

Thatā€™s not how you spell duelsā€¦

me too ,
I donā€™t know what is the problem!

Iā€™m using Dart as a language
I let the first loop to save all values in a list
then I sort it
then I compare every value with its next value but this algo took me more than what proposed
How can I do better ?

2 Likes

A similar timeout issue occurred in another puzzle previously (https://forum.codingame.com/t/stock-exchange-losses-puzzle-discussion/40/150), but that one seems fixed. Iā€™m not sure if this puzzle suffers from the same issue. My original solution which passed the puzzle also times out now.

Hi, Any one has completed in C, I really could not find how to optimise the solution am getting error timeout.

Please read the hint which can be found by clicking HINTS to the left of the puzzle statement. See if that helps you? If not, you may then read the cover page of the puzzle and look for another hint there :wink:

1 Like

I got the solution now.I have used the merge sort and once the array is sorted in perfect time span remaining problems are resolved and all 3 Test case or passed now.

[nope]