max count是一个可能的最大计数。在这里,我们给出了一个整数n和一个整数m的字符串。我们的任务是使用整数m的数字来组成数字n,并返回最大计数。同时,我们可以将2和5视为同一个数字,将6和9视为同一个数字。
样例示例输入 1n = 29m = 2569783output 1: 2
解释 − 因为5和2相同,6和9相同,所以我们有两个'2'和两个'9'。因此,使用字符串m(2596783)的数字来组成数字n(29)的最大计数是2。
输入2n = 999m = 6666925output 2: 1
方法让我们逐步讨论下面的方法-
首先,我们将创建一个名为‘maxcountofn’的函数,该函数将以给定的字符串‘m’和数字‘n’作为参数,并将返回所需的整数‘maxcount’作为返回值。
在该函数中,我们将创建一个哈希映射 'mp' 来存储字符串 'm' 中每个数字的频率。
我们定义一个变量‘len’来存储字符串‘m’的大小。
从索引‘i = 0’开始遍历字符串‘m’,直到小于等于‘len’,并在此循环下执行以下操作:
如果我们得到的数字是‘2’,我们将其转换为‘5’。
如果我们得到一个数字为‘6’,我们将其转换为‘9’。
统计'mp'映射中每个数字的频率作为字符-整数对。
创建另一个哈希映射 'mpn' 以存储数字 n 的频率
使用while循环遍历数字‘n’,直到n大于0,并在此循环下执行以下操作 -
创建一个整数‘rem’来存储数字的最后一个元素
检查是否为2并将其转换为5
检查 rem 是否为 6 并将其转换为 9
将'mpn'映射中每个数字的频率计数为字符-整数对。即将整数作为字符存储在映射中,如'mpn[rem + '0']'。
将 n 减少到 n%10,以去除数字的最后一位
我们创建一个变量 'maxcount',其中存储 'int_max'。
最后,我们遍历地图 'mpn' 来找到 n 的最大计数,并在此循环下执行以下操作 -
在变量‘key’中存储数字的位数
检查键是否存在于字符串的映射中,如果不存在意味着我们无法使用字符串'm'的数字创建数字'n',我们返回'0'。
在变量'tempcount'中创建一个变量,我们将值存储在其中(将字符串m中的数字频率除以n的当前数字频率)。
在maxcount中,我们存储tempcount和maxcount的最小值,因为只有当数字'n'的每个数字都出现在字符串'm'中时,才可能生成数字'n'
return maxcount
example的中文翻译为:示例#include <bits/stdc++.h>using namespace std;int maxcountofn(int n, string m){ map< char, int >mp; //created hashmap to store the frequency of each digit of //string int len = m.size(); // getting the size of the string // iterating string using for loop for(int i=0;i<len;i++){ if(m[i] == '2'){ m[i] = '5'; // replace 2 with 5 } else if(m[i] == '6'){ m[i] = '9'; // replace 6 with 9 } mp[m[i]]++; //count frequency of digit of string } // creating another hashmap to store the frequency of digit of the number n map<char, int>mpn; // iterating number 'n' using while loop while(n > 0){ int rem = n % 10; // get the last digit as the remainder //replace 2 with 5 if(rem == 2){ rem = 5; } //replace 6 with 9 if(rem == 6){ rem = 9; } mpn[rem + '0']++; //count frequency of digit of number n = n / 10; } int maxcount = int_max; //trvaerse the hashmap of the number to get the maxcount for(auto el : mpn){ // get the key which is a digit from the number n to be formed int key = el.first; // if the key is not present in the string m, then the number n cannot be formed if (!mp.count(key)) return 0; // divide the frequency of the digit from the string m with the frequency of the current digit of n int tempcount = mp[key] / el.second; // choose the minimum maxcount = min(maxcount, tempcount); } // returning the maximum count return maxcount;}// main function int main(){ int n = 29; // given number string m = 2569783;// given string // calling the function to get a maximum count of n int maxcount = maxcountofn(n, m); cout<<the max count of making the number << n << using the digits of the string << m << is << maxcount<<endl; return 0;}
输出the max count of making the number 29 using the digits of the string 2569783 is 2
结论in this tutorial, we have implemented a program to find the max count of n using digits of m such that 2 and 5, and, 6 and 9 can be treated as the same respectively. we have implemented an approach of hashing as we have to store the frequency with the time complexity of o(n+m) and space complexity of o(n+m). where m is the size of the string and n is the size of the number.
以上就是使用m的数字,最大计数为n,其中2和5以及6和9可以互相视为相同的详细内容。