当前位置:网站首页>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
}
}
边栏推荐
- 【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
- Database execution error: SQL_ mode only_ full_ group_ by:
- LeetCode每日一题(1362. Closest Divisors)
- 【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
- PIP configuring domestic sources
- 【Kotlin学习】类、对象和接口——定义类继承结构
- 【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
- PolyWorks script development learning notes (4) - data import and alignment using file import
- Overview of image restoration methods -- paper notes
- LeetCode每日一题(1162. As Far from Land as Possible)
猜你喜欢

Flink学习笔记(八)多流转换

Alibaba cloud notes for the first time

Learning C language from scratch -- installation and configuration of 01 MinGW

PolyWorks script development learning notes (II) -treeview basic operations

Global KYC service provider advance AI in vivo detection products have passed ISO international safety certification, and the product capability has reached a new level

Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)

Go language - JSON processing
![[CSDN]C1训练题解析_第三部分_JS基础](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN]C1训练题解析_第三部分_JS基础

Hudi quick experience (including detailed operation steps and screenshots)
![[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis](/img/c6/5f723d9021cf684dcfb662ed3acaec.png)
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
随机推荐
Win10 install elk
PIP configuring domestic sources
Windows安装Redis详细步骤
Logstash+jdbc data synchronization +head display problems
Jenkins learning (III) -- setting scheduled tasks
从0开始使用pnpm构建一个Monorepo方式管理的demo
Run flash demo on ECS
Jestson Nano 从tftp服务器下载更新kernel和dtb
Hudi 数据管理和存储概述
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
Spark overview
Long类型的相等判断
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Overview of image restoration methods -- paper notes
Hudi quick experience (including detailed operation steps and screenshots)
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
Flask+supervisor installation realizes background process resident
Spark 集群安装与部署
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
Filter comments to filter out uncommented and default values