classSolution{public:longlongnumberOfWeeks(vector<int>&milestones){// The best strategy is to pick "max, nonMax, max, nonMax, ...".constintmx=ranges::max(milestones);constlongsum=accumulate(milestones.begin(),milestones.end(),0L);constlongnonMax=sum-mx;returnmin(sum,2*nonMax+1);}};
123456789
classSolution{publiclongnumberOfWeeks(int[]milestones){// The best strategy is to pick "max, nonMax, max, nonMax, ...".finalintmx=Arrays.stream(milestones).max().getAsInt();finallongsum=Arrays.stream(milestones).asLongStream().sum();finallongnonMax=sum-mx;returnMath.min(sum,2*nonMax+1);}}
123456
classSolution:defnumberOfWeeks(self,milestones:list[int])->int:# The best strategy is to pick 'max, nonMax, max, nonMax, ...'.summ=sum(milestones)nonMax=summ-max(milestones)returnmin(summ,2*nonMax+1)