๐Ÿ”ฅ ๋‹คํŠธ ๐Ÿ”ฅ ํ† ํฐ ๋ฐฑ || ๋ฆฌํŠธ์ฝ”๋“œ || ์‰ฝ๊ณ  ๊ฐ„๋‹จํ•œ ์†”๋ฃจ์…˜ || 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
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