iT邦幫忙

2025 iThome 鐵人賽

DAY 20
1

https://ithelp.ithome.com.tw/upload/images/20251004/20177944n4shzHq9vj.jpg

https://ithelp.ithome.com.tw/upload/images/20251004/20177944dtFYBtKz6e.jpg

https://ithelp.ithome.com.tw/upload/images/20251004/20177944rrkJPI2PrI.jpg

https://ithelp.ithome.com.tw/upload/images/20251004/20177944NfFImxiIRs.jpg

#include <vector>
using namespace std;

class Solution { // review O(N) O(1)
public:
    int totalFruit(vector<int>& f){
        int a = -1, b = -1, run = 0, cur = 0, ans = 0;
        for (int x : f) {
            cur = (x == a || x == b) ? cur + 1 : run + 1;
            run = (x == b) ? run + 1 : 1;
            if (x != b) { a = b; b = x; }
            ans = (ans > cur) ? ans : cur;
        }
        return ans;
    }
};

上一篇
無限九零四(第三次)40分鐘靜下心來理解很棒:)&得到第三枚小物 $$+u吧:'(
下一篇
Sliding Window 9 review & 是否能第一個發文了呢XD
系列文
轉職仔之Data Science and ai master後的持續精進技術之路21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

1
AndyAWD
iT邦新手 2 級 ‧ 2025-10-04 22:06:36

太棒啦,剩下三分之一

可以準備來倒數了 10.9.8.7.6.5.4.3.2.1

我要留言

立即登入留言