Can we solve The Best Vacation problem using sliding window concept, if yes so how
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
Can we solve The Best Vacation problem using sliding window concept, if yes so how
Name |
---|
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
maybe write the solution couple of hours afterward
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
Take a look at 81531917