iT邦幫忙

2025 iThome 鐵人賽

0

https://ithelp.ithome.com.tw/upload/images/20251221/20177944o5ZMaua5Qy.jpg

https://ithelp.ithome.com.tw/upload/images/20251221/20177944hurVuO7HPy.jpg

https://ithelp.ithome.com.tw/upload/images/20251221/20177944EvDC4jflkQ.jpg

https://ithelp.ithome.com.tw/upload/images/20251221/20177944ycanuBca6D.jpg

https://ithelp.ithome.com.tw/upload/images/20251221/201779444l7tdcNukU.jpg

https://ithelp.ithome.com.tw/upload/images/20251221/20177944wgBVk5V689.jpg

#include <vector>
using namespace std;
class Solution{
public: // 410. O(log(R-L+1) * n), O(1)
    int splitArray(vector<int>& a, int k){
        const int n = (int)a.size();
        int L = 0, R = 0;
        for (int i = 0; i < n; ++i){
            if(a[i] > L) L = a[i];
            R += a[i];
        }
        auto ok = [&](int c){
            int need = 1, cur = 0;
            for(int i = 0; i < n; ++i){
                if(cur + a[i] > c){
                    if(++need > k) return false;
                    cur = a[i];
                } else {
                    cur += a[i];
                }
            }
            return true;
        };
        while (L < R){
            int m = L + (R - L) / 2;
            if (ok(m)) R = m;
            else L = m + 1;
        }
        return L;
    }
};

上一篇
I have memorized 1011 感謝賣了5本筆記與年末小分享(? 空了1.6個月 ; 3m 3 models v2ing 也見證了神一般的操作 respect 嚇屬寶寶
系列文
轉職仔之Data Science and ai master後的持續精進技術之路35
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言