#(Code speed up) I'm training for a coding competation the task:

1 messages · Page 1 of 1 (latest)

covert gate
#

how could I speed my code up?
Andris and Gábor enjoy playing billiards and often go out to play against each other in a few matches. Gábor loves to brag about his good results against Andris. However, he calculates these results in a rather unique way: he only counts the matches starting from the most recent game, and goes back to the match where the difference between their victories is the greatest in his favor (draws are not considered). Gábor always includes the most recent match in his calculations. Formally, if in the last k matches Gábor has gk wins and Andris has ak wins, then he counts the results for that k where the difference between gk and ak is the largest in his favor. If there are multiple such values of k, he will use the smallest k.

Let's take their last N matches, each of which ended in either Andris's victory (A), Gábor's victory (G), or a draw (D). Write a program that determines the most favorable head-to-head result for Gábor after each match!

Input
The first line contains the integer N, the number of matches played.
The second line contains a string of length N, where each character represents the result of a match: 'A' for Andris's win, 'G' for Gábor's win, and 'D' for a draw.
Output
Output N lines, where the i-th line contains the most favorable result for Gábor after the i-th match. The result should be in the format "g - a", where g is the number of Gábor's victories, and a is the number of Andris's victories, considering only the matches that Gábor takes into account at that point.

I got my code in python: (scroll down)

golden lavaBOT
#

<@&987246964494204979> please have a look, thanks.

covert gate
#

i sec I'll translate this to java

pulsar hazel
#

It can be in Python.

#

But ideally you should include your question in your opening post. Otherwise you also miss out on the helper ping advantage.

covert gate
#

(Andris , Gábor mean names)

#

a probably good java version of the code: ```java
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
String results = scanner.nextLine();

    for (int i = 0; i < n; i++) {
        int maxDiff = Integer.MIN_VALUE;
        int bestG = 0;
        int bestA = 0;

        int tempGWins = 0;
        int tempAWins = 0;
        for (int k = i; k >= 0; k--) {
            if (results.charAt(k) == 'G') {
                tempGWins++;
            } else if (results.charAt(k) == 'A') {
                tempAWins++;
            }

            int diff = tempGWins - tempAWins;

            if (diff > maxDiff) {
                maxDiff = diff;
                bestG = tempGWins;
                bestA = tempAWins;
            }
        }

        System.out.println(bestG + "-" + bestA);
    }
    scanner.close();
}

}

golden lavaBOT
# covert gate a probably good java version of the code: ```java import java.util.Scanner; pub...

Detected code, here are some useful tools:

Formatted code
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = Integer.parseInt(scanner.nextLine());
    String results = scanner.nextLine();
    for (int i = 0; i < n; i++) {
      int maxDiff = Integer.MIN_VALUE;
      int bestG = 0;
      int bestA = 0;
      int tempGWins = 0;
      int tempAWins = 0;
      for (int k = i; k >= 0; k--) {
        if (results.charAt(k) == 'G') {
          tempGWins++;
        }
        else if (results.charAt(k) == 'A') {
          tempAWins++;
        }
        int diff = tempGWins - tempAWins;
        if (diff > maxDiff) {
          maxDiff = diff;
          bestG = tempGWins;
          bestA = tempAWins;
        }
      }
      System.out.println(bestG + "-" + bestA);
    }
    scanner.close();
  }
}
covert gate
# covert gate how could I speed my code up? Andris and Gábor enjoy playing billiards and often...
def main():
    n = int(input())
    results = input()

    for i in range(n):
        max_diff = -float("inf")
        best_g = 0
        best_a = 0

        temp_g_wins = 0
        temp_a_wins = 0
        for k in range(i, -1, -1):
            if results[k] == "G":
                temp_g_wins += 1
            elif results[k] == "A":
                temp_a_wins += 1

            diff = temp_g_wins - temp_a_wins

            if diff > max_diff:
                max_diff = diff
                best_g = temp_g_wins
                best_a = temp_a_wins

        print(f"{best_g}-{best_a}")


main()
#

(Code speed up) I'm training for a coding competation the task:

covert gate
pulsar hazel
#

Part of it is just waiting, but you also didn't ask a question.

dry glade
covert gate
#

yes that is what should happen

#

and I have been trying to do that but my brain is fried and my timelimit is ending in 23 mins

dry glade
#

and my timelimit is ending in 23 mins
Well, tbf your lack of planning isn't our emergency.

Nonetheless, do you even need to improve it? Like, is that a requirement to pass?
Cause otherwise, if you have a working solution, why not just use that?

covert gate
#

its a competation task

#

it has a time and memory limit

covert gate
pulsar hazel
#

To be honest, a competition is something you should solve yourself.

#

It's a question I'll pass on.

#

At least for the next 21 minutes.

covert gate
dry glade
covert gate
#

even chatgpt (not too much help)

dry glade
#

Where's the point in recruiting the best programmer to solve all the problems for you? What are you taking away from it?

pulsar hazel
covert gate
#

I aint got time for this... bye

#

.close

golden lavaBOT
# covert gate .close

Looks like you attempted to use a command? Please note that we only use slash-commands on this server 🙂

Try starting your message with a forward-slash / and Discord should open a popup showing you all available commands.
A command might then look like /foo 👍