Shanghai is the week after Beijing, so potentially 1000 more points there for Djoko.
If we assume Djokovic wins in USO, Beijing and Shanghai there is nothing Federer can do in USO, he will then need points from Shanghai. Final in USO won't be enough. So to ensure #1 for 300 weeks sooner he needs to match Djokovic in USO. To ensure 299 weeks, however, a USO QF is enough.
Realistically speaking, Federer's #1 ranking is secure until after
Shanghai so long as he reaches the US Open final... Djokovic defends the full 2000 at USO, whereas Roger only defends 720. So if Roger gains 480 by reaching the final (and Novak defends the title) that would leave him 1375 points above Djokovic heading into the Asian swing.
In that scenario, Novak's only hope of regaining #1 by the end of Shanghai (and thus leaving Federer on 299 weeks) would be if he won both Beijing (500 points) and Shanghai (1000 points) and Roger decided to skip Asia altogether.