no u got it wrong first read the explanation i hav given.
for this case min=2 and profit=1 only
BTW it is an ACCEPTED solution
divyam
@divyam
Posts made by divyam

RE: Best Time to Buy and Sell Shares II

RE: Best Time to Buy and Sell Shares II
u hav to consider all the cases not only the adjacent ones. like in 5 2 8 4 7 9 best solution is (82)+(94).
also u can only buy one stock and sell one.
but according to ur code it would be (82)+(74).it can be done simply by finding the first local minimum.once u got 1 local minimum den u need to find first local maximum add dem to profit and continue untill u traverse full array.
class Solution { public: int maxProfit(vector<int> &prices) { int profit=0,i=0,n=prices.size(),min; while(i<n1) { while(i<n1 && prices[i+1]<=prices[i])//finding first local minimum i++; if(i==n1)//from i till last all elements r in decreasing order return profit; min=prices[i++]; while(i<n && prices[i1]<=prices[i])//find local maximum i++; profit+=prices[i1]min;//add to total profit } return profit; } };

RE: Search a 2D matrix
it is given that for each row first element is greater than last element of previous row.
since each row is sorted ,so ith element of previous row must be less than to last element of previous row and last element of previous row is less than ith element of given rowthus matrix is sorted colomn wise also

RE: If I use extra space, will OJ give me a notice or warning?
here is my code it is not using any extra space and any inbuilt functions
bool isPalindrome(int x) { int n=1;//n stores value of highest power of 10 less than x like for 1221 n=1000 if(x<0) return false; //since negative number cant be palindrome while(x/n>=10) n*=10; //calculating n while(x && n>1) { if(x/n!=x%10) //checking first and last number return false; x=x(x/n)*n; //removing leftmost digit from x x/=10; //removing rightmost digit from x n/=100; } return true; }

RE: How can i improve my code??
yes u r correct
i actually didnt considered dat list is sorted 
How can i improve my code??
I m using an array to store the number of occurrences of each element,i have taken assumption dat elements are less than 100 and greater than 100
int A[200]={0}; ListNode *temp=head,*prev=head; while(temp) { if(temp>val<0) A[temp>val+200]++;//if element is negative then add size of array to it else //so that index always remain positive A[temp>val]++; temp=temp>next; } temp=head; while(temp) { if((temp>val<0 && A[temp>val+200]>1)(temp>val>=0&&A[temp>val]>1)) { if(temp==head) { //if head is deleted than change head head=temp>next; prev=head; free(temp); temp=head; } else { //otherwise just delete temp prev>next=temp>next; free(temp); temp=prev>next; } } else { prev=temp; temp=temp>next; } } return head;
it takes O(n) for both running time n space.
any advice in improving it??