A well known result from mathematical analysis is that if $K$ is a non empty compact metric space and $f:K \rightarrow \mathbb{R}$ is a continuous function, then $f$ achieves its maximum and minimum values on $K$. Is the reciprocal statement true? If $K$ is a metric space such that every continuous function $f:K \rightarrow \mathbb{R}$ achieves its maximum and minimum values on $K$ then $K$ is compact?
I couldn't think of a counterexample and I thought that this might be true. I was trying to prove this by using the fact that if every sequence $(x_k)$ of $K$ has a convergent subsequence that converges on $K$ then $K$ is compact. But I was not able to prove it. If $(x_k)$ is a sequence of $K$, since $f$ achieves its maximum and minimum on $K$ then the sequence $f(x_k)$ is bounded and therefore there exists a convergent subsequence $f(x_{k_j})$, but It does not necessarily imply that $(x_k)$ has a convergent subsequence on $K$.