The approach I used here is greedy I always want my score to increase so I will always go with the first condition that is if the power is greater than the current token then increment the count also if there is need or my power is less than the current token then I will always want to increase my power with the biggest token number. Therefore this gives the intuition that we need to sort the array of tokens in the ascending order so that when I am increasing my score then I pick from the left because it will be less than my power and if I want to increase my power then I will always choose from the right

/**
* @param {number[]} tokens
* @param {number} power
* @return {number}
*/
var bagOfTokensScore = function (tokens, power) {
tokens.sort((a, b) => a - b)
let score = 0
let ma...

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help