首页 > 其他分享 >Selling Products

Selling Products

时间:2023-04-11 23:46:50浏览次数:50  
标签:map Selling java bag ids item Products import

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;



public 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) {
        // Write your code here
        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);
            }
        }

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

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

        // Creating new map after sorting and also
        // maintaining insertion order
        Map<Integer, Integer> lh = new LinkedHashMap<>();
        for (Map.Entry<Integer, Integer> e : list) {
            lh.put(e.getKey(), e.getValue());
        }

        for (Integer i : lh.keySet()) {
            // removing element from whose frequency count is
            // less than m ,Sorted manner to get minimum
            // distinct ids
            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();
    }
}

标签:map,Selling,java,bag,ids,item,Products,import
From: https://www.cnblogs.com/createman/p/17308300.html

相关文章

  • 【题解】CF808E Selling Souvenirs
    题意多重背包,但数据范围很大并且体积小于等于三。思路乱搞。很自然地考虑将物品按照体积分成三类。显然对于同一类的物品从最大开始取最优,那么有一个贪心的想法。直......
  • 题解 UVA12265【贩卖土地 Selling Land】
    postedon2022-09-2414:33:29|under题解|sourceproblem一个黑白矩阵,求以每个点为右下角,能围出的周长最大的全白矩形的周长。\(n\leq2000\)。solution试图进行......
  • E. Selling Souvenirs 不会做
    ​​http://codeforces.com/contest/808/problem/E​​不理解为什么dp={cost,cnt1,cnt2}可以而dp={cost,cnt1,cnt2,cnt3}不可以上面那个不可以的例子是:  但是这......
  • OGL-Level6-Unit5-Selling Your Things
    TodayisSaturday,August27,2022.MynameisLuke,andIwillbeyourteachertoday.IamoriginallyfromBostonintheUnitedStatesbutcurrentlyliveinThail......
  • Today's topic-selling your thins
    GroupLessonTodayisSaturday,August27,2022Welcometoourclass!MynameisLuke,andIwillbeyourteachertoday.IamoriginallyfromBostonintheUni......