AMAN KUMAR的动态

查看AMAN KUMAR的档案

Full-Stack Web Development ???? | MERN Stack | Seeking New Challenges ??

42. Trapping Rain Water class Solution { public: ?int trap(vector<int>& height) { ? ? if (height.empty()) return 0; ? ? int n = height.size(), left = 0, right = n - 1, res = 0, maxleft = height[left], maxright = height[right]; ? ? while (left < right) { ? ? ? ? if (height[left] <= height[right]) { ? ? ? ? ? ? maxleft = max(maxleft, height[left]); ? ? ? ? ? ? res += maxleft - height[left]; ? ? ? ? ? ? ++left; ? ? ? ? } else { ? ? ? ? ? ? maxright = max(maxright, height[right]); ? ? ? ? ? ? res += maxright - height[right]; ? ? ? ? ? ? --right; ? ? ? ? } ? ? } ? ? return res; } };

要查看或添加评论,请登录