classSolution{public:intmaxSum(vector<int>&nums1,vector<int>&nums2){constexprintkMod=1'000'000'007;// Keep the running the sum of `nums1` and `nums2` before the next// rendezvous. Since `nums1` and `nums2` are increasing, move forward on the// smaller one to ensure we don't miss any rendezvous. When meet rendezvous,// choose the better path.longans=0;// sum(nums1) in (the prevoious rendezvous, the next rendezvous)longsum1=0;// sum(nums2) in (the prevoious rendezvous, the next rendezvous)longsum2=0;inti=0;// nums1's indexintj=0;// nums2's indexwhile(i<nums1.size()&&j<nums2.size())if(nums1[i]<nums2[j]){sum1+=nums1[i++];}elseif(nums1[i]>nums2[j]){sum2+=nums2[j++];}else{// An rendezvous happens.ans+=max(sum1,sum2)+nums1[i];sum1=0;sum2=0;++i;++j;}while(i<nums1.size())sum1+=nums1[i++];while(j<nums2.size())sum2+=nums2[j++];return(ans+max(sum1,sum2))%kMod;}};
classSolution{publicintmaxSum(int[]nums1,int[]nums2){finalintMOD=1_000_000_007;// Keep the running the sum of `nums1` and `nums2` before the next rendezvous.// Since `nums1` and `nums2` are increasing, move forward on the smaller one// to ensure we don't miss any rendezvous. When meet rendezvous, choose the// better path.longans=0;// sum(nums1) in (the prevoious rendezvous, the next rendezvous)longsum1=0;// sum(nums2) in (the prevoious rendezvous, the next rendezvous)inti=0;// nums1's indexintj=0;// nums2's indexwhile(i<nums1.length&&j<nums2.length)if(nums1[i]<nums2[j]){sum1+=nums1[i++];}elseif(nums1[i]>nums2[j]){sum2+=nums2[j++];}else{// An rendezvous happens.ans+=Math.max(sum1,sum2)+nums1[i];sum1=0;sum2=0;++i;++j;}while(i<nums1.length)sum1+=nums1[i++];while(j<nums2.length)sum2+=nums2[j++];return(int)((ans+Math.max(sum1,sum2))%MOD);}}
classSolution:defmaxSum(self,nums1:list[int],nums2:list[int])->int:# Keep the running the sum of `nums1` and `nums2` before the next rendezvous.# Since `nums1` and `nums2` are increasing, move forward on the smaller one# to ensure we don't miss any rendezvous. When meet rendezvous, choose the# better path.ans=0sum1=0# sum(nums1) in (the prevoious rendezvous, the next rendezvous)sum2=0# sum(nums2) in (the prevoious rendezvous, the next rendezvous)i=0# nums1's indexj=0# nums2's indexwhilei<len(nums1)andj<len(nums2):ifnums1[i]<nums2[j]:sum1+=nums1[i]i+=1elifnums1[i]>nums2[j]:sum2+=nums2[j]j+=1else:# An rendezvous happens.ans+=max(sum1,sum2)+nums1[i]sum1=0sum2=0i+=1j+=1whilei<len(nums1):sum1+=nums1[i]i+=1whilej<len(nums2):sum2+=nums2[j]j+=1return(ans+max(sum1,sum2))%(10**9+7)
Thanks for stopping by! If you find this site helpful, consider buying me some protein powder to keep me fueled! π