首页 > 其他分享 >[LeetCode] 2064. Minimized Maximum of Products Distributed to Any Store

[LeetCode] 2064. Minimized Maximum of Products Distributed to Any Store

时间:2024-11-14 13:41:59浏览次数:1  
标签:given 商店 quantities Distributed mid Maximum Minimized products type

You are given an integer n indicating there are n specialty retail stores. There are m product types of varying amounts, which are given as a 0-indexed integer array quantities, where quantities[i] represents the number of products of the ith product type.

You need to distribute all products to the retail stores following these rules:
A store can only be given at most one product type but can be given any amount of it.
After distribution, each store will have been given some number of products (possibly 0). Let x represent the maximum number of products given to any store. You want x to be as small as possible, i.e., you want to minimize the maximum number of products that are given to any store.
Return the minimum possible x.

Example 1:
Input: n = 6, quantities = [11,6]
Output: 3
Explanation: One optimal way is:

  • The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
  • The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
    The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.

Example 2:
Input: n = 7, quantities = [15,10,10]
Output: 5
Explanation: One optimal way is:

  • The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
  • The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
  • The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
    The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.

Example 3:
Input: n = 1, quantities = [100000]
Output: 100000
Explanation: The only optimal way is:

  • The 100000 products of type 0 are distributed to the only store.
    The maximum number of products given to any store is max(100000) = 100000.

Constraints:
m == quantities.length
1 <= m <= n <= 105
1 <= quantities[i] <= 105

分配给商店的最多商品的最小值。

给你一个整数 n ,表示有 n 间零售商店。总共有 m 种产品,每种产品的数目用一个下标从 0 开始的整数数组 quantities 表示,其中 quantities[i] 表示第 i 种商品的数目。

你需要将 所有商品 分配到零售商店,并遵守这些规则:

  • 一间商店 至多 只能有 一种商品 ,但一间商店拥有的商品数目可以为 任意 件。
  • 分配后,每间商店都会被分配一定数目的商品(可能为 0 件)。用 x 表示所有商店中分配商品数目的最大值,你希望 x 越小越好。也就是说,你想 最小化 分配给任意商店商品数目的 最大值 。

请你返回最小的可能的 x 。

思路

思路是二分法,而且是有点类似875题那种在答案上二分的题目。题目是请我们把 m 种商品分配到 n 个商店里且需要尽量让每个商店分配商品数目的最大值越小越好。所以我们在做二分找 mid 的时候,mid 的意思是每个商店需要放多少件商品。如果按照 mid 的个数分配商品需要的商店个数 > n,则把 mid 改小;反之则把 mid 改大。

注意代码中 count 的计算方式,因为是计算需要的商店个数所以需要向上取整。

复杂度

时间O(nlogn)
空间O(1)

代码

Java实现

class Solution {
    public int minimizedMaximum(int n, int[] quantities) {
        int left = 1;
        int right = 100000;
        while (left < right) {
            int mid = left + (right - left) / 2;
            int count = 0;
            for (int q : quantities) {
                // count += (q + mid - 1) / mid;
                count += q / mid;
                if (q % mid != 0) {
                    count++;
                }
            }
            if (count > n) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }
}

相关题目

410. Split Array Largest Sum
774. Minimize Max Distance to Gas Station
875. Koko Eating Bananas
1011. Capacity To Ship Packages In N Days
1060. Missing Element in Sorted Array
1231. Divide Chocolate
1283. Find the Smallest Divisor Given a Threshold
1482. Minimum Number of Days to Make m Bouquets
1539. Kth Missing Positive Number
1870. Minimum Speed to Arrive on Time
2064. Minimized Maximum of Products Distributed to Any Store

标签:given,商店,quantities,Distributed,mid,Maximum,Minimized,products,type
From: https://www.cnblogs.com/cnoodle/p/18545808

相关文章

