tag:blogger.com,1999:blog-4731036105252322780.post615330435044449785..comments2024-03-01T02:55:58.951-08:00Comments on Buttercola: Leetcode: 3Sum SmallerButter is looking for a jobhttp://www.blogger.com/profile/01481083468821703855noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-4731036105252322780.post-44215126679869540942020-11-08T12:31:19.536-08:002020-11-08T12:31:19.536-08:00The above code is incorrect. It will fail most of ...The above code is incorrect. It will fail most of the Test Casesbm1905https://www.blogger.com/profile/05293242884201904711noreply@blogger.comtag:blogger.com,1999:blog-4731036105252322780.post-89015365734729559652020-11-06T13:22:40.939-08:002020-11-06T13:22:40.939-08:00the above code is wrong
here is a code using backt...the above code is wrong<br />here is a code using backtracking<br />res=[]<br />nums=sorted([-2, 0, 1, 3])<br />target=2<br />def btr(com,k,n):<br /> #print(res)<br /> if(k==0 and sum(com)=target):<br /> return<br /> for i in range(n,len(nums)):<br /> btr(com+[nums[i]],k-1,i+1)<br />btr([],3,0)<br />print(res)Anonymoushttps://www.blogger.com/profile/17765306489549566837noreply@blogger.comtag:blogger.com,1999:blog-4731036105252322780.post-41282654201318175352018-12-21T21:50:15.953-08:002018-12-21T21:50:15.953-08:00If A[i] + A[j] + A[k] < target, then we can fix...If A[i] + A[j] + A[k] < target, then we can fix i and j and move k in the range (j+1,k) to get all feasible triplets. so we add the total as (k-j) tripletsAnonymoushttps://www.blogger.com/profile/10493715994639743547noreply@blogger.comtag:blogger.com,1999:blog-4731036105252322780.post-36832951807555873982017-07-13T12:26:10.179-07:002017-07-13T12:26:10.179-07:00You can't sort. i < j < k .. means you n...You can't sort. i < j < k .. means you need to retain the order of the indices! A more "brute force solution" with a duplicate removal mechanism is required.Anonymoushttps://www.blogger.com/profile/08405862893652872008noreply@blogger.comtag:blogger.com,1999:blog-4731036105252322780.post-58175965417189747972017-02-27T02:41:58.971-08:002017-02-27T02:41:58.971-08:00Arrays.sort(array);
for(int i = 0 ; i=target){
...Arrays.sort(array);<br /><br /> for(int i = 0 ; i=target){<br /> break;<br /> }<br /> for(int j = i+1;j<array.length;j++){<br /> for(int k=j+1;k<array.length;k++){<br /> count++;<br /> sum=0;<br /> sum= array[i]+array[j]+array[k];<br /> if(sum < target){<br /> System.out.println("["+array[i]+","+array[j]+","+array[k]+"]");<br /> }else{<br /> <br /> break;<br /> }<br /> }// K<br /> }// J<br /> }// I<br /> System.out.println("count:"+count);Anonymoushttps://www.blogger.com/profile/03374906567761357686noreply@blogger.comtag:blogger.com,1999:blog-4731036105252322780.post-83041593068695656902017-02-27T02:40:02.105-08:002017-02-27T02:40:02.105-08:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/03374906567761357686noreply@blogger.comtag:blogger.com,1999:blog-4731036105252322780.post-33339873172056518722017-02-27T02:37:52.926-08:002017-02-27T02:37:52.926-08:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/03374906567761357686noreply@blogger.comtag:blogger.com,1999:blog-4731036105252322780.post-79662833160655246242016-07-13T18:11:54.020-07:002016-07-13T18:11:54.020-07:00If A[i] + A[j] + A[k] < target, which means the...If A[i] + A[j] + A[k] < target, which means the numbers between j and k are all less than target, because the array is sorted. Then we move the j pointer forward. <br /><br />If it's less then the target, then that means it needs to be accounted for when you update the result count. If you don't, the algorithm won't hit those cases.<br />Anonymoushttps://www.blogger.com/profile/11944569555973048871noreply@blogger.comtag:blogger.com,1999:blog-4731036105252322780.post-80260786517451917242016-03-07T18:32:56.965-08:002016-03-07T18:32:56.965-08:00Why you used result += (k - j) instead of just res...Why you used result += (k - j) instead of just result++ ?<br />Can you explain the logic ?Pawanhttps://www.blogger.com/profile/10235406606518735255noreply@blogger.com