At Gettysburg, maybe Lee figured he needed a victory inside Northern territory because the South could not compete with the North in terms of resources and logistics.
So perhaps Pickett’s charge was a calculated gamble. If not now, when? He had to take the chance to secure a major victory because after Gettysburg and Grant/Sherman’s successes in the West, it was almost guaranteed that the South would lose.
Then it was a terrible calculation. Lee had already seen at Malvern Hill and Fredericksburg the results of an uphill massed charge over open ground against an entrenched enemy - annihilation. Longstreet saw it and did everything he could to change Lee's mind. "Sir, no 15 thousand men who ever took the field of battle could take that hill"