Skip to content

Commit 7a44695

Browse files
Merge pull request #1875 from CodingTestStudy2/황은지
[황은지] Day22
2 parents f9fde0f + 1584e80 commit 7a44695

2 files changed

Lines changed: 46 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+
* @param {string} s
3+
* @param {character} x
4+
* @param {character} y
5+
* @return {string}
6+
*/
7+
var rearrangeString = function(s, x, y) {
8+
let left=0;
9+
let right=s.length-1;
10+
const arr=s.split("");
11+
12+
while(left<right){
13+
while(left<arr.length && arr[left]!==x ){left++};
14+
while(right>=0 && arr[right]!==y){right--};
15+
if(left>=right) break;
16+
// swap
17+
const temp=arr[left];
18+
arr[left++]=arr[right];
19+
arr[right--]=temp;
20+
console.log(arr);
21+
}
22+
23+
return arr.join("");
24+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} tokens
3+
* @param {number} power
4+
* @return {number}
5+
*/
6+
var bagOfTokensScore = function (tokens, power) {
7+
const dp = Array(10000);
8+
dp[power] = 0;
9+
const queue = [[power, 0]];
10+
let head = 0;
11+
let maxScore = 0;
12+
13+
while (queue.length - head > 0) {
14+
const [curP, curS] = queue[head++];
15+
maxScore = Math.max(maxScore, curS);
16+
17+
for (const token of tokens) {
18+
if (curP >= token) queue.push([curP - token, curS + 1]);
19+
if (curS >= 1) queue.push([curP + token, curS - 1]);
20+
}
21+
}
22+
};

0 commit comments

Comments
 (0)