Skip to content

3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

  • Time: $O(\log 10^5)$
  • Space: $O(1)$
 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
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
 public:
  long long findMaximumNumber(long long k, int x) {
    long l = 1;
    long r = 1e15;

    while (l < r) {
      const long m = (l + r + 1) / 2;
      if (getSumPrices(m, x) > k)
        r = m - 1;
      else
        l = m;
    }

    return l;
  }

 private:
  // Returns the sum of prices of all numbers from 1 to `num`.
  long getSumPrices(long num, int x) {
    long sumPrices = 0;
    // Increment `num` to account the 0-th row in the count of groups.
    ++num;
    for (int i = leftmostColumnIndex(num); i > 0; --i)
      // If the current column is valid, count the number of 1s in this column.
      if (i % x == 0) {
        const long groupSize = 1L << i;
        const long halfGroupSize = 1L << i - 1;
        sumPrices += num / groupSize * halfGroupSize;
        sumPrices += max(0L, (num % groupSize) - halfGroupSize);
      }
    return sumPrices;
  }

  // Returns the leftmost column index in 1-indexed.
  int leftmostColumnIndex(long num) {
    return 63 - __builtin_clzl(num) + 1;
  }
};
 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
27
28
29
30
31
32
33
34
35
class Solution {
  public long findMaximumNumber(long k, int x) {
    long l = 1;
    long r = 1000000000000000L;

    while (l < r) {
      final long m = (l + r + 1) / 2;
      if (getSumPrices(m, x) > k)
        r = m - 1;
      else
        l = m;
    }

    return l;
  }

  // Returns the sum of prices of all numbers from 1 to `num`.
  private long getSumPrices(long num, int x) {
    long sumPrices = 0;
    num++; // Increment `num` to account the 0-th row in the count of groups.
    for (int i = leftmostColumnIndex(num); i > 0; --i)
      if (i % x == 0) {
        final long groupSize = 1L << i;
        final long halfGroupSize = 1L << i - 1;
        sumPrices += num / groupSize * halfGroupSize;
        sumPrices += Math.max(0L, (num % groupSize) - halfGroupSize);
      }
    return sumPrices;
  }

  // Returns the leftmost column index in 1-indexed.
  private int leftmostColumnIndex(long num) {
    return 63 - Long.numberOfLeadingZeros(num) + 1;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution:
  def findMaximumNumber(self, k: int, x: int) -> int:
    def getSumPrices(num: int) -> int:
      """Returns the sum of prices of all numbers from 1 to `num`."""
      sumPrices = 0
      # Increment `num` to account the 0-th row in the count of groups.
      num += 1
      for i in range(num.bit_length(), 0, -1):
        if i % x == 0:
          groupSize = 1 << i
          halfGroupSize = 1 << i - 1
          sumPrices += num // groupSize * halfGroupSize
          sumPrices += max(0, (num % groupSize) - halfGroupSize)
      return sumPrices

    return bisect.bisect_right(range(1, 10**15), k,
                               key=lambda m: getSumPrices(m))