The accuracy of the extracted optical constants by terahertz time-domain spectroscopy critically depends on the accuracy of the sample thickness. However, due to the large number of optimization steps, it is time consuming to extract the optical constants over a broad frequency range and determine the sample thickness simultaneously using conventional algorithms. With particular emphasis on thin liquid samples, a three-dimensional optimization algorithm was used to obtain the optical constants and sample thickness simultaneously, significantly reducing the calculation time. In the experiments, the THz signals of 200-μm-thick water in a known cuvette were measured, and its accurate thickness and optical constants were determined by the algorithm, validating the effectiveness of the approach.