diff --git a/banque.c b/banque.c index 62a9290..e300a41 100644 --- a/banque.c +++ b/banque.c @@ -128,18 +128,21 @@ void total_par_jour(const Transaction *t, int n, long *totaux) long meilleure_periode(const long *totaux, int nb_jours, int *debut, int *fin) { long meilleur = totaux[0]; + long somme_courante = totaux[0]; + int debut_courant = 0; *debut = 0; *fin = 0; - for (int i = 0; i < nb_jours; i++) { - for (int j = i; j < nb_jours; j++) { - long somme = 0; - for (int k = i; k <= j; k++) - somme += totaux[k]; - if (somme > meilleur) { - meilleur = somme; - *debut = i; - *fin = j; - } + for (int j = 1; j < nb_jours; j++) { + if (somme_courante < 0) { + somme_courante = totaux[j]; + debut_courant = j; + } else { + somme_courante += totaux[j]; + } + if (somme_courante > meilleur) { + meilleur = somme_courante; + *debut = debut_courant; + *fin = j; } } return meilleur;