A particle traveling with velocity $v_a $ in the medium $A$ and with velocity $v_b $ in the medium $B$. The particle starts at time $t = 0$ from the point $P_i$ and has to get in the minimum time to the point $P_j$.
How can i determine the trajectory that this particle must follow to reach the point $P_f$ in minimum time.

Some help to formulate the equation for this problem.
Assuming that no force is acting on the particle, the time to go from $P_i$ to $P_j$ is $$T=\frac{l_{P_iP}}{v_a}+\frac{l_{PP_j}}{v_b}$$ where $P$ is some point at the interface between the two mediums and $l_{P_iP},\ l_{PP_j}$ denotes their lengths. Now, let the Interface denotes $x$ axis and the points $P_i,P_j$ have coordinates $(0,h),\ (s,-m)$ respectively. Let $P$ has coordinate $(x,0)$. Then $$l_{P_iP}=\sqrt{x^2+h^2},\ l_{PP_j}=\sqrt{(x-s)^2+m^2}$$ So, $T$ is minimized if $\frac{dT}{dx}=0,\ \frac{d^2T}{dT^2}>0$. Then, $$\frac{dT}{dx}=0\Rightarrow \frac{x}{v_a\sqrt{x^2+h^2}}+\frac{x-s}{v_b\sqrt{(x-s)^2+m^2}}=0\\ \Rightarrow \frac{\sin \theta_i}{v_a}=\frac{\sin \theta_r}{v_b}$$ where $\theta_i,\ \theta_r$ are the angles of incidence and refraction respectively. You can perhaps recognize this result. It is Snell's law of refraction, which is actually based upon Fermat's principle of least time. Hope your question is answered.