Leetcode 面试题 16.10. 生存人数
2022/1/18 23:40:41
本文主要是介绍Leetcode 面试题 16.10. 生存人数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
面试题 16.10. 生存人数
C++
class Solution { public: int maxAliveYear(vector<int>& birth, vector<int>& death) { int ans = 0, length = birth.size(); vector<int> cnt(105,0); for(int i = 0; i < length; i++){ cnt[birth[i] - 1900]++; cnt[death[i] - 1900 + 1]--; } int preSum = 0, maxLive = 0; for(int i = 0; i < 105; i++){ preSum += cnt[i]; // 前缀和/累加 if(preSum > maxLive){ maxLive = preSum; ans = i; } } return ans + 1900; } };
Python
class Solution: def maxAliveYear(self, birth: List[int], death: List[int]) -> int: birth_total = [0 for _ in range(105)] death_total = [0 for _ in range(105)] length = len(birth) # 记录这100年每年出生和死亡的人数 for i in range(length): birth_total[birth[i] - 1900] += 1 death_total[death[i] - 1900 + 1] += 1 # 记录这100年每年累计出生和累计死亡的人数 for i in range(1,101): birth_total[i] += birth_total[i - 1] death_total[i] += death_total[i - 1] live = 0 for i in range(101): #print(birth_total[i] - death_total[i], live, i + 1900) if birth_total[i] - death_total[i] > live: live = birth_total[i] - death_total[i] re = i + 1900 return re
我是傻逼…没好好审题,手动调试了1个小时,1909年死亡的人在1909年是计数的…
这篇关于Leetcode 面试题 16.10. 生存人数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升