Editorial for Speeds


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
by: donchominkov

Sample solutions:

Video in BG:

JavaScript

let bestLength = 0;
let bestSum = -1;

const n = +gets();

let currentLength = 1;
let currentSum = +gets();

let groupSpeed = currentSum;

for (let i = 0; i < n - 1; i += 1) {
    const speed = +gets();
    if (groupSpeed < speed) {
        currentSum += speed;
        currentLength += 1;
    } else {
        if (bestLength == currentLength) {
            bestSum = Math.max(bestSum, currentSum);
        } else if (bestLength < currentLength) {
            bestLength = currentLength;
            bestSum = currentSum;
        }

        groupSpeed = speed;
        currentLength = 1;
        currentSum = speed;
    }
}

if (bestLength < currentLength) {
    bestLength = currentLength;
    bestSum = Math.max(bestSum, currentSum);
}

print(bestSum);

Java

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int n = in.nextInt();


    int groupSpeed = in.nextInt();

    int groupLength = 1;
    int groupSum = groupSpeed;
    int bestLength = 1;
    int bestSum = groupSpeed;

    for (int i = 0; i < n - 1; i++) {
      int speed = in.nextInt();

      if (groupSpeed < speed) {
        groupLength++;
        groupSum += speed;
      } else {
        groupSpeed = speed;
        groupSum = speed;
        groupLength = 1;
      }
      if (bestLength < groupLength) {
        bestLength = groupLength;
        bestSum = groupSum;
      } else if (bestLength == groupLength) {
        bestSum = Math.max(bestSum, groupSum);
      }
    }

    System.out.println(bestSum);
  }
}

Sample Solution in Python

import sys

n = int(sys.stdin.readline())

best_group_len = 0
best_group_speeds_sum = 0

current_group_len = 1
current_group_speeds_sum = int(sys.stdin.readline())
group_speed = current_group_speeds_sum

for i in range(n - 1):
    speed = int(sys.stdin.readline())
    if speed <= group_speed:
        if current_group_len > best_group_len:
            best_group_len = current_group_len
            best_group_speeds_sum = current_group_speeds_sum
        elif current_group_len == best_group_len:
            if best_group_speeds_sum < current_group_speeds_sum:
                best_group_speeds_sum = current_group_speeds_sum
        else:
            # current group is smaller than best
            pass
        group_speed = speed
        current_group_len = 1
        current_group_speeds_sum = speed
    else:
        current_group_len += 1
        current_group_speeds_sum += speed

if current_group_len > best_group_len:
    best_group_len = current_group_len
    best_group_speeds_sum = current_group_speeds_sum
elif current_group_len == best_group_len:
    if best_group_speeds_sum < current_group_speeds_sum:
        best_group_speeds_sum = current_group_speeds_sum
print(best_group_speeds_sum)

Comments

There are no comments at the moment.