Skip to content

Commit fc02ac6

Browse files
Improve
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent ec051ec commit fc02ac6

File tree

5 files changed

+67
-11
lines changed

5 files changed

+67
-11
lines changed

205_isomorphic_strings/1

Whitespace-only changes.

207_course_schedule/course_schedule.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ static bool dfs(struct graph_node *courses, int id, bool *takens, bool *touched)
2929
}
3030
}
3131

32-
#define N 2000
33-
static struct graph_node courses[N];
34-
static bool takens[N];
35-
static bool touched[N];
36-
3732
static bool canFinish(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize)
3833
{
3934
int i;
35+
bool *takens = malloc(numCourses);
36+
bool *touched = malloc(numCourses);
37+
struct graph_node *courses = malloc(numCourses * sizeof(*courses));
38+
4039
memset(courses, 0, numCourses * sizeof(*courses));
4140
memset(takens, false, numCourses * sizeof(bool));
4241
memset(touched, false, numCourses * sizeof(bool));

210_course_schedule_ii/course_schedule.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,18 @@ static bool dfs(struct graph_node *courses, int id, bool *takens, bool *touched,
3030
}
3131
}
3232

33-
#define N 2000
34-
static int order[N];
35-
static struct graph_node courses[N];
36-
static bool takens[N];
37-
static bool touched[N];
38-
3933
/**
4034
* Return an array of size *returnSize.
4135
* Note: The returned array must be malloced, assume caller calls free().
4236
*/
4337
static int *findOrder(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize, int *returnSize)
4438
{
4539
int i;
40+
int *order = malloc(numCourses * sizeof(int));
41+
bool *takens = malloc(numCourses);
42+
bool *touched = malloc(numCourses);
43+
struct graph_node *courses = malloc(numCourses * sizeof(*courses));
44+
4645
memset(courses, 0, numCourses * sizeof(*courses));
4746
memset(takens, false, numCourses * sizeof(bool));
4847
memset(touched, false, numCourses * sizeof(bool));

239_sliding_window_maximum/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 slide_window.c
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
/**
5+
* Return an array of size *returnSize.
6+
* Note: The returned array must be malloced, assume caller calls free().
7+
*/
8+
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize)
9+
{
10+
int i, head = 0, tail = 0;
11+
int count = 0;
12+
int *indexes = malloc(numsSize * sizeof(int));
13+
int *results = malloc((numsSize - k + 1) * sizeof(int));
14+
15+
for (i = 0; i < numsSize; i++) {
16+
/* monotonous decreasing */
17+
while (tail > head && nums[i] >= nums[indexes[tail - 1]]) {
18+
tail--;
19+
}
20+
indexes[tail++] = i;
21+
22+
if (indexes[head] <= i - k) {
23+
head++;
24+
}
25+
26+
if (i >= k - 1) {
27+
results[count++] = nums[indexes[head]];
28+
}
29+
}
30+
31+
*returnSize = count;
32+
return results;
33+
}
34+
35+
int main(int argc, char **argv)
36+
{
37+
if (argc < 2) {
38+
fprintf(stderr, "Usage: ./test k n1 n2...\n");
39+
exit(-1);
40+
}
41+
42+
int i, size = argc - 2;
43+
int k = atoi(argv[1]);
44+
int *nums = malloc(size * sizeof(int));
45+
for (i = 0; i < size; i++) {
46+
nums[i] = atoi(argv[i + 2]);
47+
}
48+
49+
int count = 0;
50+
int *lists = maxSlidingWindow(nums, size, k, &count);
51+
for (i = 0; i < count; i++) {
52+
printf("%d ", lists[i]);
53+
}
54+
printf("\n");
55+
return 0;
56+
}

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