Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions Submission/BestTimeToBuyAndSellStocks/solution.c
Original file line number Diff line number Diff line change
@@ -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;
}
Binary file added Submission/TwoSum/screenshot1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions Submission/TwoSum/solution,c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdlib.h>

#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;
}