-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1679-maxOperations.c
More file actions
105 lines (91 loc) · 2.51 KB
/
Copy path1679-maxOperations.c
File metadata and controls
105 lines (91 loc) · 2.51 KB
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
#include <stdio.h>
#include <stdlib.h>
// Define a hash map entry
typedef struct {
int key;
int value;
} HashMapEntry;
// Define a hash map
typedef struct {
HashMapEntry* entries;
int size;
int capacity;
} HashMap;
// Function to create a hash map
HashMap* createHashMap(int capacity) {
HashMap* map = (HashMap*)malloc(sizeof(HashMap));
map->entries = (HashMapEntry*)malloc(capacity * sizeof(HashMapEntry));
map->size = 0;
map->capacity = capacity;
for (int i = 0; i < capacity; i++) {
map->entries[i].key = 0;
map->entries[i].value = 0;
}
return map;
}
// Hash function
int hash(int key, int capacity) {
return abs(key) % capacity;
}
// Function to find an entry in the hash map
HashMapEntry* findEntry(HashMap* map, int key) {
int index = hash(key, map->capacity);
while (map->entries[index].key != 0 && map->entries[index].key != key) {
index = (index + 1) % map->capacity;
}
return &map->entries[index];
}
// Function to insert a key-value pair into the hash map
void insert(HashMap* map, int key, int value) {
HashMapEntry* entry = findEntry(map, key);
if (entry->key == 0) {
map->size++;
}
entry->key = key;
entry->value = value;
}
// Function to get the value associated with a key
int get(HashMap* map, int key) {
HashMapEntry* entry = findEntry(map, key);
if (entry->key == 0) {
return 0;
}
return entry->value;
}
// Function to increment the value associated with a key
void increment(HashMap* map, int key) {
HashMapEntry* entry = findEntry(map, key);
if (entry->key == 0) {
map->size++;
}
entry->key = key;
entry->value++;
}
// Function to decrement the value associated with a key
void decrement(HashMap* map, int key) {
HashMapEntry* entry = findEntry(map, key);
if (entry->key != 0) {
entry->value--;
}
}
// Function to free the hash map
void freeHashMap(HashMap* map) {
free(map->entries);
free(map);
}
int maxOperations(int* nums, int numsSize, int k) {
HashMap* map = createHashMap(numsSize * 2); // Create a hash map with double capacity for better performance
int maxPairs = 0;
for (int i = 0; i < numsSize; i++) {
int complement = k - nums[i];
int count = get(map, complement);
if (count > 0) {
maxPairs++;
decrement(map, complement);
} else {
increment(map, nums[i]);
}
}
freeHashMap(map); // Free the hash map
return maxPairs;
}