Plans to restructure Thames Water have been approved by a High Court judge, allowing the struggling utility to stay afloat just weeks before it was set to run out of money.