Last active
January 3, 2016 00:19
-
-
Save pveyes/8381659 to your computer and use it in GitHub Desktop.
Twitter Interview Test http://qandwhat.apps.runkite.com/i-failed-a-twitter-interview/ My C solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
int graph[] = {2, 5, 1, 3, 1, 2, 1, 7, 7, 6}; | |
int size = sizeof(graph) / sizeof(graph[0]); | |
int water = waterization(graph, size); | |
printf("%d\n", water); | |
return 0; | |
} | |
int waterization(int graph[], int size) { | |
int i, sum = 0, startsum; | |
int posleft = 0, | |
posright = size - 1; | |
for (i = 1; i < size / 2; i++) { | |
if (graph[i] > graph[posleft]) { | |
posleft = i; | |
} | |
if (graph[size - i] > graph[posright]) { | |
posright = size - i; | |
} | |
} | |
if (graph[posleft] > graph[posright]) { | |
startsum = graph[posright]; | |
} | |
else { | |
startsum = graph[posleft]; | |
} | |
for (i = posleft; i < posright; i++) | |
{ | |
if (graph[i] < startsum) { | |
sum += startsum - graph[i]; | |
} | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment