This is universal help for all of your homework problems - not only for this problem but any computing problem.
- Determine what the problem is
- Determine what steps you would go through to solve with pencil and paper
- Determine which parts are repetitive
Now, convert to code by using the meaning of code: convert one description to another.
(your manual process for equivalent programming steps).
Now, after debugging for typos and such, you should have a working (although not optimal) solution.
Now - start to become smarter by optimizing steps. This comes with experience. This does not come from have other people do your assignments.
By analogy, this works for the world outside of programming.