How did King Solomon have ruach hakodesh/prophecy if he didn't observe the Torah properly? (referring to all his gentile wives)
The Tannaic (ancient) rabbis thought at one point to add Shlomo (King Solomon) to the list of the three kings who have no share in the World to Come, but G-d corrected them, and emphatically answered that his verdict is that of righteousness, and he apparently repented (Yerushalmi Sanhedrin ch. 10; Tanchuma Metzora 1). In other words, at the beginning and end, he was righteous and that's when he had his prophecy.