📜  数字小于 Y 的最小集合数

📅  最后修改于: 2021-10-25 11:21:08             🧑  作者: Mango

给定字符串连续数字和一个数字 Y,任务是找到最小集合的数量,使得每个集合都遵循以下规则:

  1. 集合应包含连续数字
  2. 任何数字都不能多次使用。
  3. 集合中的数量不应超过 Y。

例子:

Input: s = "1234", Y = 30  
Output: 3
Three sets of {12, 3, 4}  

Input: s = "1234", Y = 4 
Output: 4
Four sets of {1}, {2}, {3}, {4} 

方法:可以使用贪心方法解决以下问题,其步骤如下:

  • 在字符串迭代,并使用 num*10 + (s[i]-‘0’) 将数字连接到一个变量(比如 num)
  • 如果数字不大于 Y,则标记 f = 1。
  • 如果数量超过 Y,则在 f = 1 时增加计数并将 f 重新初始化为 0,并将 num 初始化为 s[i]-‘0’ 或 num 为 0。
  • 在字符串完全迭代后,如果 f 为 1,则增加计数。

下面是上述方法的实现:

C++
// C++ program to find the minimum number
// sets with consecutive numbers and less than Y
#include 
using namespace std;
 
// Function to find the minimum number of shets
int minimumSets(string s, int y)
{
 
    // Variable to count
    // the number of sets
    int cnt = 0;
    int num = 0;
 
    int l = s.length();
    int f = 0;
 
    // Iterate in the string
    for (int i = 0; i < l; i++) {
 
        // Add the number to string
        num = num * 10 + (s[i] - '0');
 
        // Mark that we got a number
        if (num <= y)
            f = 1;
        else // Every time it exceeds
        {
 
            // Check if previous was
            // anytime  less than Y
            if (f)
                cnt += 1;
 
            // Current number
            num = s[i] - '0';
            f = 0;
 
            // Check for current number
            if (num <= y)
                f = 1;
            else
                num = 0;
        }
    }
 
    // Check for last added number
    if (f)
        cnt += 1;
 
    return cnt;
}
 
// Driver Code
int main()
{
    string s = "1234";
    int y = 30;
    cout << minimumSets(s, y);
 
 return 0;
}


Java
// Java program to find the minimum number
// sets with consecutive numbers and less than Y
import java.util.*;
 
class solution
{
 
// Function to find the minimum number of shets
static int minimumSets(String s, int y)
{
 
    // Variable to count
    // the number of sets
    int cnt = 0;
    int num = 0;
 
    int l = s.length();
    boolean f = false;
 
    // Iterate in the string
    for (int i = 0; i < l; i++) {
 
        // Add the number to string
        num = num * 10 + (s.charAt(i) - '0');
 
        // Mark that we got a number
        if (num <= y)
            f = true;
        else // Every time it exceeds
        {
 
            // Check if previous was
            // anytime less than Y
            if (f)
                cnt += 1;
 
            // Current number
            num = s.charAt(i) - '0';
            f = false;
 
            // Check for current number
            if (num <= y)
                f = true;
            else
                num = 0;
        }
    }
 
    // Check for last added number
    if (f == true)
        cnt += 1;
 
    return cnt;
}
 
// Driver Code
public static void main(String args[])
{
    String s = "1234";
    int y = 30;
    System.out.println(minimumSets(s, y));
}
}
// This code is contributed by
// Shashank_Sharma


Python3
# Python3 program to find the minimum number
# sets with consecutive numbers and less than Y
import math as mt
 
# Function to find the minimum number of shets
def minimumSets(s, y):
     
    # Variable to count the number of sets
    cnt = 0
    num = 0
 
    l = len(s)
    f = 0
 
    # Iterate in the string
    for i in range(l):
         
        # Add the number to string
        num = num * 10 + (ord(s[i]) - ord('0'))
 
        # Mark that we got a number
        if (num <= y):
            f = 1
        else: # Every time it exceeds
 
            # Check if previous was anytime
            # less than Y
            if (f):
                cnt += 1
 
            # Current number
            num = ord(s[i]) - ord('0')
            f = 0
 
            # Check for current number
            if (num <= y):
                f = 1
            else:
                num = 0
         
    # Check for last added number
    if (f):
        cnt += 1
 
    return cnt
 
# Driver Code
s = "1234"
y = 30
print(minimumSets(s, y))
 
# This code is contributed by
# Mohit kumar 29


C#
// C# program to find the minimum number
// sets with consecutive numbers and less than Y
 
using System;
 
class solution
{
 
// Function to find the minimum number of shets
static int minimumSets(string s, int y)
{
 
    // Variable to count
    // the number of sets
    int cnt = 0;
    int num = 0;
 
    int l = s.Length ;
    bool f = false;
 
    // Iterate in the string
    for (int i = 0; i < l; i++) {
 
        // Add the number to string
        num = num * 10 + (s[i] - '0');
 
        // Mark that we got a number
        if (num <= y)
            f = true;
        else // Every time it exceeds
        {
 
            // Check if previous was
            // anytime less than Y
            if (f)
                cnt += 1;
 
            // Current number
            num = s[i] - '0';
            f = false;
 
            // Check for current number
            if (num <= y)
                f = true;
            else
                num = 0;
        }
    }
 
    // Check for last added number
    if (f == true)
        cnt += 1;
 
    return cnt;
}
 
// Driver Code
public static void Main()
{
    string s = "1234";
    int y = 30;
     
    Console.WriteLine(minimumSets(s, y));
}
// This code is contributed by Ryuga
 
}


PHP


Javascript


输出:
3