Advertisement
Lisaveta777

2 - Blind 75

Aug 17th, 2022
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. //  https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
  2. // https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions
  3. //17 aug 2022
  4.  
  5. #include <stdio.h>
  6. #define SIZE 10
  7.  
  8.  
  9. int main()
  10. {
  11.    
  12.     int i,j,k,diff,max_diff,max_i,max_j,arr[SIZE]={4, 8, 3, 6, 2, 9, 1, 5, 7,0 };
  13.     //wrong variable names - it's better to rename max_i, max_j into buy_day, sell_day
  14.     //also rename max_diff into profit
  15.       //can't believe I named them so badly!
  16.    
  17.     max_diff =0;
  18.     max_i=max_j=-1;
  19.    
  20.     for(i=0;i<SIZE-1;i++)
  21.     {
  22.         for(j=i;j<SIZE;j++)
  23.         {
  24.             diff = arr[j]-arr[i];
  25.             if( diff > max_diff)
  26.             {
  27.                 printf("old max_diff is %d",max_diff);
  28.                 max_diff = diff;
  29.                 max_i=i;max_j=j;
  30.                 printf("\tnew maax_diff is %d",max_diff);
  31.                 printf("\ti-%d  j-%d\n",i,j);
  32.             }
  33.         }
  34.     }
  35.     if(max_i==-1)
  36.         printf("zero\n");
  37.     else
  38.        printf("max_diff %d  max_i  %d  max_j  %d\n", max_diff, max_i,max_j);
  39.     return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement