1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| typedef struct IdxHashEntry { int key; struct ListNode * head; UT_hash_handle hh; }IdxHashEntry;
typedef struct SetHashEntry { int key; UT_hash_handle hh; }SetHashEntry;
typedef struct Pair { int idx; int step; }Pair;
void hashAddIdxItem(struct IdxHashEntry **obj, int key, int val) { struct IdxHashEntry *pEntry = NULL; struct ListNode * node = (struct ListNode *)malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL;
HASH_FIND(hh, *obj, &key, sizeof(key), pEntry); if (NULL == pEntry) { pEntry = (struct IdxHashEntry *)malloc(sizeof(struct IdxHashEntry)); pEntry->key = key; pEntry->head = node; HASH_ADD(hh, *obj, key, sizeof(int), pEntry); } else { node->next = pEntry->head; pEntry->head = node; } }
struct IdxHashEntry *hashFindIdxItem(struct IdxHashEntry **obj, int key) { struct IdxHashEntry *pEntry = NULL; HASH_FIND(hh, *obj, &key, sizeof(int), pEntry); return pEntry; }
void hashFreeIdxAll(struct IdxHashEntry **obj) { struct IdxHashEntry *curr = NULL, *next = NULL; HASH_ITER(hh, *obj, curr, next) { HASH_DEL(*obj, curr); free(curr); } }
void hashAddSetItem(struct SetHashEntry **obj, int key) { struct SetHashEntry *pEntry = NULL; HASH_FIND(hh, *obj, &key, sizeof(key), pEntry); if (pEntry == NULL) { pEntry = malloc(sizeof(struct SetHashEntry)); pEntry->key = key; HASH_ADD(hh, *obj, key, sizeof(int), pEntry); } }
struct SetHashEntry *hashFindSetItem(struct SetHashEntry **obj, int key) { struct SetHashEntry *pEntry = NULL; HASH_FIND(hh, *obj, &key, sizeof(int), pEntry); return pEntry; }
void hashFreeSetAll(struct SetHashEntry **obj) { struct SetHashEntry *curr = NULL, *next = NULL; HASH_ITER(hh, *obj, curr, next) { HASH_DEL(*obj, curr); free(curr); } }
int minJumps(int* arr, int arrSize){ struct IdxHashEntry * idxSameValue = NULL; for (int i = 0; i < arrSize; i++) { hashAddIdxItem(&idxSameValue, arr[i], i); } struct SetHashEntry * visitedIndex = NULL; struct Pair * queue = (struct Pair *)malloc(sizeof(struct Pair) * arrSize * 2); int head = 0; int tail = 0; queue[tail].idx = 0; queue[tail].step = 0; tail++; hashAddSetItem(&visitedIndex, 0); while (head != tail) { int idx = queue[head].idx; int step = queue[head].step; head++; if (idx + 1 == arrSize) { hashFreeIdxAll(&idxSameValue); hashFreeSetAll(&visitedIndex); free(queue); return step; } int v = arr[idx]; step++; struct IdxHashEntry * pEntry = hashFindIdxItem(&idxSameValue, v); if (NULL != pEntry) { for (struct ListNode * node = pEntry->head; node; node = node->next) { if (NULL == hashFindSetItem(&visitedIndex, node->val)) { hashAddSetItem(&visitedIndex, node->val); queue[tail].idx = node->val; queue[tail].step = step; tail++; } } HASH_DEL(idxSameValue, pEntry); } if (idx + 1 < arrSize && NULL == hashFindSetItem(&visitedIndex, idx + 1)) { hashAddSetItem(&visitedIndex, idx + 1); queue[tail].idx = idx + 1; queue[tail].step = step; tail++; } if (idx - 1 >= 0 && NULL == hashFindSetItem(&visitedIndex, idx - 1)) { hashAddSetItem(&visitedIndex, idx - 1); queue[tail].idx = idx - 1; queue[tail].step = step; tail++; } } hashFreeIdxAll(&idxSameValue); hashFreeSetAll(&visitedIndex); free(queue); return -1; }
|