From 38afea4824b8e27252cbd94b5ba24c4d96bd0710 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Sun, 9 Feb 2025 15:45:21 -0600 Subject: [PATCH] Initial commit. --- .gitignore | 1 + .prettierignore | 1 + README.md | 5 +++ two-sum/.gitignore | 1 + two-sum/Makefile | 5 +++ two-sum/runTwoSum.c | 26 ++++++++++++ two-sum/twoSum.c | 98 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 .prettierignore create mode 100644 README.md create mode 100644 two-sum/.gitignore create mode 100644 two-sum/Makefile create mode 100644 two-sum/runTwoSum.c create mode 100644 two-sum/twoSum.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..693222e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.dSYM \ No newline at end of file diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..2e1fa2d --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +*.md \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..612115f --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Jesse Brault: LeetCode Solutions + +These are my LeetCode solutions. They are a work in progress. I am trying to them in C, since I want to have a good +language challenge and get good performance in the process. Also, C is not "batteries included"—I'll have to write my +data structures by hand and optimize them as needed. diff --git a/two-sum/.gitignore b/two-sum/.gitignore new file mode 100644 index 0000000..56d41e4 --- /dev/null +++ b/two-sum/.gitignore @@ -0,0 +1 @@ +twoSum diff --git a/two-sum/Makefile b/two-sum/Makefile new file mode 100644 index 0000000..cf8a1c5 --- /dev/null +++ b/two-sum/Makefile @@ -0,0 +1,5 @@ +twoSum: runTwoSum.c twoSum.c + gcc -Wall -std=gnu99 -g runTwoSum.c -o twoSum + +run: twoSum + ./twoSum diff --git a/two-sum/runTwoSum.c b/two-sum/runTwoSum.c new file mode 100644 index 0000000..386bb1f --- /dev/null +++ b/two-sum/runTwoSum.c @@ -0,0 +1,26 @@ +#include "./twoSum.c" +#include +#include + +int main() { + int nums[] = { 0, 4, 3, 0 }; + int returnSize = 0; + int *result = twoSum(nums, 4, 0, &returnSize); + bool foundFirst, foundSecond; + for (int i = 0; i < returnSize; i++) { + switch (result[i]) { + case 0: + foundFirst = true; + break; + case 3: + foundSecond = true; + break; + } + } + if (foundFirst && foundSecond) { + return 0; + } else { + fprintf(stderr, "Did not find indices [0, 1]\n"); + return 1; + } +} diff --git a/two-sum/twoSum.c b/two-sum/twoSum.c new file mode 100644 index 0000000..ce75c12 --- /dev/null +++ b/two-sum/twoSum.c @@ -0,0 +1,98 @@ +#include + +typedef struct { + int key, value; +} HashMapEntry; + +typedef struct { + int size; + HashMapEntry **entries; +} HashMap; + +HashMap *makeHashMap(int size) { + HashMap *m = malloc(sizeof(HashMap)); + m->size = size; + m->entries = malloc(size * sizeof(HashMapEntry*)); + for (int i = 0; i < size; i++) { + m->entries[i] = NULL; + } + return m; +} + +int hashCode(HashMap *m, int key) { + return ((unsigned int) key * 2654435761U) % m->size; +} + +HashMapEntry *makeEntry(int key, int value) { + HashMapEntry *e = malloc(sizeof(HashMapEntry)); + e->key = key; + e->value = value; + return e; +} + +#define PUT_SUCCESS 1 +#define PUT_FAIL 0 + +int putAt(HashMap *m, HashMapEntry *e, int index) { + if (m->entries[index] == NULL) { + m->entries[index] = e; + return PUT_SUCCESS; + } + return PUT_FAIL; +} + +int put(HashMap *m, int key, int value) { + int hash = hashCode(m, key); + HashMapEntry *e = makeEntry(key, value); + for (int i = hash; i < m->size; i++) { + if (putAt(m, e, i)) return PUT_SUCCESS; + } + for (int i = 0; i < hash; i++) { + if (putAt(m, e,i)) return PUT_SUCCESS; + } + return PUT_FAIL; +} + +HashMapEntry *get(HashMap *m, int key) { + int hash = hashCode(m, key); + for (int i = hash; i < m->size; i++) { + if (m->entries[i] != NULL) { + HashMapEntry *e = m->entries[i]; + if (e->key == key) { + return e; + } + } + } + for (int i = 0; i < hash; i++) { + if (m->entries[i] != NULL) { + HashMapEntry *e = m->entries[i]; + if (e->key == key) { + return e; + } + } + } + return NULL; +} + +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* twoSum(int* nums, int numsSize, int target, int* returnSize) { + HashMap *m = makeHashMap(numsSize / 0.7); + for (int i = 0; i < numsSize; i++) { + int complement = target - nums[i]; + HashMapEntry *e = get(m, complement); + if (e != NULL && e->value != i) { + int *result = malloc(2 * sizeof(int)); + result[0] = i; + result[1] = e->value; + *returnSize = 2; + return result; + } else if (!put(m, nums[i], i)) { + *returnSize = 0; + return malloc(0); // fail + } + } + *returnSize = 0; + return malloc(0); +} \ No newline at end of file