当前位置:网站首页>Leetcode:857. Minimum cost of employing K workers [think in blocks + start with simplicity]
Leetcode:857. Minimum cost of employing K workers [think in blocks + start with simplicity]
2022-07-26 22:27:00 【White speed Dragon King's review】

analysis
Suppose you choose k The individual is x1 … xk
They correspond to q and w yes q1…qk;w1…wk
The actual income is r1…rk;
We know ri = qi * t(t It's the proportional coefficient )
Then there must be ri >= wi therefore t >= wi / qi
So the most greedy words t = max(wi / qi)
Special , We make t = [wage[i] / quality[i] for i in range(n)]
So we smoke k Come out alone
It is demand. sum(qi) * max(ti)
obviously max Easy to ask , We can t Make a sequence
Then when fixed max t after ,qi The scope is limited , In this case, use a sortedlist Before maintenance k Big ones can be done
ac code
from sortedcontainers import SortedList
class Solution:
def mincostToHireWorkers(self, quality: List[int], wage: List[int], k: int) -> float:
# Ratio of actual wages = The ratio of mass
# Real wages >= Ideal salary
# Minimum total salary
n = len(quality)
t = [wage[i] / quality[i] for i in range(n)]
# from q,t choose k One makes sum(qi) * max(ti) Minimum , Multivariable use fixed one idea
# max(ti) Good fixation
# sum(qi) Choose the one in front k Make and minimize ,sortedlist?
sorted_t_q = sorted([(tt, qq) for tt, qq in zip(t, quality)])
#print(sorted_t_q)
ans = inf
stl = SortedList([])
# init
for i in range(k):
stl.add(sorted_t_q[i][1])
ans = min(ans, sum(stl[:k]) * sorted_t_q[k - 1][0])
# next
for i in range(k, n):
stl.add(sorted_t_q[i][1])
ans = min(ans, sum(stl[:k]) * sorted_t_q[i][0])
return ans
summary
Two quantity problems are hard to find
Fix one first , Then ask for another
Generally speaking , Fix the simple one first
边栏推荐
- Understand China's financial system in one article
- C data type_ From rookie tutorial
- unity 安装失败:operation not permitted, mkdir......
- Makefile相关语法总结(Openc910)
- 挖财钱堂和启牛学堂哪个更好一些?是安全的吗
- What you need to know about mobile video compatibility
- DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
- The potential of emerging markets is unlimited, and advance.ai risk control products help Chinese offshore enterprises build a solid foundation for safe development
- Operating guidelines and suggestions for spot gold (Part 1)
- Alibaba three sides: how to solve the problems of MQ message loss, duplication and backlog?
猜你喜欢

Botu PLC output channel mapping FC

SQL injection less26 (filter spaces and comments, and use error injection without spaces)

Try new functions | decrypt Doris complex data type array

Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click

Implementation of MATLAB short-time autocorrelation

06 cp 命令

nvidia-smi报错:NVIDIA-SMI has failed because it couldn‘t communicate with the NVIDIA driver 完整记录

2018 arXiv preprint | MolGAN: An implicit generative model for small molecular graphs

A few pictures help you clarify "China's financial institution system"
![[horizon sunrise X3 sect trial experience] + unpacking post](/img/b1/3196b470e601dc63ce3b11a7deb3cd.png)
[horizon sunrise X3 sect trial experience] + unpacking post
随机推荐
Excel VBA quick start (XII. Common usage of like comparison)
Matlab pitch period estimation post-processing
【C语言基础】17 链表初探
Luo Yonghao bet on the last entrepreneurial credit
Alibaba three sides: how to solve the problems of MQ message loss, duplication and backlog?
Implementation of V-model syntax sugar
Development status of Dao
Leetcode 121: the best time to buy and sell stocks
Go -- go language naming specification
Operating guidelines and suggestions for spot gold (Part 1)
小白学习MySQL - Derived Table
What is Base64?
30000 hair loss people, supporting an IPO
Qt中为工程添加资源文件、给按钮添加图片
JS verify complex password
Do you know why to design test cases after learning so long about use case design
Interview Essentials
Actual authority comes from information superiority
08 Du command
The PRI that Hillhouse joined, including Junlian, Huakong, Shengshi, Lvdong and other 100 institutions, was killed