返回介绍

solution / 2900-2999 / 2999.Count the Number of Powerful Integers / README_EN

发布于 2024-06-17 01:02:58 字数 7717 浏览 0 评论 0 收藏 0

2999. Count the Number of Powerful Integers

中文文档

Description

You are given three integers start, finish, and limit. You are also given a 0-indexed string s representing a positive integer.

A positive integer x is called powerful if it ends with s (in other words, s is a suffix of x) and each digit in x is at most limit.

Return _the total number of powerful integers in the range_ [start..finish].

A string x is a suffix of a string y if and only if x is a substring of y that starts from some index (including 0) in y and extends to the index y.length - 1. For example, 25 is a suffix of 5125 whereas 512 is not.

 

Example 1:

Input: start = 1, finish = 6000, limit = 4, s = "124"
Output: 5
Explanation: The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
It can be shown that there are only 5 powerful integers in this range.

Example 2:

Input: start = 15, finish = 215, limit = 6, s = "10"
Output: 2
Explanation: The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.
It can be shown that there are only 2 powerful integers in this range.

Example 3:

Input: start = 1000, finish = 2000, limit = 4, s = "3000"
Output: 0
Explanation: All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.

 

Constraints:

  • 1 <= start <= finish <= 1015
  • 1 <= limit <= 9
  • 1 <= s.length <= floor(log10(finish)) + 1
  • s only consists of numeric digits which are at most limit.
  • s does not have leading zeros.

Solutions

Solution 1

class Solution:
  def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
    @cache
    def dfs(pos: int, lim: int):
      if len(t) < n:
        return 0
      if len(t) - pos == n:
        return int(s <= t[pos:]) if lim else 1
      up = min(int(t[pos]) if lim else 9, limit)
      ans = 0
      for i in range(up + 1):
        ans += dfs(pos + 1, lim and i == int(t[pos]))
      return ans

    n = len(s)
    t = str(start - 1)
    a = dfs(0, True)
    dfs.cache_clear()
    t = str(finish)
    b = dfs(0, True)
    return b - a
class Solution {
  private String s;
  private String t;
  private Long[] f;
  private int limit;

  public long numberOfPowerfulInt(long start, long finish, int limit, String s) {
    this.s = s;
    this.limit = limit;
    t = String.valueOf(start - 1);
    f = new Long[20];
    long a = dfs(0, true);
    t = String.valueOf(finish);
    f = new Long[20];
    long b = dfs(0, true);
    return b - a;
  }

  private long dfs(int pos, boolean lim) {
    if (t.length() < s.length()) {
      return 0;
    }
    if (!lim && f[pos] != null) {
      return f[pos];
    }
    if (t.length() - pos == s.length()) {
      return lim ? (s.compareTo(t.substring(pos)) <= 0 ? 1 : 0) : 1;
    }
    int up = lim ? t.charAt(pos) - '0' : 9;
    up = Math.min(up, limit);
    long ans = 0;
    for (int i = 0; i <= up; ++i) {
      ans += dfs(pos + 1, lim && i == (t.charAt(pos) - '0'));
    }
    if (!lim) {
      f[pos] = ans;
    }
    return ans;
  }
}
class Solution {
public:
  long long numberOfPowerfulInt(long long start, long long finish, int limit, string s) {
    string t = to_string(start - 1);
    long long f[20];
    memset(f, -1, sizeof(f));

    function<long long(int, bool)> dfs = [&](int pos, bool lim) -> long long {
      if (t.size() < s.size()) {
        return 0;
      }
      if (!lim && f[pos] != -1) {
        return f[pos];
      }
      if (t.size() - pos == s.size()) {
        return lim ? s <= t.substr(pos) : 1;
      }
      long long ans = 0;
      int up = min(lim ? t[pos] - '0' : 9, limit);
      for (int i = 0; i <= up; ++i) {
        ans += dfs(pos + 1, lim && i == (t[pos] - '0'));
      }
      if (!lim) {
        f[pos] = ans;
      }
      return ans;
    };

    long long a = dfs(0, true);
    t = to_string(finish);
    memset(f, -1, sizeof(f));
    long long b = dfs(0, true);
    return b - a;
  }
};
func numberOfPowerfulInt(start, finish int64, limit int, s string) int64 {
  t := strconv.FormatInt(start-1, 10)
  f := make([]int64, 20)
  for i := range f {
    f[i] = -1
  }

  var dfs func(int, bool) int64
  dfs = func(pos int, lim bool) int64 {
    if len(t) < len(s) {
      return 0
    }
    if !lim && f[pos] != -1 {
      return f[pos]
    }
    if len(t)-pos == len(s) {
      if lim {
        if s <= t[pos:] {
          return 1
        }
        return 0
      }
      return 1
    }

    ans := int64(0)
    up := 9
    if lim {
      up = int(t[pos] - '0')
    }
    up = min(up, limit)
    for i := 0; i <= up; i++ {
      ans += dfs(pos+1, lim && i == int(t[pos]-'0'))
    }
    if !lim {
      f[pos] = ans
    }
    return ans
  }

  a := dfs(0, true)
  t = strconv.FormatInt(finish, 10)
  for i := range f {
    f[i] = -1
  }
  b := dfs(0, true)
  return b - a
}
function numberOfPowerfulInt(start: number, finish: number, limit: number, s: string): number {
  let t: string = (start - 1).toString();
  let f: number[] = Array(20).fill(-1);

  const dfs = (pos: number, lim: boolean): number => {
    if (t.length < s.length) {
      return 0;
    }
    if (!lim && f[pos] !== -1) {
      return f[pos];
    }
    if (t.length - pos === s.length) {
      if (lim) {
        return s <= t.substring(pos) ? 1 : 0;
      }
      return 1;
    }

    let ans: number = 0;
    const up: number = Math.min(lim ? +t[pos] : 9, limit);
    for (let i = 0; i <= up; i++) {
      ans += dfs(pos + 1, lim && i === +t[pos]);
    }

    if (!lim) {
      f[pos] = ans;
    }
    return ans;
  };

  const a: number = dfs(0, true);
  t = finish.toString();
  f = Array(20).fill(-1);
  const b: number = dfs(0, true);

  return b - a;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文