Re: [algogeeks] Re: DE Shaw written test
Test { 30,40,15,35,10,20 } using the logic specified as Find the min and max element in the array. If the min comes before max, then return the dates- min to buy and max to sell. But if the min comes after max, find the max element that comes after min i.e. maxRight find the min element that comes before max i.e. minLeft Now find the difference (max - minLeft)( maxRight - min) return the dates for which the difference is higher. It does not work. The assumption that either one of real max or real min has to be part to compute max profit does not seem correct. alternatively after findiing a profit, if we find a min, save the previous computations (imagine previous array is not part anymore and apply the same rule of findling min and then maxProfit. int minIndex = 0; int maxProfit = 0; int maxIndex = -1; int finalMinIndex = -1; int finalMaxIndex = -1; for (int i=1; in;i++) { if (a[i] - a[minIndex] maxProfit) { maxProfilt = a[i] - a[minIndex]; maxIndex = i; continue;} if (a[i] a[minIndex]) { if (maxIndex ==-1) minIndex = i; //safely ignore previous min as for upcoming max, the diff will be more now with this new min else { finalMinIndex = minIndex; finalMaxIndex = maxIndex; maxIndex = -1;} } } if (maxIndex !=-1) {finalMinIndex = minIndex; finalMaxIndex = maxIndex;} Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Sun, Aug 5, 2012 at 11:36 PM, Navin Kumar navin.nit...@gmail.com wrote: This is a linear time constant space algorithm. Assume the stocks prices are given in an array where index represents the day no . For ex a[365] represents stock price on 365th day of year. Find the min and max element in the array. If the min comes before max, then return the dates- min to buy and max to sell. But if the min comes after max, find the max element that comes after min i.e. maxRight find the min element that comes before max i.e. minLeft Now find the difference (max - minLeft)( maxRight - min) return the dates for which the difference is higher. Code:-- void FindDaytoBuySellStock(int stocks[], int N) // here N = 365 { int minPos = findMinPos(stocks); int maxPos = findMaxPos(stocks); if(minPos maxPos ) { BuyDate = minPos,SellDate = maxPos; } else{ minLeft = find min in array from 0 to maxPos-1 maxRight = find max in array from minPos+1 to N int d1 = max - minLeft; int d2 = maxRight - min; if(d1 = d2 ) {BuyDate = minLeft,SellDate = max; } else{ BuyDate = min ,SellDate = maxRight; } } } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/dSJCOIuUuKUJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Difference between memcpy and memmove
As far as I could understand, memcpy just iterate through the memory( byte by byte or word by word) and blindly copies 'n' number of bytes from source to destination without considering whether source and destination memory overlaps. While memmove first copies n number of bytes to intermediate buffer and then copies the buffer content to destination address. consider the following example: #include cstdio #include memory.h #include iostream using namespace std; int main() { char a[20] = weather is good; memcpy(a+1, a, strlen(a) ); cout a; return 0; } it produces the o/p: wweather is good But as per definition of memcpy, o/p should be : www. Its because, after writing 'w' at a[1], memcpy tries to copy a[1] to a[2] , but now we have 'w' at a[1]. so effectively w should be copied on subsequent locations (from a[2] onwards) strlen(a) times. can anybody explain the o/p wweather is good to me? Can you give me an example when just replacing memmove by memcpy produces different output? -- Vipin Delhi College of Engineering -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/QG9qW4hwkloJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
RE: [algogeeks] DE Shaw written test
The sequence of price is make more sense if buy price is less than next value then you can buy even its not the min and next day you can sell all and Buy again when min price stock come. Eg 6, 10, 5, 7, 2,7. There are many case need to consider the above is the only one scenario. Other like random order profits and loss so need to decide min n max for a interval. Here is no profit 9 8 7 6 5 4 3 2 1 :(. Sent from my Windows Phone -- From: Mukul Gupta Sent: 08/05/2012 8:47 PM To: algogeeks@googlegroups.com Subject: Re: [algogeeks] DE Shaw written test Thanks for pointing out the mistake.Though my code will correctly calculate the max_profit but I must keep track of the buying_day.I have made some modification in my code.Hope it works fine now. int min = a[0]; // initialized to first element int max_profit = 0; //when you buy and sell on same day int buying_day = a[0]; for ( int i = 1; i n; i++ ){ if ( max_profit (a[i] - min ) ){ max_profit = a[i] - min; buying_day = min; } if ( a[i] min ) min = a[i]; } Finally. I'll have buying_day and max_profit, so if you need to find the selling day you can easily calculate : Selling day = buying_day+max_profit; Correct me if I'm wrong. On Sun, Aug 5, 2012 at 5:43 PM, Arun Kindra arunkin...@gmail.com wrote: @harsha : yes, the problem is if u r finding only min and max value, it might happen that u sell the stock before buying. Ex- int a [ ] = { 5, 10, 4, 6, 7 }; the min value is 4 and max is 10 and 10 comes before 4, means u sell the stock before buying. and i think the sol given by mukul does the same mistake.we need to keep track this case also whether the day(array index) i m buying is not more than the day(array index) we are selling the stock. *correct me if m wrong*. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] DE Shaw written test
As i understand the question says, one time buy and one time sell, buy preceeds sell... if multiple such transactions allowed, we may need DP Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Mon, Aug 6, 2012 at 7:16 AM, umesh kewat umesh1...@gmail.com wrote: The sequence of price is make more sense if buy price is less than next value then you can buy even its not the min and next day you can sell all and Buy again when min price stock come. Eg 6, 10, 5, 7, 2,7. There are many case need to consider the above is the only one scenario. Other like random order profits and loss so need to decide min n max for a interval. Here is no profit 9 8 7 6 5 4 3 2 1 :(. Sent from my Windows Phone -- From: Mukul Gupta Sent: 08/05/2012 8:47 PM To: algogeeks@googlegroups.com Subject: Re: [algogeeks] DE Shaw written test Thanks for pointing out the mistake.Though my code will correctly calculate the max_profit but I must keep track of the buying_day.I have made some modification in my code.Hope it works fine now. int min = a[0]; // initialized to first element int max_profit = 0; //when you buy and sell on same day int buying_day = a[0]; for ( int i = 1; i n; i++ ){ if ( max_profit (a[i] - min ) ){ max_profit = a[i] - min; buying_day = min; } if ( a[i] min ) min = a[i]; } Finally. I'll have buying_day and max_profit, so if you need to find the selling day you can easily calculate : Selling day = buying_day+max_profit; Correct me if I'm wrong. On Sun, Aug 5, 2012 at 5:43 PM, Arun Kindra arunkin...@gmail.com wrote: @harsha : yes, the problem is if u r finding only min and max value, it might happen that u sell the stock before buying. Ex- int a [ ] = { 5, 10, 4, 6, 7 }; the min value is 4 and max is 10 and 10 comes before 4, means u sell the stock before buying. and i think the sol given by mukul does the same mistake.we need to keep track this case also whether the day(array index) i m buying is not more than the day(array index) we are selling the stock. *correct me if m wrong*. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Difference between memcpy and memmove
memcpy() just copies from source to destination.It copies the bytes of data between memory blocks. If the block of memory overlaps, the function might not work properly. memmove() is just like memcopy() but it deals with handling of overlapping of memory blocks. kazal HPU On 8/6/12, Vipin vipinaggarwa...@gmail.com wrote: As far as I could understand, memcpy just iterate through the memory( byte by byte or word by word) and blindly copies 'n' number of bytes from source to destination without considering whether source and destination memory overlaps. While memmove first copies n number of bytes to intermediate buffer and then copies the buffer content to destination address. consider the following example: #include cstdio #include memory.h #include iostream using namespace std; int main() { char a[20] = weather is good; memcpy(a+1, a, strlen(a) ); cout a; return 0; } it produces the o/p: wweather is good But as per definition of memcpy, o/p should be : www. Its because, after writing 'w' at a[1], memcpy tries to copy a[1] to a[2] , but now we have 'w' at a[1]. so effectively w should be copied on subsequent locations (from a[2] onwards) strlen(a) times. can anybody explain the o/p wweather is good to me? Can you give me an example when just replacing memmove by memcpy produces different output? -- Vipin Delhi College of Engineering -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/QG9qW4hwkloJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Elevator system design - Which ( Design Pattern, Class, DS) will be used
As this might be discussed many times in this thread but cant able to trace back the archived emails, so reiterating If i have to design a elevator system(for simplicity let's say 6 elevator), 1. How will be my *classes* look like ? 2. Which all *DesignPattern* i should use and how effective i can make implement the patterns ? 3. What *DataStructure *i should usequeue/priority queue ? * *Share Happy Sharing your thoughts ! ! ! -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.