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
/** | |
* Problem : 10684 - The jackpot. | |
* Author : ~menon. | |
**/ | |
#include <stdio.h> | |
int getGain(int bets[], int size); | |
int main() | |
{ | |
int bets[100002], N, i, maxSum; | |
while(scanf("%d", &N)) | |
{ | |
if(N == 0) break; | |
for(i = 0; i < N; i++) { | |
scanf("%d", &bets[i]); | |
} | |
maxSum = getGain(bets, N); | |
if(maxSum > 0) { | |
printf("The maximum winning streak is %d.\n", maxSum); | |
} | |
else { | |
printf("Losing streak.\n"); | |
} | |
} | |
return 0; | |
} | |
// Kadane's Algorithm; | |
int getGain(int bets[], int size) | |
{ | |
int max_end_here = 0; | |
int max_so_far = 0; | |
int i; | |
for(i = 0; i < size; i++) | |
{ | |
max_end_here += bets[i]; | |
if(max_end_here < 0) { | |
max_end_here = 0; | |
} | |
if(max_end_here > max_so_far) { | |
max_so_far = max_end_here; | |
} | |
} | |
return max_so_far; | |
} |
Comments
Post a Comment