You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can directly simulate the process described in the problem by constructing a new array $\textit{ans}$. For each $i$, let $\textit{ans}[i] = \textit{nums}[\textit{nums}[i]]$.
68
+
69
+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/1900-1999/1996.The Number of Weak Characters in the Game/README_EN.md
+9-1
Original file line number
Diff line number
Diff line change
@@ -68,7 +68,15 @@ tags:
68
68
69
69
<!-- solution:start -->
70
70
71
-
### Solution 1
71
+
### Solution 1: Sorting + Traversal
72
+
73
+
We can sort all characters in descending order of attack power and ascending order of defense power.
74
+
75
+
Then, traverse all characters. For the current character, if its defense power is less than the previous maximum defense power, it is a weak character, and we increment the answer by one. Otherwise, update the maximum defense power.
76
+
77
+
After the traversal, we get the answer.
78
+
79
+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of characters.
0 commit comments