diff --git "a/leetcode3/\352\271\200\353\257\274\354\240\234/3992. Rearrange String to Avoid Character Pair.py" "b/leetcode3/\352\271\200\353\257\274\354\240\234/3992. Rearrange String to Avoid Character Pair.py" new file mode 100644 index 00000000..5a03991a --- /dev/null +++ "b/leetcode3/\352\271\200\353\257\274\354\240\234/3992. Rearrange String to Avoid Character Pair.py" @@ -0,0 +1,24 @@ +class Solution: + def rearrangeString(self, s: str, x: str, y: str) -> str: + result = [] + x_count = s.count(x)#O(n) + y_count = s.count(y)#O(n) + + if s.find(x) ==-1 or s.find(y) ==-1: #O(n) + result.append(s) + else: + for s1 in s: + if s1 == y: + result.insert(0,s1) #O(n^2) + elif s1 != x: + result.append(s1) + result.insert(y_count,x*x_count) + return ''.join(result) + +s = "zaodvxbsvqstlrbn" +x = "t" +y = "s" +solution = Solution() +print(solution.rearrangeString(s,x,y)) + + diff --git "a/leetcode3/\352\271\200\353\257\274\354\240\234/948. Bag of Tokens.py" "b/leetcode3/\352\271\200\353\257\274\354\240\234/948. Bag of Tokens.py" new file mode 100644 index 00000000..48e74498 --- /dev/null +++ "b/leetcode3/\352\271\200\353\257\274\354\240\234/948. Bag of Tokens.py" @@ -0,0 +1,29 @@ +from typing import List + + +class Solution: + def bagOfTokensScore(self, tokens: List[int], power: int) -> int: + tokens.sort() #nlog(n) + left = 0 #가장 작은 토큰 + right = len(tokens)-1 #가장 큰 토큰 + score = 0 + max_score = 0 + while left <=right:#O(n) + if power >=tokens[left]:#face-up + power -= tokens[left] + score += 1 + left += 1 + max_score = max(max_score,score) + elif score > 0 and left < right:#face-down + power += tokens[right] + score -= 1 + right -= 1 + else: + break + return max_score + + +tokens = [100,200,300,400] +power = 200 +solution = Solution() +print(solution.bagOfTokensScore(tokens,power))