전체 학생의 수 n, 체육복을 도난당한 학생들의 번호가 담긴 배열 lost, 여벌의 체육복을 가져온 학생들의 번호가 담긴 배열 reserve가 매개변수로 주어질 때, 체육수업을 들을 수 있는 학생의 최댓값을 return 하도록 solution 함수를 작성해주세요.
입출력 예
n | lost | reserve | return |
---|---|---|---|
5 | [2, 4] | [1, 3, 5] | 5 |
5 | [2, 4] | [3] | 4 |
3 | [3] | [1] | 2 |
function solution(n, lost, reserve) {
let answer = 0;
for (let student = 1; student <= n; student++) {
if (!lost.includes(student)) {
answer++;
continue;
}
if (lost.includes(student) && reserve.includes(student)) {
const reserveStudentIndex = reserve.indexOf(student);
const lostStudentIndex = lost.indexOf(student);
reserve.splice(reserveStudentIndex, 1);
lost.splice(lostStudentIndex, 1);
answer++;
continue;
}
const prevStudent = student - 1;
const nextStudent = student + 1;
if (reserve.includes(prevStudent) && !lost.includes(prevStudent)) {
findReserveStudent(reserve, prevStudent, lost, student);
answer++;
continue;
} else if (reserve.includes(nextStudent) && !lost.includes(nextStudent)) {
findReserveStudent(reserve, nextStudent, lost, student);
answer++;
continue;
}
}
return answer;
}
function findReserveStudent(reserve, reserveStudent, lost, lostStudent) {
const reserveStudentIndex = reserve.indexOf(reserveStudent);
const lostStudentIndex = lost.indexOf(lostStudent);
reserve.splice(reserveStudentIndex, 1);
lost.splice(lostStudentIndex, 1);
return reserve;
}
student No. | student count | lost | reserve |
---|---|---|---|
1 | 1 | [ 2, 4 ] | [ 1, 3, 5 ] |
2 | 2 | [ 4 ] | [ 3, 5 ] |
3 | 3 | [ 4 ] | [ 3, 5 ] |
4 | 4 | [] | [ 5 ] |
5 | 5 | [] | [ 5 ] |
student No. | student count | lost | reserve |
---|---|---|---|
1 | 1 | [ 2, 4 ] | [ 3 ] |
2 | 2 | [ 4 ] | [] |
3 | 3 | [ 4 ] | [] |
5 | 4 | [ 4 ] | [] |
function solution(n, lost, reserve) {
let clothes = new Array(n).fill(1);
lost.map((lostNumber) => clothes[lostNumber - 1] = 0);
reserve.map((reserveNumber) => clothes[reserveNumber - 1] += 1);
for (let i = 0; i < n; i++) {
if (clothes[i] === 0 && clothes[i - 1] === 2) {
clothes[i] = 1;
clothes[i - 1] = 1;
} else if (clothes[i] === 0 && clothes[i + 1] === 2) {
clothes[i] = 1;
clothes[i + 1] = 1;
}
}
return clothes.filter((clothCount) => clothCount > 0).length;
}
function solution(name) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const reverse = 'ZYXWVUTSRGPONMLKJIHGFEDCBA';
const nameArray = name.split('');
let topDownMove = 0;
let leftRightMove = name.length - 1;
nameArray.forEach((str, index) => {
const alphabetIndex = alphabet.indexOf(str);
const reverseIndex = reverse.indexOf(str);
if (alphabetIndex < reverseIndex) {
topDownMove += alphabetIndex;
} else {
topDownMove += reverseIndex + 1;
}
const nameLength = name.length;
let nextIndex = index + 1;
while(nextIndex < nameLength && name[nextIndex] === 'A') {
nextIndex++;
}
leftRightMove = Math.min(leftRightMove, (index * 2) + nameLength - nextIndex);
leftRightMove = Math.min(leftRightMove, (nameLength - nextIndex) * 2 + index);
});
const answer = topDownMove + leftRightMove;
return answer;
}
function solution(number, k) {
const stack = [];
for (let index = 0; index < number.length; index++) {
const digit = number[index];
while(k > 0 && stack[stack.length - 1] < digit) {
stack.pop();
k--;
}
stack.push(digit);
}
stack.splice(stack.length - k, k); // pop이 k보다 적게 되었을 경우, splice로 남은 k를 제거함
const answer = stack.join('');
return answer;
}
/**
* @param {number[]} g
* @param {number[]} s
* @return {number}
*/
var findContentChildren = function(g, s) {
g.sort((a,b) => a - b);
s.sort((a,b) => a - b);
let childIndex = 0;
let contentChildrenCount = 0;
for (let cookieIndex = 0; cookieIndex < s.length; cookieIndex++) {
if (s[cookieIndex] >= g[childIndex]) {
childIndex++;
contentChildrenCount++;
}
if (childIndex === g.length) {
break;
}
}
return contentChildrenCount;
};
Balanced strings are those that have an equal quantity of 'L'
and 'R'
characters.
Given a balanced string s
, split it into some number of substrings such that:
Each substring is balanced.
Return the maximum number of balanced strings you can obtain.
2 <= s.length <= 1000
s[i]
is either 'L'
or 'R'
.s
is a balanced string.Example 1
Example 2
/**
* @param {string} s
* @return {number}
*/
var balancedStringSplit = function(s) {
let countOfBalancedStr = 0;
let countOfR = 0;
let countOfL = 0;
for (let i = 0; i < s.length; i++) {
const str = s[i];
if (str === 'R') {
countOfR++;
} else {
countOfL++;
}
if (countOfR === countOfL) {
countOfBalancedStr++;
countOfR = 0;
countOfL = 0;
}
}
return countOfBalancedStr;
};