Skip to content

Commit 65ac101

Browse files
authored
Merge pull request #2763 from mdmzfzl/leetcode-0981
Added 0981-time-based-key-value-store.c
2 parents 61025b1 + 0a6e175 commit 65ac101

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

c/0981-time-based-key-value-store.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
// Define a struct to hold key-value pairs and timestamps
6+
typedef struct {
7+
char* key;
8+
char* value;
9+
int timestamp;
10+
} Entry;
11+
12+
// Define the TimeMap structure
13+
typedef struct {
14+
Entry** entries; // Array of arrays to store key-value pairs and timestamps
15+
int* sizes; // Array to store the current size of each entry
16+
int* capacities; // Array to store the current capacity of each entry
17+
int size; // Number of distinct keys
18+
} TimeMap;
19+
20+
// Function to create a new TimeMap
21+
TimeMap* timeMapCreate() {
22+
TimeMap* obj = (TimeMap*)malloc(sizeof(TimeMap));
23+
obj->entries = NULL;
24+
obj->sizes = NULL;
25+
obj->capacities = NULL;
26+
obj->size = 0;
27+
return obj;
28+
}
29+
30+
// Function to set a key-value pair with timestamp
31+
void timeMapSet(TimeMap* obj, char* key, char* value, int timestamp) {
32+
if (obj == NULL || key == NULL || value == NULL) return;
33+
34+
int index = -1;
35+
for (int i = 0; i < obj->size; i++) {
36+
if (strcmp(obj->entries[i][0].key, key) == 0) {
37+
index = i;
38+
break;
39+
}
40+
}
41+
42+
// If key is not found, create a new entry
43+
if (index == -1) {
44+
obj->size++;
45+
obj->entries = (Entry**)realloc(obj->entries, sizeof(Entry*) * obj->size);
46+
obj->sizes = (int*)realloc(obj->sizes, sizeof(int) * obj->size);
47+
obj->capacities = (int*)realloc(obj->capacities, sizeof(int) * obj->size);
48+
49+
index = obj->size - 1;
50+
obj->entries[index] = (Entry*)malloc(sizeof(Entry));
51+
obj->sizes[index] = 0;
52+
obj->capacities[index] = 1;
53+
}
54+
// If the entry is full, double its capacity
55+
else if (obj->sizes[index] == obj->capacities[index]) {
56+
obj->capacities[index] *= 2;
57+
obj->entries[index] = (Entry*)realloc(obj->entries[index], sizeof(Entry) * obj->capacities[index]);
58+
}
59+
60+
// Add the new entry to the TimeMap
61+
Entry* entry = &obj->entries[index][obj->sizes[index]];
62+
entry->key = strdup(key);
63+
entry->value = strdup(value);
64+
entry->timestamp = timestamp;
65+
66+
obj->sizes[index]++;
67+
}
68+
69+
// Function to get the value for a given key and timestamp
70+
char* timeMapGet(TimeMap* obj, char* key, int timestamp) {
71+
if (obj == NULL || key == NULL || obj->size == 0) return "";
72+
73+
// Iterate through entries to find the matching key
74+
for (int i = 0; i < obj->size; i++) {
75+
if (strcmp(obj->entries[i][0].key, key) == 0) {
76+
Entry* entries = obj->entries[i];
77+
int left = 0;
78+
int right = obj->sizes[i] - 1;
79+
int index = -1;
80+
81+
// Binary search to find the appropriate timestamp index
82+
while (left <= right) {
83+
int mid = left + (right - left) / 2;
84+
85+
if (entries[mid].timestamp <= timestamp) {
86+
index = mid;
87+
left = mid + 1;
88+
} else {
89+
right = mid - 1;
90+
}
91+
}
92+
93+
// If a matching timestamp is found, return the corresponding value
94+
if (index != -1) {
95+
return entries[index].value;
96+
}
97+
}
98+
}
99+
100+
return ""; // Return an empty string if key or timestamp is not found
101+
}
102+
103+
// Function to free memory allocated by the TimeMap
104+
void timeMapFree(TimeMap* obj) {
105+
if (obj == NULL) return;
106+
107+
for (int i = 0; i < obj->size; i++) {
108+
for (int j = 0; j < obj->sizes[i]; j++) {
109+
free(obj->entries[i][j].key);
110+
free(obj->entries[i][j].value);
111+
}
112+
free(obj->entries[i]);
113+
}
114+
115+
free(obj->entries);
116+
free(obj->sizes);
117+
free(obj->capacities);
118+
free(obj);
119+
}

0 commit comments

Comments
 (0)