Skip to content

Commit 27cbe7d

Browse files
Merge pull request #1871 from CodingTestStudy2/김민제
[김민제] Day22
2 parents 48fcd03 + f1698ea commit 27cbe7d

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def rearrangeString(self, s: str, x: str, y: str) -> str:
3+
result = []
4+
x_count = s.count(x)#O(n)
5+
y_count = s.count(y)#O(n)
6+
7+
if s.find(x) ==-1 or s.find(y) ==-1: #O(n)
8+
result.append(s)
9+
else:
10+
for s1 in s:
11+
if s1 == y:
12+
result.insert(0,s1) #O(n^2)
13+
elif s1 != x:
14+
result.append(s1)
15+
result.insert(y_count,x*x_count)
16+
return ''.join(result)
17+
18+
s = "zaodvxbsvqstlrbn"
19+
x = "t"
20+
y = "s"
21+
solution = Solution()
22+
print(solution.rearrangeString(s,x,y))
23+
24+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
6+
tokens.sort() #nlog(n)
7+
left = 0 #가장 작은 토큰
8+
right = len(tokens)-1 #가장 큰 토큰
9+
score = 0
10+
max_score = 0
11+
while left <=right:#O(n)
12+
if power >=tokens[left]:#face-up
13+
power -= tokens[left]
14+
score += 1
15+
left += 1
16+
max_score = max(max_score,score)
17+
elif score > 0 and left < right:#face-down
18+
power += tokens[right]
19+
score -= 1
20+
right -= 1
21+
else:
22+
break
23+
return max_score
24+
25+
26+
tokens = [100,200,300,400]
27+
power = 200
28+
solution = Solution()
29+
print(solution.bagOfTokensScore(tokens,power))

0 commit comments

Comments
 (0)