  • Principles of Distributed Ledgers
    PrinciplesofDistributedLedgers:CourseworkObjectiveTheobjectiveofthiscourseworkistodevelopaSoliditycontract,HumanResources,whichimplementsahumanresources(HR)paymentsystemandtodeployitonOptimism.Thiscontractwillenableahum......
  • 在 Windows 系统中,DFS (Distributed File System) 是一种用于文件共享和管理的技术,能
    在Windows系统中,DFS(DistributedFileSystem)是一种用于文件共享和管理的技术,能够让多个服务器上的共享文件夹(共享资源)通过一个统一的命名空间来访问。DFS主要通过DFS命名空间和DFS复制这两个组件来实现。DFS相关命令和功能在Windows中,DFS相关的命令通常是通过......
  • Educational Codeforces Round 144 (Rated for Div. 2) C. Maximum Set
    我们要选出最长的子序列,使得每一个数都是前一个数的倍数。因此自然我们可以想到选择最小值然后每次乘\(2\)。所以有\(l\times2^k\ler\),即\(k=\left\lfloor\log_2\frac{r}{l}\right\rfloor\)。所以最大的集合大小就是\(k+1\)。然后考虑最大的集合中最小值可能不同,我假设......
  • 一道题把我气笑了:) 力扣.53 最大子数组和 leetcode maximum-subarray
    数组系列力扣数据结构之数组-00-概览力扣.53最大子数组和maximum-subarray力扣.128最长连续系列longest-consecutive-sequence力扣.1两数之和N种解法two-sum力扣.167两数之和IItwo-sum-ii力扣.170两数之和IIItwo-sum-iii力扣.653两数之和IVtwo-sum-IV力......
  • [LeetCode] 3090. Maximum Length Substring With Two Occurrences
    Givenastrings,returnthemaximumlengthofasubstringsuchthatitcontainsatmosttwooccurrencesofeachcharacter.Example1:Input:s="bcbbbcba"Output:4Explanation:Thefollowingsubstringhasalengthof4andcontainsatmosttw......
  • [LeetCode] 2841. Maximum Sum of Almost Unique Subarray
    Youaregivenanintegerarraynumsandtwopositiveintegersmandk.Returnthemaximumsumoutofallalmostuniquesubarraysoflengthkofnums.Ifnosuchsubarrayexists,return0.Asubarrayofnumsisalmostuniqueifitcontainsatleastmdisti......
  • ECE6101/CSE6461  Distributed,  Independent random
    ECE6101/CSE6461Homework2AssignmentAutumn2024Expected Completion Date: October 11, 20241. Let {N1(t), t ≥ 0} be a Poisson Process with rate λ . Can Z(t) = αN1(t)+βN2 (t) ever bea Poisson Process, where α and β a......
  • CF1249F Maximum Weight Subset 题解 / 长链剖分复习
    CF1249FMaximumWeightSubset题解题目大意给定一个\(n\)个节点的树,每个节点有点权\(a_i\)。从中选出若干节点,要求这些点两两之间距离大于给定常数\(k\),使得点权和最大。Solve给出一种线性做法。前置知识:长链剖分优化DP。考虑一个DP:设\(f(u,d)\)表示在\(u\)的子......
  • 题解:Maximum AND
    ProblemLinkMaximumAND题外话用sort肘过去了?题面翻译给定数组\(a\)和\(b\),重排\(b\)数组,求\(a_i\oplusb_i\)之后与和的最大值。Solution不难想到按位贪心。从最高位开始,逐位讨论是否能为\(1\)。接下来有一个做法:先将\(a\)数组升序排序,\(b\)数组降序排......
  • Maximum Control (medium)
    算法转化题意,即为求树上最长不重链覆盖长链剖分显然可以使用长链剖分的思想,直接剖分后贪心的求最大链即可代码#include<bits/stdc++.h>usingnamespacestd;#definelllonglongconstintmaxn=1e5+5;intn,u,v,rt,d[maxn],son[maxn],h[maxn];inth......