編輯:關於Android編程
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
1 兩段分段思想
2 前往後,後往前都可以處理數列的思想
時間復雜度是O(n),不掌握這種思想是很難做出來的。
class Solution {
public:
int maxProfit(vector &prices)
{
vector profit(prices.size()+1);
int buy = INT_MAX;
for (int i = 0; i < prices.size(); i++)
{
if (prices[i] < buy) buy = prices[i];
else profit[i+1] = max(profit[i], prices[i]-buy);
}
int sale = INT_MIN , max_profit = 0, res = 0;
for (int i = prices.size() - 1; i >= 0 ; i--)
{
if (prices[i] > sale) sale = prices[i];
else max_profit = max(max_profit, sale - prices[i]);
profit[i+1] = profit[i+1]+ max_profit;
res = max(profit[i+1], res);
}
return res;
}
};
android快速上手(二)android開發環境搭建及hello world
基本了解了java語法,下一步,我們一起開啟hello world的神秘之旅。 (一)android開發環境搭建 之前搭建android開發環境是件非常費力的事情,
android之路之Handler的使用
Handler的作用一般是子線程向主線程中傳遞消息,用來主線程處理和UI相關的東西。為什麼要在子線程中用呢,因為如果主線程處理了過多耗時的東西,可能會導致假死,所以一般都
Android實現Service下載文件,Notification顯示下載進度的示例
先放個gif。。最終效果如果: 主要演示了Android從服務器下載文件,調用Notification顯示下載進度,並且在下載完畢以後點擊通知會跳轉到安裝APK的界面,演
Gradle for Android學習筆記(二)
Gradle之管理多個Module編譯在一個工程項目中,我們可能會有多個Module,如:多個app,library。我們來看下一個最簡單的多個Module的build文