当前位置:网站首页>The number of weak characters in the game (1996)
The number of weak characters in the game (1996)
2022-07-03 09:33:00 【wangjun861205】
You are playing a game that contains multiple characters, and each of the characters has two main properties: attack and defense. You are given a 2D integer array properties where properties[i] = [attacki, defensei] represents the properties of the ith character in the game.
A character is said to be weak if any other character has both attack and defense levels strictly greater than this character’s attack and defense levels. More formally, a character i is said to be weak if there exists another character j where attackj > attacki and defensej > defensei.
Return the number of weak characters.
Example 1:
Input: properties = [[5,5],[6,3],[3,6]]
Output: 0
Explanation: No character has strictly greater attack and defense than the other.
Example 2:
Input: properties = [[2,2],[3,3]]
Output: 1
Explanation: The first character is weak because the second character has a strictly greater attack and defense.
Example 3:
Input: properties = [[1,5],[10,4],[4,3]]
Output: 1
Explanation: The third character is weak because the second character has a strictly greater attack and defense.
Constraints:
- 2 <= properties.length <= 105
- properties[i].length == 2
- 1 <= attacki, defensei <= 105
- according to attack Sort , If attack If the same, press defense In reverse order
- Reverse traversal , Time record the largest traversal defense value , If properties[i][1] < max(defense) Think properties[i] Weaker than a convenient one property
The reason attack Press if the same defense In reverse order , Because the title requires attack and defense Are strictly less than can be considered weak , We can guarantee attack Is not strictly incremental , Press defense Reverse sorting can ensure that mistakes can be avoided when traversing attack The same situation is included in the answer
impl Solution {
pub fn number_of_weak_characters(mut properties: Vec<Vec<i32>>) -> i32 {
properties.sort_by(|v1, v2| {
if v1[0] == v2[0] {
return v2[1].cmp(&v1[1]);
}
v1[0].cmp(&v2[0])
});
let mut max = properties.last().unwrap()[1];
let mut ans = 0;
for v in properties.into_iter().rev().skip(1) {
if v[1] < max {
ans += 1;
}
max = max.max(v[1]);
}
ans
}
}
边栏推荐
- Alibaba cloud notes for the first time
- Jestson nano downloads updated kernel and DTB from TFTP server
- Failed building wheel for argon2 cffi when installing Jupiter
- LeetCode每日一题(1996. The Number of Weak Characters in the Game)
- [point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
- 【Kotlin学习】类、对象和接口——定义类继承结构
- 专利查询网站
- Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
- Explanation of the answers to the three questions
- 从0开始使用pnpm构建一个Monorepo方式管理的demo
猜你喜欢
Modify idea code
Utilisation de hudi dans idea
The rise and fall of mobile phones in my perspective these 10 years
Solve editor MD uploads pictures and cannot get the picture address
Please tell me how to set vscode
Redis learning (I)
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
Spark cluster installation and deployment
随机推荐
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
1922. Count Good Numbers
Hudi integrated spark data analysis example (including code flow and test results)
Go language - Reflection
Spark 集群安装与部署
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
LeetCode每日一题(1024. Video Stitching)
Temper cattle ranking problem
Solve the problem of disordered code in vscode development, output Chinese and open source code
Common formulas of probability theory
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
制作jetson nano最基本的根文件系统、服务器挂载NFS文件系统
Construction and test of TFTP server under unbuntu (Debian)
LeetCode每日一题(516. Longest Palindromic Subsequence)
Banner - Summary of closed group meeting
PolyWorks script development learning notes (III) -treeview advanced operation
[kotlin learning] classes, objects and interfaces - define class inheritance structure
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip