1011-1020

1011. Capacity To Ship Packages Within D Days $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
 public:
  int shipWithinDays(vector<int>& weights, int D) {
    int l = *max_element(weights.begin(), weights.end());
    int r = accumulate(weights.begin(), weights.end(), 0);

    while (l < r) {
      int m = l + (r - l) / 2;
      int day = 1;
      int capacity = 0;
      for (int weight : weights) {
        if (capacity + weight > m) {
          ++day;
          capacity = weight;
        } else
          capacity += weight;
      }
      if (day <= D)
        r = m;
      else
        l = m + 1;
    }

    return l;
  }
};

1012. Numbers With Repeated Digits $\star\star\star$

1013. Partition Array Into Three Parts With Equal Sum $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  bool canThreePartsEqualSum(vector<int>& A) {
    int sum = accumulate(A.begin(), A.end(), 0);
    int presum = 0;
    int parts = 1;

    for (int a : A) {
      presum += a;
      if (presum == sum * parts / 3) ++parts;
    }

    return sum % 3 == 0 && parts >= 3;
  }
};

1014. Best Sightseeing Pair $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int maxScoreSightseeingPair(vector<int>& A) {
    int ans = 0;
    int bestPrev = 0;

    for (int a : A) {
      ans = max(ans, a + bestPrev);
      bestPrev = max(bestPrev, a) - 1;
    }

    return ans;
  }
};

1015. Smallest Integer Divisible by K $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  int smallestRepunitDivByK(int K) {
    if (K % 10 != 1 && K % 10 != 3 && K % 10 != 7 && K % 10 != 9) return -1;

    unordered_set<int> set;
    int mod = 0;

    for (int N = 1; N <= K; ++N) {
      mod = (mod * 10 + 1) % K;
      if (mod == 0) return N;
      if (set.count(mod)) return -1;
      set.insert(mod);
    }

    return -1;
  }
};

1016. Binary String With Substrings Representing 1 To N $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  bool queryString(string S, int N) {
    if (N > 1511) return false;

    for (int i = N; i > N / 2; --i) {
      string binary = bitset<32>(i).to_string();
      binary = binary.substr(binary.find("1"));
      if (S.find(binary) == string::npos) return false;
    }

    return true;
  }
};

1017. Convert to Base -2 $\star\star$

1018. Binary Prefix Divisible By 5 $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  vector<bool> prefixesDivBy5(vector<int>& A) {
    vector<bool> ans;
    int num = 0;

    for (int a : A) {
      num = (num * 2 + a) % 5;
      ans.push_back(num % 5 == 0);
    }

    return ans;
  }
};

1019. Next Greater Node In Linked List $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
 public:
  vector<int> nextLargerNodes(ListNode* head) {
    vector<int> ans;
    vector<int> stack;

    for (auto curr = head; curr; curr = curr->next) {
      while (stack.size() && ans[stack.back()] < curr->val) {
        ans[stack.back()] = curr->val;
        stack.pop_back();
      }
      stack.push_back(ans.size());
      ans.push_back(curr->val);
    }

    for (int i : stack) ans[i] = 0;

    return ans;
  }
};

1020. Number of Enclaves $\star\star$