解題心得
不能用暴力解,會TLE。
可以一邊讀新的數字進來,一邊紀錄是不是目前遇到的最大值,並且把之前紀錄的最大值跟當下吃到的數字比較,如果比較大就記起來。
程式碼
#include <iostream> using namespace std; int main() { int T; cin>>T; while(T--) { int n, max=0, ans=-999999,temp; cin>>n; for(int i=0;i<n;i++) { cin>>temp; if(i==0) { max = temp; continue; } if(max-temp>ans) ans = max-temp; if(temp>max) max=temp; } cout<<ans<<endl; } return 0; }
沒有留言:
張貼留言