iT邦幫忙

2025 iThome 鐵人賽

0
Software Development

轉職仔之Data Science and ai master後的持續精進技術之路系列 第 34

I have memorized 1011 感謝賣了5本筆記與年末小分享(? 空了1.6個月 ; 3m 3 models v2ing 也見證了神一般的操作 respect 嚇屬寶寶

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20251213/20177944eg9ZmByUdi.jpg

https://ithelp.ithome.com.tw/upload/images/20251213/201779444WOjoeM7Cl.jpg

https://ithelp.ithome.com.tw/upload/images/20251213/20177944RcizLQvnPM.jpg

https://ithelp.ithome.com.tw/upload/images/20251213/20177944MJxj6Gr7QQ.jpg

https://ithelp.ithome.com.tw/upload/images/20251213/20177944LAsPVrfJ2F.jpg

#include <vector> // 1011. O(N * Log(Sum(W))) O(1)
#include <numeric>
#include <algorithm>
using namespace std;

class Solution {
public:
    int shipWithinDays(vector<int>& w, int d) {
        const int n = (int)w.size();
        const int* a = w.data();

        int L = *max_element(w.begin(), w.end());
        int R = accumulate(w.begin(), w.end(), 0);

        auto ok = [&](int c) { // lambda
            int need = 1, cur = 0;
            for (int i = 0; i < n; ++i) {  // for
                if (cur + a[i] > c) {       // if
                    if (++need > d) return false;
                    cur = a[i];
                } else {                   // else
                    cur += a[i];
                } // else
            } // for
            return true;
        };  // lambda
        while (L < R) { // while
            int m = L + (R - L) / 2;
            if (ok(m)) R = m;
            else L = m + 1;
        } // while
        return L;
    }
};

上一篇
技術小書打槍重新整理中 結果發現三十天寫得好少題:"( 一次要補七題..
系列文
轉職仔之Data Science and ai master後的持續精進技術之路34
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言