iT邦幫忙

2025 iThome 鐵人賽

DAY 17
1

https://ithelp.ithome.com.tw/upload/images/20251001/20177944OJON7kDufs.jpg

https://ithelp.ithome.com.tw/upload/images/20251001/20177944mVdBcJ4NDg.jpg

https://ithelp.ithome.com.tw/upload/images/20251001/20177944qZeVVXd0uo.jpg

https://ithelp.ithome.com.tw/upload/images/20251001/20177944XV8IueDCuY.jpg

#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)

留學經驗分享交流,跟考完雅思一樣,總是讓人持續的回想。

但重點要先專注發完鐵人賽,不要超時阿,我的隊友都好強,好早發文嗚嗚:)


上一篇
有推薦C++公認的linter嗎 & IT成功開跑XD & 永續
系列文
轉職仔之Data Science and ai master後的持續精進技術之路17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言