Skip to content

Commit b1e31af

Browse files
Find peak element
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 32473f8 commit b1e31af

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

162_find_peak_element/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 peak.c

162_find_peak_element/peak.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static int findPeakElement(int* nums, int numsSize)
5+
{
6+
if (numsSize == 1) {
7+
return nums[0];
8+
}
9+
10+
int i;
11+
for (i = 1; i < numsSize && nums[i] > nums[i - 1]; i++) {}
12+
return i - 1;
13+
}
14+
15+
int main(int argc, char **argv)
16+
{
17+
int i, count = argc - 1;
18+
int *nums = malloc(count * sizeof(int));
19+
for (i = 0; i < count; i++) {
20+
nums[i] = atoi(argv[i + 1]);
21+
}
22+
printf("%d\n", findPeakElement(nums, count));
23+
return 0;
24+
}

0 commit comments

Comments
 (0)