暴力模拟删除每个元素后最大间隔的变化即可
#include <algorithm> #include <climits> #include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n) { vector<int> nums(n); int temp; for (int i = 0; i < n; i++) { cin >> temp; nums[i] = temp; } int maxDiff = 0; for (int i = 0; i < n - 1; i++) { maxDiff = max(maxDiff, nums[i + 1] - nums[i]); } vector<int> dp(n, 0); dp[0] = INT_MAX, dp[n - 1] = INT_MAX; for (int i = 1; i < n - 1; i++) { dp[i] = max(maxDiff, nums[i + 1] - nums[i - 1]); } cout << *min_element(dp.begin(), dp.end()) << endl; } return 0; }
时间复杂度:O(n),用于遍历数组
空间复杂度:O(n),用于存储数组