数据结构练习(40)数对之差的最大值
http://zhedahht.blog.163.com/blog/static/2541117420116135376632/
思路:
很简单的动态规划题目,设置一个当前最大值和当前最大差值就行了。
#include <iostream> using namespace std; int MaxDelta(int data[], unsigned length) { if (data == nullptr || length <= 1) return 0; int delta = data[0] - data[1]; int maxvalue = data[0]; for (int i = 1; i < length - 1; ++i) { if (data[i] > maxvalue) maxvalue = data[i]; if (maxvalue - data[i+1] > delta) delta = maxvalue - data[i+1]; } return delta; } int main() { int data[] = {2, 4, 1, 16, 7, 5, 11, 9}; int delta = MaxDelta(data, 8); cout << delta << endl; return 0; }
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------