当前位置:网站首页>LeetCode每日一题(1996. The Number of Weak Characters in the Game)
LeetCode每日一题(1996. The Number of Weak Characters in the Game)
2022-07-03 09:01: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
- 根据 attack 排序, 如果 attack 相同则按 defense 的倒序排序
- 反向遍历,时刻记录遍历过的最大的 defense 值, 如果 properties[i][1] < max(defense)则认为 properties[i]弱于前面便利过的某个 property
之所以 attack 相同时按 defense 的倒序排序, 是因为题目要求是 attack 和 defense 都严格小于才能算弱, 我们这样排序可以保证 attack 是不严格递增, 按 defense 倒序排序可以保证在遍历的时候可以避免误把 attack 相同的情况计入答案中
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
}
}
边栏推荐
- [point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
- Spark 概述
- npm install安装依赖包报错解决方法
- 【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature
- 2022-2-14 learning the imitation Niuke project - send email
- [point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
- STM32F103 can learning record
- Notes on numerical analysis (II): numerical solution of linear equations
- State compression DP acwing 291 Mondrian's dream
- [point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
猜你喜欢

Go language - Reflection

On February 14, 2022, learn the imitation Niuke project - develop the registration function

Utilisation de hudi dans idea

CATIA automation object architecture - detailed explanation of application objects (III) systemservice
![[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs](/img/0a/480f1d1eea6f2ecf84fd5aa96bd9fb.png)
[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs

We have a common name, XX Gong
![[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion](/img/76/b92fe4549cacba15c113993a07abb8.png)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion

Run flash demo on ECS

Recommend a low code open source project of yyds
![[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds](/img/ee/3286e76797a75c0f999c728fd2b555.png)
[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
随机推荐
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
Detailed steps of windows installation redis
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
Problems in the implementation of lenet
Solve POM in idea Comment top line problem in XML file
Integrated use of interlij idea and sonarqube
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
Hudi learning notes (III) analysis of core concepts
ERROR: certificate common name “*.” doesn’t match requested ho
Beego learning - JWT realizes user login and registration
Vscode编辑器右键没有Open In Default Browser选项
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
Simple use of MATLAB
With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode
Uc/os self-study from 0
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
[advanced feature learning on point clouds using multi resolution features and learning]
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks