surajpanker82's blog

By surajpanker82, history, 4 years ago, In English

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

  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Take a look at 81531917