Asymptotically optimal shapes for counting lattice points and eigenvalues