Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
- // https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions
- //17 aug 2022
- #include <stdio.h>
- #define SIZE 10
- int main()
- {
- int i,j,k,diff,max_diff,max_i,max_j,arr[SIZE]={4, 8, 3, 6, 2, 9, 1, 5, 7,0 };
- //wrong variable names - it's better to rename max_i, max_j into buy_day, sell_day
- //also rename max_diff into profit
- //can't believe I named them so badly!
- max_diff =0;
- max_i=max_j=-1;
- for(i=0;i<SIZE-1;i++)
- {
- for(j=i;j<SIZE;j++)
- {
- diff = arr[j]-arr[i];
- if( diff > max_diff)
- {
- printf("old max_diff is %d",max_diff);
- max_diff = diff;
- max_i=i;max_j=j;
- printf("\tnew maax_diff is %d",max_diff);
- printf("\ti-%d j-%d\n",i,j);
- }
- }
- }
- if(max_i==-1)
- printf("zero\n");
- else
- printf("max_diff %d max_i %d max_j %d\n", max_diff, max_i,max_j);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement