📌  相关文章
📜  通过删除所有出现的单个数字来最小化给定数组的总和

📅  最后修改于: 2021-09-05 11:49:27             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是通过删除所有出现的单个数字来最小化总和。

例子:

方法:想法是将所有出现的每个可能的数字 ( [0, 9] ) 一一删除,并在删除每个数字后计算数组的总和。最后,找出这些总和中的最小值。请按照以下步骤解决问题:

  • 初始化一个变量,比如minSum ,以存储最小和和curSum来存储在删除所有出现的数字后获得的和。
  • 迭代范围[0, 9] 中的数字并执行以下操作:
    • 遍历数组arr[]并通过删除每个数字来检查最小和。
    • 从字符串除去数字后,转换的字符串返回一个整数,并将其添加到curSum。
    • 每次迭代后更新minSum的值。
  • 打印minSum的值作为所需答案。

下面是上述方法的实现:

C++
// C++ program for super ugly number
#include
using namespace std;
 
// Function to remove each digit
// from the given integer
int remove(int N, int digit)
{
 
    // Convert into string
    string strN = to_string(N);
 
    // Stores final string
    string ans = "";
 
    // Traverse the string
    for (char i:strN)
    {
        if ((i - '0') == digit)
        {
            continue;
        }
 
        // Append it to the
        // final string
        ans += i;
      }
   
    // Return integer value
    return stoi(ans);
}
 
// Function to find the minimum sum by
// removing occurences of each digit
void getMin(vector arr)
{
    int minSum = INT_MAX;
 
    // Iterate in range [0, 9]
    for (int i = 0; i < 10; i++)
    {
        int curSum = 0;
 
        // Traverse the array
        for (int num :arr)
            curSum += remove(num, i);
 
        // Update the minimum sum
        minSum = min(minSum, curSum);
      }
 
    // Print the minimized sum
    cout << minSum;
}
 
/* Driver program to test above functions */
int main()
{
  vector arr = {34, 23, 85, 93};
  getMin(arr);
  return 0;
}
 
// This code is contributed by mohit kumar 29.


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to remove each digit
// from the given integer
static int remove(int N, int digit)
{
 
    // Convert into string
    String strN = String.valueOf(N);
 
    // Stores final string
    String ans = "";
     
    // Traverse the string
    for (char i:strN.toCharArray())
    {
        if ((i - '0') == digit)
        {
            continue;
        }
 
        // Append it to the
        // final string
        ans += i;
      }
   
    // Return integer value
    return Integer.parseInt(ans);
}
 
// Function to find the minimum sum by
// removing occurences of each digit
static void getMin(int[] arr)
{
    int minSum = Integer.MAX_VALUE;
 
    // Iterate in range [0, 9]
    for (int i = 0; i < 10; i++)
    {
        int curSum = 0;
 
        // Traverse the array
        for (int num :arr)
            curSum += remove(num, i);
 
        // Update the minimum sum
        minSum = Math.min(minSum, curSum);
      }
 
    // Print the minimized sum
    System.out.print(minSum);
}
 
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = {34, 23, 85, 93};
    getMin(arr);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
 
# Function to remove each digit
# from the given integer
def remove(N, digit):
 
    # Convert into string
    strN = str(N)
 
    # Stores final string
    ans = ''
 
    # Traverse the string
    for i in strN:
        if int(i) == digit:
            continue
 
        # Append it to the
        # final string
        ans += i
 
    # Return integer value
    return int(ans)
 
# Function to find the minimum sum by
# removing occurences of each digit
def getMin(arr):
    minSum = float('inf')
 
    # Iterate in range [0, 9]
    for i in range(10):
        curSum = 0
 
        # Traverse the array
        for num in arr:
            curSum += remove(num, i)
 
        # Update the minimum sum
        minSum = min(minSum, curSum)
 
    # Print the minimized sum
    print(minSum)
 
 
# Given array
arr = [34, 23, 85, 93]
getMin(arr)


C#
using System;
public class GFG
{
 
  // Function to remove each digit
  // from the given integer
  static int remove(int N, int digit)
  {
 
    // Convert into string
    String strN = N.ToString();
 
    // Stores final string
    String ans = "";
 
    // Traverse the string
    foreach(char i in strN.ToCharArray())
    {
      if ((i - '0') == digit)
      {
        continue;
      }
 
      // Append it to the
      // final string
      ans += i;
    }
 
    // Return integer value
    return Int32.Parse(ans);
  }
 
  // Function to find the minimum sum by
  // removing occurences of each digit
  static void getMin(int[] arr)
  {
    int minSum = Int32.MaxValue;
 
    // Iterate in range [0, 9]
    for (int i = 0; i < 10; i++)
    {
      int curSum = 0;
 
      // Traverse the array
      foreach(int num in arr)
        curSum += remove(num, i);
 
      // Update the minimum sum
      minSum = Math.Min(minSum, curSum);
    }
 
    // Print the minimized sum
    Console.WriteLine(minSum);
  }
 
  // Driver Code
  static public void Main (){
 
    int[] arr = {34, 23, 85, 93};
    getMin(arr);
  }
}
 
// This code is contributed by Dharanendra L V.


Javascript


输出:
100

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live