Question: 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$.

Answer: 
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.
Not sure about what you meant by saying "achieves a local maximum". If you meant that $M$ is the set of local maximum points, then here is a counter-example.

Let us consider the following function $f:R\rightarrow R$.

* $f(x) = 0$ for $x \leq 0$ and $x\geq 1$,
* $f(x) = x$ for $x > 0$ and $x \leq 1$,
* $f(x) = 1$ for $x > 1$ and $x \leq 2$,
* $f(x) = x-1$ for $x>2$ and $x \leq 3$.

The union of [-1,0) and [1,2) is the set of local maximum points, but it is open.