Let $(K,d)$ be a compact metric space, and $f:K\rightarrow \mathbb{R}$ be a continuous function on $K$. Define: $$M=\left \{ x\in K :\text{$f$ achieves a local maximum in $x$} \right \}$$
I need to show that $K$ is compact. Now, my strategy was to define $V$ to be the set of all local maximum values of $f$ on $K$; that is $f^{-1}(V)=M$. If I can show that $V$ is closed, then by the continuity of $f$, $f^{-1}(V)=M$ will be closed, hence compact since $M\subset K$.
I considered a limit point $y$ of $V$, and was able to prove that $y=f(c)$ for some $x\in K$, but I don't know how to shows that $f$ achieves a local maximum at $x$ (that is, $y \in V$).
Thanks in advance!
EDIT: We say that $f$ has a local maximum at a point $x\in K$ if there exists $\delta >0$ such that $f(p)≤f(x)$ for all $p\in K$ such that $d(x,p)<\delta$.
Note that $f(M) = \alpha$ for a single $\alpha\in \mathbb{R}$. Therefore $M = f^{-1}(\alpha)$ is closed, since $\{\alpha\}$ is a closed set. We now have a closed subset of a compact space, which means that $M$ is also compact. See for example https://proofwiki.org/wiki/Closed_Subspace_of_Compact_Space_is_Compact for that result.