Блог пользователя surajpanker82

Автор surajpanker82, история, 4 года назад, По-английски

Can we solve The Best Vacation problem using sliding window concept, if yes so how

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Yeah...

You actually can, similar to my binary search solution, doing two-pointer instead of bs, for each time left pointer moves, the right pointer moves into the biggest pos that $$$\Sigma_{i=l}^{r}d_i\le x$$$ and do the rest of the thing(updating the ans).

It can be done in $$$O(n)$$$, better than my $$$O(n\log n)$$$ solution

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

we know that the maximal substring will be at the end of a month then stretch backwards x days, because that collects the maximal amount of points (if you go forward, you're collecting starting from 1). so for every position X, slide window backwards. it's kinda cancer to implement though

take for example calendar 1 4 3 2, x = 5

start at 2, sliding window extends to 2, then to 3, and then stops.

now goes to 3, so sliding window removes 2 indices and stretches into 4 by 2 indices

now goes to 4, so add 3 days but then spend 3 days to push back window to 1 4

now finally case 1, slide back around so sliding window encapsulates 1, 2, and the last 2 days of 3

take maximal sum accordingly using sum of natural numbers

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

Take a look at 81531917