Question: Does it matter if we do something that would be considered bad in a situation if in the end it turns out to be a great or righteous result?
Answer: Your question raises additional questions before an answer can be given. The most important additional question is: from whose point of view are we examining what is “good vs bad” in the given situation, and from whose point of view are we reaching a “great and righteous” result?
As long as we remain in our inherently self-centered, egoistic and subjective point of view, we will always inevitably judge such intermediary or final states from selfish, egoistic and subjective point of view.
In principle what you are suggesting is right, if we have a great and righteous final purpose, then any preceding state, action that hastens, brings forward the desired final state can be judged as purposeful, necessary, regardless of how it feels at the time of the actual, intermediary state.
But in order to judge the final state as truly “great and righteous” we need to rise above self-interest and acquire the capability of look at life, our integrated and interdependent system ‘from the side”, so our calculations, judgments are impeccable, selfless and objective.
Then from that objective viewpoint we can judge the final result perfect and accept any “unpleasant” intermediary state as the sourness of the unripe fruit.