Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Best buy/sell day in stock
#include <iostream> #include <vector> #include <stdio.h> using namespace std; /* static void Main(string[] args) { double[] values = new double[8] { 55.39, 109.23, 48.29, 81.59, 81.58, 105.53, 94.45, 12.24 }; double max = double.MinValue, maxDiff = double.MinValue, diff = 0; double bottom = values[0]; for (int i = 1; i < values.Length; i++) { diff += values[i] - values[i - 1]; if (diff > maxDiff) { maxDiff = diff; max = values[i]; } if (values[i] < bottom) { bottom = values[i]; diff = 0; } } Console.WriteLine("Buy at {0}; Sell at {1}", max - maxDiff, max); } */ void printMaxSubSequence(vector<int>& t) { int max_so_far = 0;//t[0]; int max_ending_here = 0;//t[0]; int begin = 0; int begin_temp = 0; int end = 0; for (int i = 1; i < t.size(); i++) { if(max_ending_here < 0) { max_ending_here = t[i] - t[i - 1]; begin_temp = i; } else { max_ending_here += t[i] - t[i - 1]; } // calculate max_so_far if(max_ending_here >= max_so_far ) { max_so_far = max_ending_here; begin = begin_temp; end = i; } } cout << begin << " " << end; } int main(void) { vector<int> t; t.push_back(6); t.push_back(4); t.push_back(5); t.push_back(10); t.push_back(1); t.push_back(5); t.push_back(3); t.push_back(2); t.push_back(1); t.push_back(2); printMaxSubSequence(t); /* int arr[] = {-11,-2,3,-1,2,-9,-4,-5,-2, -3}; int cur = arr[0] >= 0? arr[0] : 0, max = arr[0]; int start = 0, end = 0; int i,j = cur == 0 ? 1 : 0; printf("Cur\tMax\tStart\tEnd\n"); printf("%d\t%d\t%d\t%d\n",cur,max,start,end); for (i = 1; i < 10; i++) { cur += arr[i]; if (cur > max) { max = cur; end = i; if (j > start) start = j; } if (cur < 0) { cur = 0; j = i+1; } printf("%d\t%d\t%d\t%d\n",cur,max,start,end); } */ }
run
|
edit
|
history
|
help
0
Wygner aprendizado
Gauss 4x4
Sangharsh.vhawale
BintTree vertical sum
Test 12(2021)
sample
dia
Destroy It!
override
WeekAgenda2