Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*

1. 아이디어 : x가 아닌 숫자들을 먼저 붙이고, 이후 x개수만큼 더 붙인다

2. 시간복잡도 : O(N+N)

3. 자료구조/알고리즘 : 완전탐색

*/
class Solution {
public String rearrangeString(String s, char x, char y) {
StringBuilder sb = new StringBuilder();
int cnt = 0;
for(int i=0; i<s.length(); i++) {
if(s.charAt(i) != x) sb.append(s.charAt(i));
else cnt++;
}

while(cnt-->0) sb.append(x);
return sb.toString();
}
}
47 changes: 47 additions & 0 deletions leetcode3/이진희/948. Bag of Tokens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*

1. 아이디어 : face-up과 face-down의 규칙을 파악후, 그리디로 계산
- face-up: powers 이하의 가장 낮은 점수
- face-down: 가장 높은 점수
2. 시간복잡도 : O(NlogN) + O(N)

3. 자료구조/알고리즘 : 투포인터 , 정렬 (그리디)

*/

class Solution {
public int bagOfTokensScore(int[] tokens, int power) {
// face-up: power가 tokens[i] 이상이면 -> tokens[i]만큼 power를 잃고, 1점+
// face-down: score가 1 이상이면, tokens[i] 만큼 powers를 얻고, 1점 감점
// 가능한 가장 높은 score

// face-up: powers 이상의 가장 낮은 점수
// face-down: tokens중 가장 높은 점수

Arrays.sort(tokens);

int l=0;
int r=tokens.length-1;
int score = 0;
int maxScore = 0;

while(l<=r) {
// face-up
if(tokens[l]<=power) {
power-=tokens[l];
score++;
l++;
maxScore = Math.max(score, maxScore);
}
// face-down
else if(score>0) {
power+=tokens[r];
score--;
r--;
}
else break;
}

return maxScore;
}
}