-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1996
No.1996.The Number of Weak Characters in the Game
- Loading branch information
Showing
6 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
solution/1900-1999/1996.The Number of Weak Characters in the Game/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
public: | ||
int numberOfWeakCharacters(vector<vector<int>> &properties) { | ||
sort(properties.begin(), properties.end(), [&](vector<int> &a, vector<int> &b) | ||
{ return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; }); | ||
int ans = 0, mx = 0; | ||
for (auto &p : properties) | ||
{ | ||
if (mx > p[1]) ++ans; | ||
else mx = p[1]; | ||
} | ||
return ans; | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
17
solution/1900-1999/1996.The Number of Weak Characters in the Game/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
func numberOfWeakCharacters(properties [][]int) int { | ||
sort.Slice(properties, func(i, j int) bool { | ||
if properties[i][0] == properties[j][0] { | ||
return properties[i][1] < properties[j][1] | ||
} | ||
return properties[i][0] > properties[j][0] | ||
}) | ||
ans, mx := 0, 0 | ||
for _, p := range properties { | ||
if mx > p[1] { | ||
ans++ | ||
} else { | ||
mx = p[1] | ||
} | ||
} | ||
return ans | ||
} |
15 changes: 15 additions & 0 deletions
15
solution/1900-1999/1996.The Number of Weak Characters in the Game/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public int numberOfWeakCharacters(int[][] properties) { | ||
Arrays.sort(properties, (a, b) -> { | ||
return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]; | ||
}); | ||
int ans = 0, mx = 0; | ||
for (int[] p : properties) { | ||
if (mx > p[1]) { | ||
++ans; | ||
} | ||
mx = Math.max(mx, p[1]); | ||
} | ||
return ans; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
solution/1900-1999/1996.The Number of Weak Characters in the Game/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Solution: | ||
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int: | ||
properties.sort(key=lambda x: (-x[0], x[1])) | ||
ans = mx = 0 | ||
for _, d in properties: | ||
if mx > d: | ||
ans += 1 | ||
mx = max(mx, d) | ||
return ans |