Skip to content

Commit 48fcd03

Browse files
Merge pull request #1872 from CodingTestStudy2/이진희
[이진희] Day22
2 parents 5587677 + d9a9a0e commit 48fcd03

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
3+
1. 아이디어 : x가 아닌 숫자들을 먼저 붙이고, 이후 x개수만큼 더 붙인다
4+
5+
2. 시간복잡도 : O(N+N)
6+
7+
3. 자료구조/알고리즘 : 완전탐색
8+
9+
*/
10+
class Solution {
11+
public String rearrangeString(String s, char x, char y) {
12+
StringBuilder sb = new StringBuilder();
13+
int cnt = 0;
14+
for(int i=0; i<s.length(); i++) {
15+
if(s.charAt(i) != x) sb.append(s.charAt(i));
16+
else cnt++;
17+
}
18+
19+
while(cnt-->0) sb.append(x);
20+
return sb.toString();
21+
}
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
3+
1. 아이디어 : face-up과 face-down의 규칙을 파악후, 그리디로 계산
4+
- face-up: powers 이하의 가장 낮은 점수
5+
- face-down: 가장 높은 점수
6+
2. 시간복잡도 : O(NlogN) + O(N)
7+
8+
3. 자료구조/알고리즘 : 투포인터 , 정렬 (그리디)
9+
10+
*/
11+
12+
class Solution {
13+
public int bagOfTokensScore(int[] tokens, int power) {
14+
// face-up: power가 tokens[i] 이상이면 -> tokens[i]만큼 power를 잃고, 1점+
15+
// face-down: score가 1 이상이면, tokens[i] 만큼 powers를 얻고, 1점 감점
16+
// 가능한 가장 높은 score
17+
18+
// face-up: powers 이상의 가장 낮은 점수
19+
// face-down: tokens중 가장 높은 점수
20+
21+
Arrays.sort(tokens);
22+
23+
int l=0;
24+
int r=tokens.length-1;
25+
int score = 0;
26+
int maxScore = 0;
27+
28+
while(l<=r) {
29+
// face-up
30+
if(tokens[l]<=power) {
31+
power-=tokens[l];
32+
score++;
33+
l++;
34+
maxScore = Math.max(score, maxScore);
35+
}
36+
// face-down
37+
else if(score>0) {
38+
power+=tokens[r];
39+
score--;
40+
r--;
41+
}
42+
else break;
43+
}
44+
45+
return maxScore;
46+
}
47+
}

0 commit comments

Comments
 (0)