Skip to content

Commit a576021

Browse files
Clone graph
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 06f42e8 commit a576021

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

133_clone_graph/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test clone_graph.c

133_clone_graph/clone_graph.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct hlist_node;
5+
6+
struct hlist_head {
7+
struct hlist_node *first;
8+
};
9+
10+
struct hlist_node {
11+
struct hlist_node *next, **pprev;
12+
};
13+
14+
static inline void INIT_HLIST_HEAD(struct hlist_head *h) {
15+
h->first = NULL;
16+
}
17+
18+
static inline void INIT_HLIST_NODE(struct hlist_node *n) {
19+
n->next = NULL;
20+
n->pprev = NULL;
21+
}
22+
23+
static inline int hlist_empty(struct hlist_head *h) {
24+
return !h->first;
25+
}
26+
27+
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
28+
{
29+
if (h->first != NULL) {
30+
h->first->pprev = &n->next;
31+
}
32+
n->next = h->first;
33+
n->pprev = &h->first;
34+
h->first = n;
35+
}
36+
37+
static inline void hlist_del(struct hlist_node *n)
38+
{
39+
struct hlist_node *next = n->next;
40+
struct hlist_node **pprev = n->pprev;
41+
*pprev = next;
42+
if (next != NULL) {
43+
next->pprev = pprev;
44+
}
45+
}
46+
47+
#define container_of(ptr, type, member) \
48+
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
49+
50+
#define list_entry(ptr, type, member) \
51+
container_of(ptr, type, member)
52+
53+
#define hlist_for_each(pos, head) \
54+
for (pos = (head)->first; pos; pos = pos->next)
55+
56+
#define hlist_for_each_safe(pos, n, head) \
57+
for (pos = (head)->first; pos && ({ n = pos->next; true; }); pos = n)
58+
59+
#define NEIGHBORS_MAX_SIZE 100
60+
61+
struct UndirectedGraphNode {
62+
int label;
63+
struct UndirectedGraphNode *neighbors[NEIGHBORS_MAX_SIZE];
64+
int neighborsCount;
65+
};
66+
67+
struct label_node {
68+
struct UndirectedGraphNode *gn;
69+
struct hlist_node node;
70+
};
71+
72+
static struct UndirectedGraphNode *find(int label, int size, struct hlist_head *heads)
73+
{
74+
int hash = (label < 0 ? -label : label) % size;
75+
struct hlist_node *p;
76+
hlist_for_each(p, &heads[hash]) {
77+
struct label_node *ln = list_entry(p, struct label_node, node);
78+
if (ln->gn->label == label) {
79+
return ln->gn;
80+
}
81+
}
82+
return NULL;
83+
}
84+
85+
static struct UndirectedGraphNode *recursive_clone(struct UndirectedGraphNode *graph, struct hlist_head *heads, int size)
86+
{
87+
if (graph == NULL) {
88+
return NULL;
89+
}
90+
91+
struct UndirectedGraphNode *node = find(graph->label, size, heads);
92+
if (node != NULL) {
93+
return node;
94+
}
95+
96+
node = malloc(sizeof(*node));
97+
node->label = graph->label;
98+
node->neighborsCount = graph->neighborsCount;
99+
struct label_node *ln = malloc(sizeof(*ln));
100+
ln->gn = node;
101+
int hash = (node->label < 0 ? -node->label : node->label) % size;
102+
hlist_add_head(&ln->node, &heads[hash]);
103+
104+
int i;
105+
for (i = 0; i < node->neighborsCount; i++) {
106+
node->neighbors[i] = recursive_clone(graph->neighbors[i], heads, size);
107+
}
108+
109+
return node;
110+
}
111+
112+
static struct UndirectedGraphNode *cloneGraph(struct UndirectedGraphNode *graph)
113+
{
114+
int i, cap = 1000;
115+
struct hlist_head *heads = malloc(cap * sizeof(*heads));
116+
for (i = 0; i < cap; i++) {
117+
INIT_HLIST_HEAD(&heads[i]);
118+
}
119+
120+
return recursive_clone(graph, heads, cap);
121+
}
122+
123+
int main(void)
124+
{
125+
struct UndirectedGraphNode n0, n1, n2;
126+
n0.label = 0;
127+
n1.label = 1;
128+
n2.label = 2;
129+
n0.neighborsCount = 2;
130+
n1.neighborsCount = 1;
131+
n2.neighborsCount = 1;
132+
n0.neighbors[0] = &n1;
133+
n0.neighbors[1] = &n2;
134+
n1.neighbors[0] = &n2;
135+
n2.neighbors[0] = &n2;
136+
137+
struct UndirectedGraphNode *res = cloneGraph(&n0);
138+
return 0;
139+
}

134_gas_station/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test gas_station.c

134_gas_station/gas_station.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize)
5+
{
6+
int i, j, store = 0, start = -1;
7+
for (i = 0; i < gasSize; i++) {
8+
if (start < 0) {
9+
start = i;
10+
}
11+
store += gas[i] - cost[i];
12+
if (store < 0) {
13+
store = 0;
14+
start = -1;
15+
}
16+
}
17+
if (start > 0) {
18+
for (i = 0; i < start; i++) {
19+
store += gas[i] - cost[i];
20+
if (store < 0) {
21+
return -1;
22+
}
23+
}
24+
}
25+
return start;
26+
}
27+
28+
int main(void)
29+
{
30+
int gas[] = { 4 };
31+
int cost[] = { 5 };
32+
int count = sizeof(gas) / sizeof(*gas);
33+
printf("%d\n", canCompleteCircuit(gas, count, cost, count));
34+
return 0;
35+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy