Skip to content

Commit ca21462

Browse files
Merge pull request #1870 from CodingTestStudy2/변지협
[변지협] Day22
2 parents 27cbe7d + 5a30041 commit ca21462

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
'''
3+
1. 아이디어 :
4+
그냥 y를 먼저 붙이고, 나머지 문자를 붙인다.
5+
x 몰라도 그냥 풀림
6+
7+
2. 시간복잡도 :
8+
o(n)
9+
10+
3. 자료구조/알고리즘 :
11+
'''
12+
13+
class Solution:
14+
def rearrangeString(self, s: str, x: str, y: str) -> str:
15+
ans = ''
16+
tmp = ''
17+
18+
for i in s:
19+
if i == y:
20+
tmp += i
21+
else:
22+
ans += i
23+
24+
return tmp+ans
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
'''
3+
1. 아이디어 :
4+
점수를 잃을땐 최대 power를 얻고, 점수를 얻을 땐 최소 pwoer를 소모한다.
5+
6+
2. 시간복잡도 :
7+
o(nlogn)
8+
9+
3. 자료구조/알고리즘 :
10+
투포인터
11+
'''
12+
13+
14+
class Solution:
15+
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
16+
tokens.sort()
17+
n = len(tokens)
18+
19+
a = 0
20+
b = n - 1
21+
score = 0
22+
ans = 0
23+
24+
while True:
25+
print('a,b,power,score:', a,b,power)
26+
if a > b:
27+
break
28+
29+
if power >= tokens[a]:
30+
power -= tokens[a]
31+
score +=1
32+
ans = max([score, ans])
33+
a += 1
34+
else:
35+
if score == 0:
36+
break
37+
power += tokens[b]
38+
score -= 1
39+
b -= 1
40+
41+
return ans

0 commit comments

Comments
 (0)