fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. // Write an efficient C program to find the sum of contiguous
  7. // subarray within a one-dimensional array of numbers which has the largest sum.
  8. #define MAX_COINS 1000
  9. int maxSubArray(vector<int > &data )
  10. {
  11.  
  12. }
  13.  
  14. #define MIN_NUM 0xffff
  15. int maxSubArrayDP(vector<int > &data )
  16. {
  17. // dynamic programming
  18. int i;
  19. int max = 0;
  20. int max_now = 0;
  21.  
  22. for (i = 0; i < data.size(); i++)
  23. {
  24. max_now += data[i];
  25. max_now = (max_now >0)? max_now:0;
  26.  
  27. if (max < max_now)
  28. max = max_now;
  29. }
  30. return max;
  31. }
  32.  
  33. int main() {
  34. // your code goes here
  35. vector<int> a = {-2, -3, 4, -1, -2, 1, 5, -3};
  36.  
  37. cout << maxSubArrayDP(a) << endl;
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
7