📜  小于Y的最小集合数

📅  最后修改于: 2021-04-24 14:44:57             🧑  作者: 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


输出:
3