Williams battles to thrilling win over Ostapenko