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
Copy file name to clipboardexpand all lines: solution/0900-0999/0948.Bag of Tokens/README_EN.md
+53-26
Original file line number
Diff line number
Diff line change
@@ -131,7 +131,13 @@ tags:
131
131
132
132
<!-- solution:start -->
133
133
134
-
### Solution 1
134
+
### Solution 1: Greedy + Sorting + Two Pointers
135
+
136
+
There are two ways to use tokens: one is to consume energy to gain points, and the other is to consume points to gain energy. Obviously, we should consume as little energy as possible to gain as many points as possible.
137
+
138
+
Therefore, we can sort the tokens by the amount of energy they consume, and then use two pointers: one moving from left to right and the other from right to left. In each iteration, we try to consume energy to gain points as much as possible, and then update the maximum score. If the current energy is not enough to consume the current token, we try to consume the current token using points. If the points are not enough to consume the current token, we stop the iteration.
139
+
140
+
The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of tokens.
0 commit comments