产品销售-最小不同id

该问题涉及一个销售人员需要销售带有随机ID的商品,允许最多删除一定数量的商品。目标是确定在执行最多m次删除后,包内能包含的最少不同ID的数量。给定商品ID列表和最大删除次数,函数deleteProducts计算并返回这个最小数量。示例展示了如何在不同情况下找到最小的不同ID数。
销售人员必须使用随机 ID 在袋子中销售 n 件商品。销售人员可以从袋子中取出多达 m 件物品。确定最终包在执行最多 m 个删除后可以包含的最小不同 ID 数。

A salesperson must sell n items in a bag with random IDs. The salesperson can remove as many as m items from the bag. Determine the minimum number of different IDs the final bag can contain after performing, at most, m deletions.

Example

n = 6

ids = [1, 1, 1, 2, 3, 2]

m = 2

Two possible actions that give the minimum 2 different IDs:

  1. Remove 2 items with ID = 2 and the final bag will contain item ids' = [1, 1, 1, 3]

  2. Remove 1 item with ID = 2 and 1 item with ID=3 and the final bag will contain item ids' = [1, 1, 1, 2]

The minimum number of distinct IDs is 2.

Function Description

Complete the function deleteProducts in the editor below.

deleteProducts has the following parameters:

int ids[n]: an array of integers

int m: an integer, the maximum number of deletions

Returns:

int: an integer that represents the minimum number of item IDs

Constraints

  • 1 ≤ n ≤ 105

  • 1 ≤ ids[i] ≤ 106

  • 1 ≤ m ≤ 105

Input Format For Custom Testing

The first line contains an integer, n, that denotes the number of elements in ids. Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer ids[i].

The last line contains an integer, m, that denotes the maximum number of items that can be deleted.

​​

Sample Case 0

Sample Input

STDIN    Function
-----    -----
4     →  ids[] size n = 4
1     →  ids = [1, 1, 5, 5]
1
5
5
2     →  m = 2

Sample Output

1

Explanation

Two possible actions that give 1 as the minimum number of different IDs:

  1. Remove 2 items with ID = 1  and the final bag will contain item ids' = [5, 5]
  2. Remove 2 items with ID = 5  and the final bag will contain item ids' = [1, 1]

​​

Sample Case 1

Sample Input

STDIN    Function
-----    -----
7     →  ids[] size n = 7
1     →  ids = [1, 2, 3, 1, 2, 2, 1]
2
3
1
2
2
1
3    →   m = 3

Sample Output

2

Explanation

Three possible actions that give 2 as the minimum number of different IDs:

  1. Remove 3 items with ID = 1  and the final bag will contain item ids' = [2, 3, 2, 2]
  2. Remove 3 items with ID = 2  and the final bag will contain item ids' = [1, 3, 1, 1]
  3. Remove 1 item with ID = 3  and the final bag will contain item ids' = [1, 2, 1, 2, 2, 1]
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;



class Result {

    /*
     * Complete the 'deleteProducts' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY ids
     *  2. INTEGER m
     */

    public static int deleteProducts(List<Integer> ids, int m) {
        Map<Integer, Integer> map = new HashMap<>();
        for (Integer id : ids) {
            if (map.containsKey(id)) {
                map.put(id, map.get(id) + 1);
            } else {
                map.put(id, 1);
            }
        }

        List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet());

        list.sort(Comparator.comparingInt(Map.Entry::getValue));

        Map<Integer, Integer> lh = new LinkedHashMap<>();
        for (Map.Entry<Integer, Integer> e : list) {
            lh.put(e.getKey(), e.getValue());
        }

        for (Integer i : lh.keySet()) {
            if (map.get(i) <= m) {
                m -= map.get(i);
                map.remove(i);
            }
        }
        return map.size();
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int idsCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> ids = IntStream.range(0, idsCount).mapToObj(i -> {
            try {
                return bufferedReader.readLine().replaceAll("\\s+$", "");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .map(String::trim)
            .map(Integer::parseInt)
            .collect(toList());

        int m = Integer.parseInt(bufferedReader.readLine().trim());

        int result = Result.deleteProducts(ids, m);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

### ID3算法在机器学习中的应用 ID3(Iterative Dichotomiser 3)是一种经典的决策树算法,由Ross Quinlan于1986年提出。该算法的核心思想是通过最大化信息增益来选择最佳分裂属性[^1]。具体而言,ID3利用信息熵的概念计算每个特征的信息增益,并选取使信息增益最大的特征作为当前节点的最佳分割标准。 #### 信息熵与信息增益 信息熵是对数据集中不确定性程度的度量。对于一个具有多个类别的数据集,其信息熵定义如下: \[ H(D) = - \sum_{i=1}^{n} p_i \log_2(p_i) \] 其中 \(p_i\) 表示第\(i\)类样本所占的比例[^1]。当数据完全纯净时(即所有样本属于同一类别),信息熵为0;反之,如果各类别分布均匀,则信息熵达到最大值。 信息增益用于评估某一特定特征对减少数据集不确定性的贡献大小。给定特征A,其信息增益可表示为: \[ Gain(A) = H(D) - \sum_{v \in Values(A)} \frac{|D_v|}{|D|} H(D_v) \] 这里,\(Values(A)\)代表特征A的所有取值集合,而\(D_v\)则指代那些在特征A上等于某固定值的数据子集。 #### 应用场景——销售数据分析 在实际业务中,ID3算法可以被用来预测销售趋势或客户行为模式。例如,在零售行业中,企业可能会收集关于顾客购买习惯的各种变量,如年龄、收入水平、地理位置等。这些因素都可以视为潜在的分裂依据。通过对历史交易记录建立一棵基于ID3的决策树模型,可以帮助商家识别哪些组合条件更有可能促成高销售额或者吸引回头客[^2]。 然而需要注意的是,原始版本的ID3仅支持离散型输入特性,并且容易受到噪声干扰的影响。因此,在处理连续数值类型的字段之前需先将其转换成分类标签形式;另外还需采取措施防止过拟合现象发生,比如设置最小叶子结点数限制或是提前剪枝操作。 ```python from sklearn.tree import DecisionTreeClassifier import pandas as pd # 假设我们有一个简单的销售数据集 data = { 'Age': ['Young', 'Middle', 'Old'], 'Income': ['High', 'Medium', 'Low'], 'Buys': ['No', 'Yes', 'Yes'] } df = pd.DataFrame(data) X = df[['Age', 'Income']] y = df['Buys'] clf = DecisionTreeClassifier(criterion='entropy') # 使用信息熵作为准则 clf.fit(X, y) print(clf.predict([['Middle', 'Medium']])) # 预测新实例是否会购买商品 ``` 上面展示了一个非常基础的例子说明如何使用Python库`sklearn`里的函数快速搭建起一个以ID3理论为基础的学习器并完成简单任务演示。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值