Optimal charging of a quantum battery