nikunjsoni

1124

Apr 7th, 2021
63
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     int longestWPI(vector<int>& hours) {
  4.         int curr=0, ans=0;
  5.         unordered_map<int, int> seen;
  6.         for(int i=0; i<hours.size(); i++){
  7.             curr += ((hours[i]>8) ? 1:-1);
  8.             if(curr > 0)
  9.                 ans = i+1;
  10.             if(seen.find(curr) == seen.end()){
  11.                 seen[curr] = i;
  12.             }
  13.             if(seen.find(curr-1) != seen.end()){
  14.                 ans = max(ans, i-seen[curr-1]);
  15.             }
  16.         }
  17.         return ans;
  18.     }
  19. };
RAW Paste Data