121. Найкращий час для купівлі та продажу акцій

текст перекладу
Наступний метод є інтуїтивно зрозумілим, але через двошаровий цикл for виникає складність часу O(N^2), що призводить до перевищення часу виконання (>48ms)

int maxProfit(int* prices, int pricesSize) {  
 int maxProfit=0, temp=0;  

 for(int i=0; i < pricesSize; i++)  
 {  
 for(int j=i; j < pricesSize; j++)  
 {  
 if(*(prices+i) < *(prices+j))  
 temp=*(prices+j)-*(prices+i);  
 if(maxProfitcurrentProfit) //prevent negetive result  
 {  
 sol=sol;  
 }  
 else  
 {  
 sol=currentProfit;  
 }  
 }  

 return sol;  
}




Перекладено з: [121. Best Time to Buy and Sell Stock](https://medium.com/@mandudu123/121-best-time-to-buy-and-sell-stock-1f1eba611037)

Leave a Reply

Your email address will not be published. Required fields are marked *