当前位置:网站首页>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
}
}
边栏推荐
- [set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
- LeetCode每日一题(2090. K Radius Subarray Averages)
- IDEA 中使用 Hudi
- 数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
- Go language - Reflection
- Flink学习笔记(十)Flink容错机制
- 全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
- Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
- 【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
- Navicat, MySQL export Er graph, er graph
猜你喜欢
Flink-CDC实践(含实操步骤与截图)
Solve the problem of disordered code in vscode development, output Chinese and open source code
Go language - JSON processing
Utilisation de hudi dans idea
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
[kotlin learning] classes, objects and interfaces - define class inheritance structure
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
Spark structured stream writing Hudi practice
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
npm install安装依赖包报错解决方法
随机推荐
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
Spark 概述
Beego learning - Tencent cloud upload pictures
unbuntu(debian)下TFTP服务器搭建及测试
Spark cluster installation and deployment
Esp32 at command does not respond
LeetCode每日一题(1162. As Far from Land as Possible)
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
基于opencv实现桌面图标识别
LeetCode每日一题(516. Longest Palindromic Subsequence)
Solve editor MD uploads pictures and cannot get the picture address
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
Numerical analysis notes (I): equation root
Win10安装ELK
[untitled] use of cmake
Call the contents of Excel cells opened at the same time - button line feed
Hudi integrated spark data analysis example (including code flow and test results)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template