diff --git a/Submission/BestTimeToBuyAndSellStocks/screenshot2.jpeg b/Submission/BestTimeToBuyAndSellStocks/screenshot2.jpeg new file mode 100644 index 0000000..6a0f6ad Binary files /dev/null and b/Submission/BestTimeToBuyAndSellStocks/screenshot2.jpeg differ diff --git a/Submission/BestTimeToBuyAndSellStocks/solution.c b/Submission/BestTimeToBuyAndSellStocks/solution.c new file mode 100644 index 0000000..c531c0c --- /dev/null +++ b/Submission/BestTimeToBuyAndSellStocks/solution.c @@ -0,0 +1,18 @@ +int maxProfit(int* prices, int n) { + if (n <= 1) return 0; + + int min = prices[0]; + int profit = 0; + + for (int i = 1; i < n; i++) { + if (prices[i] < min) + min = prices[i]; + else { + int p = prices[i] - min; + if (p > profit) + profit = p; + } + } + + return profit; +} \ No newline at end of file diff --git a/Submission/TwoSum/screenshot1.jpeg b/Submission/TwoSum/screenshot1.jpeg new file mode 100644 index 0000000..90d9af1 Binary files /dev/null and b/Submission/TwoSum/screenshot1.jpeg differ diff --git a/Submission/TwoSum/solution,c b/Submission/TwoSum/solution,c new file mode 100644 index 0000000..aa96f07 --- /dev/null +++ b/Submission/TwoSum/solution,c @@ -0,0 +1,64 @@ +#include + +#define SIZE 10007 + +typedef struct Node { + int key; + int index; + struct Node* next; +} Node; + +Node* table[SIZE]; + +int hash(int key) { + return ((long long)key + 1000000000LL) % SIZE; +} + +void insert(int key, int index) { + int h = hash(key); + Node* node = malloc(sizeof(Node)); + node->key = key; + node->index = index; + node->next = table[h]; + table[h] = node; +} + +int find(int key) { + int h = hash(key); + Node* curr = table[h]; + while (curr) { + if (curr->key == key) return curr->index; + curr = curr->next; + } + return -1; +} + +void clear() { + for (int i = 0; i < SIZE; i++) { + Node* curr = table[i]; + while (curr) { + Node* temp = curr; + curr = curr->next; + free(temp); + } + table[i] = NULL; + } +} + +int* twoSum(int* nums, int n, int target, int* returnSize) { + *returnSize = 2; + int* res = malloc(2 * sizeof(int)); + for (int i = 0; i < n; i++) { + int x = target - nums[i]; + int j = find(x); + if (j != -1) { + res[0] = j; + res[1] = i; + clear(); + return res; + } + insert(nums[i], i); + } + clear(); + return NULL; +} \ No newline at end of file