优化0332重新安排行程 C 版本

遍历所有ticket查找机场为O(n), 修改为使用hash表查找.
This commit is contained in:
slaier
2023-10-15 21:27:03 +08:00
parent 6b77db5be5
commit 77d6e986bb

View File

@ -652,62 +652,100 @@ function findItinerary(tickets: string[][]): string[] {
### C ### C
```C ```C
char **result; typedef struct {
bool *used; char *name; /* key */
int g_found; int cnt; /* 记录到达机场是否飞过了 */
UT_hash_handle hh; /* makes this structure hashable */
} to_airport_t;
int cmp(const void *str1, const void *str2) typedef struct {
{ char *name; /* key */
const char **tmp1 = *(char**)str1; to_airport_t *to_airports;
const char **tmp2 = *(char**)str2; UT_hash_handle hh; /* makes this structure hashable */
int ret = strcmp(tmp1[0], tmp2[0]); } from_airport_t;
if (ret == 0) {
return strcmp(tmp1[1], tmp2[1]); void to_airport_destroy(to_airport_t *airports) {
to_airport_t *airport, *tmp;
HASH_ITER(hh, airports, airport, tmp) {
HASH_DEL(airports, airport);
free(airport);
} }
return ret;
} }
void backtracting(char *** tickets, int ticketsSize, int* returnSize, char *start, char **result, bool *used) void from_airport_destroy(from_airport_t *airports) {
{ from_airport_t *airport, *tmp;
if (*returnSize == ticketsSize + 1) { HASH_ITER(hh, airports, airport, tmp) {
g_found = 1; to_airport_destroy(airport->to_airports);
return; HASH_DEL(airports, airport);
free(airport);
} }
}
int name_sort(to_airport_t *a, to_airport_t *b) {
return strcmp(a->name, b->name);
}
bool backtracking(from_airport_t *airports, int target_path_len, char **path,
int path_len) {
if (path_len == target_path_len) return true;
from_airport_t *from_airport = NULL;
HASH_FIND_STR(airports, path[path_len - 1], from_airport);
if (!from_airport) return false;
for (to_airport_t *to_airport = from_airport->to_airports;
to_airport != NULL; to_airport = to_airport->hh.next) {
if (to_airport->cnt == 0) continue;
to_airport->cnt--;
path[path_len] = to_airport->name;
if (backtracking(airports, target_path_len, path, path_len + 1))
return true;
to_airport->cnt++;
}
return false;
}
char **findItinerary(char ***tickets, int ticketsSize, int *ticketsColSize,
int *returnSize) {
from_airport_t *airports = NULL;
// 记录映射关系
for (int i = 0; i < ticketsSize; i++) { for (int i = 0; i < ticketsSize; i++) {
if ((used[i] == false) && (strcmp(start, tickets[i][0]) == 0)) { from_airport_t *from_airport = NULL;
result[*returnSize] = (char*)malloc(sizeof(char) * 4); to_airport_t *to_airport = NULL;
memcpy(result[*returnSize], tickets[i][1], sizeof(char) * 4); HASH_FIND_STR(airports, tickets[i][0], from_airport);
(*returnSize)++; if (!from_airport) {
used[i] = true; from_airport = malloc(sizeof(from_airport_t));
/*if ((*returnSize) == ticketsSize + 1) { from_airport->name = tickets[i][0];
return; from_airport->to_airports = NULL;
}*/ HASH_ADD_KEYPTR(hh, airports, from_airport->name,
backtracting(tickets, ticketsSize, returnSize, tickets[i][1], result, used); strlen(from_airport->name), from_airport);
if (g_found) {
return;
}
(*returnSize)--;
used[i] = false;
} }
HASH_FIND_STR(from_airport->to_airports, tickets[i][1], to_airport);
if (!to_airport) {
to_airport = malloc(sizeof(to_airport_t));
to_airport->name = tickets[i][1];
to_airport->cnt = 0;
HASH_ADD_KEYPTR(hh, from_airport->to_airports, to_airport->name,
strlen(to_airport->name), to_airport);
}
to_airport->cnt++;
} }
return;
}
char ** findItinerary(char *** tickets, int ticketsSize, int* ticketsColSize, int* returnSize){ // 机场排序
if (tickets == NULL || ticketsSize <= 0) { for (from_airport *from_airport = airports; from_airport != NULL;
return NULL; from_airport = from_airport->hh.next) {
HASH_SRT(hh, from_airport->to_airports, name_sort);
} }
result = malloc(sizeof(char*) * (ticketsSize + 1));
used = malloc(sizeof(bool) * ticketsSize); char **path = malloc(sizeof(char *) * (ticketsSize + 1));
memset(used, false, sizeof(bool) * ticketsSize); path[0] = "JFK"; // 起始机场
result[0] = malloc(sizeof(char) * 4); backtracking(airports, ticketsSize + 1, path, 1);
memcpy(result[0], "JFK", sizeof(char) * 4);
g_found = 0; from_airport_destroy(airports);
*returnSize = 1;
qsort(tickets, ticketsSize, sizeof(tickets[0]), cmp);
backtracting(tickets, ticketsSize, returnSize, "JFK", result, used);
*returnSize = ticketsSize + 1; *returnSize = ticketsSize + 1;
return result; return path;
} }
``` ```