Skip to content

Commit

Permalink
Jump game
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Jul 29, 2017
1 parent c595f87 commit e39d469
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 032_longest_valid_parentheses/valid_parentheses.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ static int longestValidParentheses(char* s) {
} else {
if (top > stack) {
if (--top == stack) {
/* The stack never keep ')' so we use 'error' to record index */
length = s - p - error;
} else {
/* The *(top - 1) is the index of the last kept '(' */
length = s - p - *(top - 1);
}
if (length > max_length) {
Expand Down
2 changes: 2 additions & 0 deletions 045_jump_game/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test jump_game.c
38 changes: 38 additions & 0 deletions 045_jump_game/jump_game.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>

static int jump(int* nums, int numsSize) {
if (numsSize <= 1) {
return 0;
}

int i;
int next_pos = numsSize - 1;
int pos = next_pos;
int *indexes = malloc(numsSize * sizeof(int));
int *p = indexes;

*p++ = numsSize - 1;
while (--pos >= 0) {
for (i = 0; i < p - indexes; i++) {
if (nums[pos] >= indexes[i] - pos) {
indexes[i + 1] = pos;
p = indexes + i + 2;
break;
}
}
}

return p - indexes - 1;
}

int main(int argc, char **argv)
{
int i, count = argc - 1;
int *nums = malloc(count * sizeof(int));
for (i = 0; i < count; i++) {
nums[i] = atoi(argv[i + 1]);
}
printf("%d\n", jump(nums, count));
return 0;
}

0 comments on commit e39d469

Please sign in to comment.