If every real-valued continuous bounded function on a metric space $M$ attains its maximum (or minimum), then $M$ is compact

1.2k Views Asked by At

Suppose that $(M,d)$ is a metric space. I want to show if every continuous bounded function $f:M \rightarrow \mathbb{R}$ achieves a maximum or minimum, them $M$ is compact.

I found a similar assertion in the question If every real-valued continuous function is bounded on $X$ (metric space), then $X$ is compact. The trick of proof of that assertion is to assume that $M$ is not compact and then make an unbounded function, but here the function must be bounded. What should I do?