Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Stock buy/sell, maximum subarray problem
#include <iostream> #include <vector> #include <climits> #include <stdio.h> using namespace std; void maxSubArray(vector<int>& t) { if (t.empty()) return; int maxDiff = INT_MIN; int diff = 0; int lowest = t[0]; int begin = 0; int end = 0; int tmpBegin = 0; for (int i = 0; i < t.size(); ++i) { diff += t[i] - t[i - 1]; if (diff > maxDiff) { maxDiff = diff; begin = tmpBegin; end = i; } if (t[i] < lowest) { lowest = t[i]; diff = 0; tmpBegin = i; } } cout << begin << " " << end << endl; cout << maxDiff << endl; } int main() { vector<int> t = { 1, -29, 515, -29, -30, -25, -20, 199}; maxSubArray(t); }
run
|
edit
|
history
|
help
0
w1
MyStack
Einstein's Physics
FindKthElementDivideConquer
Two pointer - MUST DO
New wall
Vector+Memory_Adv_C++_Tutorial
abbinsertbool
Merge overlapping time intervals
infections.cpp