๐ฅ ๋คํธ ๐ฅ ํ ํฐ ๋ฐฑ || ๋ฆฌํธ์ฝ๋ || ์ฝ๊ณ ๊ฐ๋จํ ์๋ฃจ์ || 100% ๋ ๋น ๋ฅธ ์ฝ๋ || ํ ์ค์ฉ ์ค๋ช
19358 ๋จ์ด dartleetcodegoprogramming
์๋ฃจ์ - 1 ์ ๋ ฌ
class Solution {
// Time Complexity : O(nlogn)
// Space Complexity : O(1)
// Runtime: 336 ms, faster than 100.00% of Dart online submissions for Bag of Tokens.
// Memory Usage: 158.9 MB, less than 100.00% of Dart online submissions for Bag of Tokens.
int bagOfTokensScore(List<int> tokens, int power) {
// length of the tokens
int n = tokens.length;
// if the length is zero we will return zero because it empty
if (n == 0) return 0;
// sorting tokens smallest to largest
tokens.sort();
// holding the value of the inside list
int value = 0;
//index of tokens inside tokens list
int index = n - 1;
// value to hold the current score
int currentScore = 0;
// value to hold max score
int maxScore = 0;
// assuming that our value is less than and equal the idex
while (value <= index) {
// for example power is greater than tokens value
if (power >= tokens[value]) {
// than we will subtract the power from the token value
power -= tokens[value];
// and we will increment the current score
currentScore++;
// for example if current score is greater than max score than
// our max score will be current score and than we will increment the value
if (currentScore > maxScore) maxScore = currentScore;
value++;
// for example if current score is just greater than 1 point
} else if (currentScore >= 1) {
// than we will increment the value of power with based on
// the value which is available at index point
power += tokens[index];
// we decrement the index value
index--;
// and also current score
currentScore--;
} else
// breaking because we can't play anymore
break;
}
// return the total max score
return maxScore;
}
}
์๋ฃจ์ - 2๊ฐ์ ๋ ํฌ์ธํฐ ์๋ฃจ์
์ค๋ช
์ฐ๋ฆฌ๋ ์ด ๋ฌธ์ ๋ฅผ ๋ ํฌ์ธํฐ ์ ๊ทผ๋ฒ์ ์ฌ์ฉํ์ฌ ํ ์ ์์ต๋๋ค!
์ง๊ด :
ํ ํฐ ๋ชฉ๋ก์ ์ ๋ ฌํฉ๋๋ค.
ํฌ์ธํฐ i๋ ์ฒ์์ ๋ชฉ๋ก์ ์์ ๋ถ๋ถ์ ๊ฐ๋ฆฌํต๋๋ค. (์ ์๋ฅผ ์ป๊ธฐ ์ํด ์๋นํ๋ ๋ฐ ํ์ํ ๊ฐ์ฅ ๋ฎ์ ์ ๋ ฅ์ผ๋ก ์ ์์ ์ถ๊ฐ๋จ)
ํฌ์ธํฐ j๋ ์ฒ์์ ๋ชฉ๋ก์ ๋์ ๊ฐ๋ฆฌํต๋๋ค. (์ ์์์ ๋จ ํ ์ ๋ง ์ฌ์ฉํ์ฌ ์ป์ ์ ์๋ ๊ฐ์ฅ ํฐ ํ)
์ ์์ฌ?
์ฐ๋ฆฌ๋ ํญ์ ์ ์๋ฅผ ๋์ด๋ ๊ฒฝํฅ์ด ์์ต๋๋ค. ๋ฐ๋ผ์ ์ถฉ๋ถํ ํ์๊ฐ ์์ ๋๊น์ง ํ์ฌ ํ์์์ ํ์(tokens[i])๋ฅผ ๋นผ์ i๋ฅผ ๊ณ์ ์ฆ๊ฐ์ํค๊ณ ๋งค๋ฒ ์ ์์ ๋จ์ผ ํฌ์ธํธ๋ฅผ ์ถ๊ฐํฉ๋๋ค.
์ฐ๋ฆฌ์๊ฒ ์ถฉ๋ถํ ํ์ด ์์ ๋, ์ฐ๋ฆฌ๋ ์ฐ๋ฆฌ๊ฐ ํ ์ ์๋ ์ต์ํ์ ์ ์์ธ 1์ ๋ง ์๋นํจ์ผ๋ก์จ ํ ํฐ ๋ฐฐ์ด์์ ์ต๋ ํ์ โโ์ป์ผ๋ ค๊ณ ๋ ธ๋ ฅํ ๊ฒ์ ๋๋ค. ์ด์ ํ์ฌ ์ ์์์ 1์ ์ ๋นผ๊ณ ํ์(ํ ํฐ[j])๋ฅผ ํ์ฌ ํ์์ ๋ํ๊ณ j๋ฅผ 1์ฉ ์ค์ ๋๋ค.
ํ ํฐ ๋ชฉ๋ก์์ ์ ์ด๋ ํ๋์ ํฌ์ธํธ๋ฅผ ๋ ์ป์ ๊ฒ์ด๋ผ๊ณ ํ์ ํ๋ ๊ฒฝ์ฐ์๋ง ์ ์์์ ๋จ์ผ ํฌ์ธํธ๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ค๋ ์ ์ ๊ธฐ์ตํ์ญ์์ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ํ ํฐ์ ์ฌ์ฉํ์ง ์์๋ ๋ฉ๋๋ค. ๊ฐ์ ์ ์ผ๋ก ๋ค์ ์๋๋ฆฌ์ค๋ฅผ ์๋ฏธํฉ๋๋ค.
๋น์ ์ ํ์ด ์์ต๋๋ค. ๋ฐฐ์ด์๋ ํ๋์ ์์๋ง ๋จ์ต๋๋ค. ๋น์ ์ ํ์ฌ ์ ์๋ >= 1์ ๋๋ค. ์ด๋ฐ ์ํฉ์์ ํ ํฐ์ ํ๋ ์ดํ๊ณ ๊ถ๋ ฅ์ ์ป๋ ๋ฐ ์ ์์ 1์ ์ ์๋นํ๋๋ผ๋ ์๋ฌด ์์ฉ์ด ์๊ฒ ์ฃ ? ์ฐ๋ฆฌ๋ ์๋ฌด ์ด๋๋ ์์ด ์ฐ๋ฆฌ์ ํ ์ ์ ๋ญ๋นํ๊ณ ์์ ๋ฟ์ ๋๋ค!!
class Solution {
// Runtime: 653 ms, faster than 100.00% of Dart online submissions for Bag of Tokens.
// Memory Usage: 147.6 MB, less than 100.00% of Dart online submissions for Bag of Tokens.
int bagOfTokensScore(List<int> tokens, int power) {
int scores = 0;
int s = 0, e = tokens.length - 1;
tokens.sort((a, b) => a - b);
if (e + 1 == 0 || power < tokens[0]) return 0;
while (s <= e) {
if (power >= tokens[s]) {
scores++;
power -= tokens[s++];
} else {
if (e - s >= 1) {
power += tokens[e--];
scores--;
} else
break;
}
}
return scores;
}
}
์๋ฃจ์ - 3
class Solution {
// Runtime: 607 ms, faster than 100.00% of Dart online submissions for Bag of Tokens.
// Memory Usage: 148 MB, less than 100.00% of Dart online submissions for Bag of Tokens.
int bagOfTokensScore(List<int> tokens, int power) {
// play i-th token face up -> lose tokens[i] power -> choose the smallest one
// play i-th token face down -> gain tokens[i] power -> choose the largest one
// hence, sort tokens first
tokens.sort();
// two pointes - l for tracking face up & r for tracking face down
int l = 0, r = tokens.length - 1;
int currentScore = 0, maxScore = 0;
while (l <= r) {
// there are 3 cases
if (tokens[l] <= power) {
// case 1. play l-th tokens face up if its power <= the current power
// ---
// losing tokens[l] power
power -= tokens[l];
// and gaining 1 score
currentScore += 1;
// cur_score can be mx_score potentially
maxScore = max(maxScore, currentScore);
// move the pointer to the right
l += 1;
} else if (currentScore >= 1) {
// case 2. play r-th tokens face down if the current score is at least 1
// ---
// gaining tokens[r] power
power += tokens[r];
// and losing 1 score
currentScore -= 1;
// move the pointer to the left
r -= 1;
} else {
// case 3. impossible to play
// ---
// either you don't enough power or enough score
break;
}
}
return maxScore;
}
}
๋ณด๋์ค ์๋ฃจ์ - Golang 100% ๋นจ๋ผ์ง
package main
import "sort"
func bagOfTokensScore(tokens []int, P int) int {
// to hold the value of our score
var score int = 0
// sorting the token and slicing because sort.slice is more stable
sort.Slice(tokens, func(i, j int) bool { return tokens[i] < tokens[j] })
// loop if the token length is greater than 0 abd power is also greater and equal
// than the first value of the token list
for len(tokens) > 0 && P >= tokens[0] {
// we will decrement the first value of token from the power
P = P - tokens[0]
// increment the score because we point a score
score++
tokens = tokens[1:]
// if the length of the list of tokens is more than 1 and power is less than first value of the list
if len(tokens) > 1 && P < tokens[0] {
// than we will increment the value of power inside the first index of the token
P = P + tokens[len(tokens)-1]
// we will decrement the score
score--
tokens = tokens[:len(tokens)-1]
}
}
// than we will total score we have achieved
return score
}
Reference
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(๐ฅ ๋คํธ ๐ฅ ํ ํฐ ๋ฐฑ || ๋ฆฌํธ์ฝ๋ || ์ฝ๊ณ ๊ฐ๋จํ ์๋ฃจ์ || 100% ๋ ๋น ๋ฅธ ์ฝ๋ || ํ ์ค์ฉ ์ค๋ช ), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://dev.to/ayoubzulfiqar/dart-bag-of-tokens-leetcode-easy-and-simple-solution-100-faster-code-explained-line-by-line-3e45ํ ์คํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ณต์ ํ๊ฑฐ๋ ๋ณต์ฌํ ์ ์์ต๋๋ค.ํ์ง๋ง ์ด ๋ฌธ์์ URL์ ์ฐธ์กฐ URL๋ก ๋จ๊ฒจ ๋์ญ์์ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค