From 3bf75a2c4c3b5dfa5cb0c9bec23166725c774975 Mon Sep 17 00:00:00 2001 From: Ryad MEHIRI Date: Wed, 21 Oct 2026 10:56:52 +0200 Subject: [PATCH] =?UTF-8?q?F2=20:=20clients=20distincts=20avec=20un=20tabl?= =?UTF-8?q?eau=20de=20pr=C3=A9sence=20(O(n=C2=B2)=20=E2=86=92=20O(n=20+=20?= =?UTF-8?q?C))?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pour chaque transaction, on cherchait le même client parmi toutes les transactions précédentes (O(n²) au pire). Un tableau vu[NB_CLIENTS] indique en O(1) si le client a déjà été rencontré. Temps (-O2) : 0,073 s → 0,069 s (au niveau du bruit de mesure ; en -O0, nb_clients_distincts passe de 0,32 s à 0). Empreinte inchangée (ff68d260a7a58441). Co-Authored-By: Claude Opus 5.5 --- banque.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/banque.c b/banque.c index 6e833e6..6180c2f 100644 --- a/banque.c +++ b/banque.c @@ -89,17 +89,13 @@ void trier_par_montant(Transaction *t, int n) /* F2 : nombre de clients ayant au moins une transaction */ int nb_clients_distincts(const Transaction *t, int n) { + char vu[NB_CLIENTS] = {0}; /* vu[c] vaut 1 dès qu'une transaction du client c est rencontrée */ int nb = 0; for (int i = 0; i < n; i++) { - int deja_vu = 0; - for (int j = 0; j < i; j++) { - if (t[j].client == t[i].client) { - deja_vu = 1; - break; - } - } - if (!deja_vu) + if (!vu[t[i].client]) { + vu[t[i].client] = 1; nb++; + } } return nb; }