Last active
August 29, 2015 14:20
-
-
Save luckypapa/eacc8c8c9fa9cd90a863 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //http://codercareer.blogspot.kr/2013/02/no-44-maximal-stolen-values.html | |
| #include <stdio.h> | |
| #define ARRAY_MAX 10 | |
| #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
| int findMaxStolenValue(int *array, int size) { | |
| if (size == 0) | |
| return 0; | |
| if (size == 1) | |
| return array[0]; | |
| if (size == 2) | |
| return (int)MAX(array[0], array[1]); | |
| int value1 = array[0]; | |
| int value2 = (int)MAX(array[0], array[1]); | |
| int maxValue; | |
| for (int i = 2; i < size; i++) { | |
| maxValue = (int)MAX(value2, value1 + array[i]); | |
| value1 = value2; | |
| value2 = maxValue; | |
| } | |
| return maxValue; | |
| } | |
| int main() { | |
| printf("Test Start\n"); | |
| int array1[ARRAY_MAX] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //25 | |
| int array2[ARRAY_MAX] = {3, 10, 3, 7, 5, 4, 7, 6, 2}; //27 | |
| int array3[ARRAY_MAX] = {10, 1, 10, 1, 1, 10, 1, 3, 1}; //33 | |
| int array4[ARRAY_MAX] = {3, 4, 1, 2, 5, 2, 8, 4, 2}; //19 | |
| int array5[ARRAY_MAX] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //25 | |
| printf("array1 Max Stolen Value : %d \n", findMaxStolenValue(array1, ARRAY_MAX)); | |
| printf("array2 Max Stolen Value : %d \n", findMaxStolenValue(array2, ARRAY_MAX)); | |
| printf("array3 Max Stolen Value : %d \n", findMaxStolenValue(array3, ARRAY_MAX)); | |
| printf("array4 Max Stolen Value : %d \n", findMaxStolenValue(array4, ARRAY_MAX)); | |
| printf("array5 Max Stolen Value : %d \n", findMaxStolenValue(array5, ARRAY_MAX)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment