leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2305.cpp (834B)


      1 class Solution {
      2     vector<int> children = vector(8, 0);
      3     int res = INT_MAX;
      4 
      5     void rec(const vector<int> &cookies, vector<int> &children, int k, int cur, int zero, int maxi = 0) {
      6         if (cookies.size() - cur < zero) return;
      7         if (cur >= cookies.size()) {
      8             res = min(res, maxi);
      9             return;
     10         }
     11 
     12         for (int i = 0; i < k; i++) {
     13             zero -= children[i] == 0 ? 1 : 0;
     14             children[i] += cookies[cur];
     15             rec(cookies, children, k, cur + 1, zero, max(maxi, children[i]));
     16             children[i] -= cookies[cur];
     17             zero += children[i] == 0 ? 1 : 0;
     18         }
     19     }
     20 
     21   public:
     22     int distributeCookies(const vector<int> &cookies, int k) {
     23         vector<int> children = vector(k, 0);
     24         rec(cookies, children, k, 0, k);
     25         return res;
     26     }
     27 };