当前位置:网站首页>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
边栏推荐
- ZABBIX calls API retrieval method
- Leetcode 121: the best time to buy and sell stocks
- JS verify complex password
- 小白学习MySQL - Derived Table
- Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
- LeetCode 121:买卖股票的最佳时机
- 参数解析与跳石板
- EasyUI DataGrid obtains multiple selected data for operation
- [waiting and wakeup of QT multithreaded threads]
- Fujian vies for vc/pe
猜你喜欢

基于gRPC编写golang简单C2远控

IDEA的那些环境配置及插件

Understanding and practice of the trend of Bank of London foreign exchange

shell相关命令总结

A chip company fell in round B

Excel-vba quick start (X. prompt box, inputable pop-up box)

Financial institution map

Fujian vies for vc/pe

毕业5年,从信息管理转行软件测试工程师,我的月薪终于突破了12k

Makefile相关语法总结(Openc910)
随机推荐
Open source | arex Ctrip traffic playback practice without code intrusion
Proto basic grammar of protobuf
Five years after graduation, I changed from information management to software testing engineer, and my monthly salary finally exceeded 12K
软件测试技术之跨平台的移动端UI自动化测试(下)
View drawing process 1-the relationship between view and window
Overview of MPLS Basics
恋爱时各个顺序整理(历时两个月详细整理)
LeetCode 122:买卖股票的最佳时机 II
【Qt多线程之线程的等待和唤醒】
Get network time by unity
The combobox of easyUI selects the first option by default
【工具】Apifox
Classification of banking business
Implementation of V-model syntax sugar
参数解析与跳石板
The potential of emerging markets is unlimited, and advance.ai risk control products help Chinese offshore enterprises build a solid foundation for safe development
挖财钱堂和启牛学堂哪个更好一些?是安全的吗
JMeter custom log and log analysis
Let Xiaobai thoroughly understand performance tuning
: could not determine a constructor for the tag !RootAdmin