字节4.13笔试第四题 楼顶数楼问题
#include <iostream> #include <vector> using namespace std; void getRes(vector<int> &heights, vector<int> &res, int len) { int j, cnt; for (int i = 0; i < len; ++i) { cnt = 0; j = i - 1; while (j >= 0) { if (heights[j] <= heights[i]) ++cnt; else break; --j; } j = i + 1; while (j < len) { if (heights[j] <= heights[i]) ++cnt; else break; ++j; } res.push_back(cnt); } } int main() { int t, n, temp; vector<int> heights; vector<int> res; cin >> t; for (int i = 0; i < t; ++i) { cin >> n; heights.clear(); res.clear(); for (int j = 0; j < n; ++j) { cin >> temp; heights.push_back(temp); } getRes(heights, res, n); int k; for (k = 0; k < (res.size() - 1); ++k) cout << res[k] << " "; cout << res[k] << endl; } system("pause"); return 0; }只会用暴力解,而且只过了71%的用例,求求大佬们给个思路。