forked from menault/2026_TD3_DEV51_Qualite_Algo
F2 : clients distincts avec un tableau de présence (O(n²) → O(n + C))
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user