If I ignore the worries about it not being the most optimal and focus only on the bold part as being the question:
Is it okay to post material, such as a puzzle or a question, from another site on Stackoverflow so the answer/solution may be provided here?
A: Yes its fine, as long as you properly give credit and the site's licensing allows it. When in doubt, don't do it. You do have to replicate the relevant bits of the material here, a direct link to the original site in itself is not enough as that is subject to link rot. The full code answer would also have to be posted here and not on an external site.
But one can't ignore the fact that the "most optimal" worry apparently had to be added to the question, it implies a hidden agenda. That hidden agenda probably puts it at risk of being off-topic.
do you want to ask others to provide different solutions "for comparison"? That's not a question, that's just a code request.
do you want to ask if the solution you have is good? That's a code review.
I have the idea this is the winner: do you just want to post the solution to a puzzle to "help" others - and you're worrying if the solution is actually good?
You'd have to begin with asking yourself what the value of the solution is regardless of it being correct or incorrect, since specifically where a puzzle is concerned it is all about the process towards the solution and not the solution itself.
Imagine a Rubik's cube. When the cube is in its solution state, all squares on each sides with a matching color, the cube is pointless other than being a decorative piece. If you'd want to provide an answer about its true purpose, you'd have to be talking about the twisting and turning to get it to the solution, optionally in the least amount of time possible.
So what would the answer you would be writing about the programming puzzle be about? Only your code, or does it mention twisting and turning of the code to get it to that solution state? If only the code, my answer would be "No, not a valid question and answer".