#include <vector> // dynamic array
using namespace std;
class Solution { // 904 O(N) O(1)
public:
int totalFruit(vector<int>& f) { // f: array of fruit types
int a = -1, b = -1, run = 0, cur = 0, ans = 0; // b = latest type
// run = length of trailing run of b;
// cur = current window length; ans = best found
for (int x : f) { // iterate each tree's fruit type
cur = (x == a || x == b) ? cur + 1 : run + 1; // x matches extend
// keep trailing run of b and add x
run = (x == b) ? run + 1 : 1; // update trailing length
if (x != b) { a = b; b = x; } // new type, b->a and set b to x
ans = (ans > cur) ? ans : cur; // keep max
}
return ans;
}
};
by the way~
同一個食物但包裝差異讓人覺得食物瞬間有不同體驗(但只有一個麥香雞堡XD)
留學經驗分享交流,跟考完雅思一樣,總是讓人持續的回想。
但重點要先專注發完鐵人賽,不要超時阿,我的隊友都好強,好早發文嗚嗚:)