Here is the problem from a book for metric spaces I'm trying to solve:
Let $f:Y\rightarrow\mathbb{R}$$k$-lipschitz in the subset $Y\subset \mathbb{R}$. Prove that there is a $k$-lipschitz function $g:\mathbb{R}\rightarrow\mathbb{R}$ such that $g|_Y=f$. Hint: Assume initially $f$ bounded and define $g(x)=\underset{y \in Y}{\inf}\left[f(y)+k\cdot d(x,y)\right]$$\forall x\in \mathbb{R}$. For the general case, extend successively each restriction $f_n=f|_{Y_n}$ to $\left[-n-1,n+1\right]$, being$Y_n = \left[-n,n\right]\cup \left\{Y\cap\left[-n-1,n+1\right]\right\}$.Obs.: $f:Y\rightarrow\mathbb{R}$ is $k$-lipschitz implies that $\left|f(x)-f(y)\right|\leq k \cdot d(x,y)$ for any $x,y\in Y$. $d$ is a metric.
I might have managed to solve the case for $f$ bounded. Here is what I've done:
Proof. Assume $f$ bounded and $x\in Y$, thus, because $f$ is $k$-lipschitz,\begin{equation*} f(y)+k\cdot d(x,y)\geq f(y) + \left|f(x)-f(y)\right|,\end{equation*}\begin{equation*} f(y)+k\cdot d(x,y)\geq f(y) - f(y)+f(x),\end{equation*}\begin{equation*} f(y)+k\cdot d(x,y)\geq f(x).\end{equation*}Taking the infimum from both sides,\begin{equation} g(x)\geq f(x).\tag{1}\label{1}\end{equation}Now, observe that\begin{equation*} g(x)\leq f(y)+k\cdot d(x,y)\end{equation*}and make $y=x$. We have,\begin{equation*} g(x)\leq f(x).\tag{2}\label{2}\end{equation*}From \eqref{1} and \eqref{2},\begin{equation*} g(x)=f(x)\,\forall x \in Y.\end{equation*}
I don't know how to proceed with $f$ unbounded. I imagine I might be unable to take the infimum because I won't be sure of its existence. I'd appreciate some help. This problem was actually already solved here, but I didn't understand totally and I'd like to use the author's hint.