Advertisement
tiom4eg

V2 MaxWindow

Apr 20th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. function max(a: int, b: int) -> int {
  2.     if (a > b) { return a; }
  3.     return b;
  4. }
  5.  
  6. struct Window {
  7.     mv: int,
  8.     l: int[],
  9.     r: int[],
  10.     function push(self: Window, x: int) {
  11.         self.r.push(x);
  12.         self.mv = max(self.mv, x);
  13.     }
  14.     function pop(self: Window) {
  15.         if (self.l.length() == 0) {
  16.             let mx = -2000000007;
  17.             while (self.r.length() > 0) {
  18.                 mx = max(mx, self.r[self.r.length() - 1]);
  19.                 self.l.push(mx);
  20.                 self.r.pop();
  21.             }
  22.             self.mv = -2000000007;
  23.         }
  24.         self.l.pop();
  25.     }
  26.     function get(self: Window) -> int {
  27.         let res = self.mv;
  28.         if (self.l.length() > 0) { res = max(res, self.l[self.l.length() - 1]); }
  29.         return res;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement