🔥 Algorithm

[Javascript] 구름톤 챌린지 1주차 5문제 풀기

Lennon 2023. 8. 16. 14:52
728x90
반응형

[개발한 프로젝트 참여하기 프로젝트]

 

1. 운동 중독 플레이어

const readline = require('readline');
let rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let input;
rl.on('line', (line) => {
	input = line;
	rl.close();
});

rl.on('close', () => {
	const [W, R] = input.split(" ").map(Number);
	
	const answer = getOneRM({W, R});
	
	console.log(answer);
})

const getOneRM = ({W, R}) => {
	const OneRM = Math.floor(W * (1 + R / 30));
	
	return OneRM;
}

 

2. 프로젝트 매니징

const readline = require('readline');
let rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let input = [];
rl.on('line', (line) => {
	input.push(line);
	
  if (input.length === +input[0] + 2) {
    rl.close();
  }
});

rl.on('close', () => {
	const [_, currentTime, ...featureArr] = input;
	
	const answer = getFinishTime({ currentTime, featureArr:featureArr.map(Number)});
	
	console.log(answer);
})

const getFinishTime = ({ currentTime, featureArr }) => {
	const allFeatureFinishTime = featureArr.reduce((prev, cur) => prev + cur, 0);
	
	const formatCurrentTime = calculateTotalMinutes(currentTime);
	
	const finishTime = allFeatureFinishTime + formatCurrentTime;
	
	return `${Math.floor((finishTime / 60) % 24)} ${finishTime % 60}`;
}

const calculateTotalMinutes = (time) => {
	const [hours, minutes] = time.split(' ').map(Number);
	
	return hours * 60 + minutes;
}


3. 합 계산기

const readline = require('readline');
let rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let input = [];
rl.on('line', (line) => {
	input.push(line);
	
	if (input.length === +input[0] + 1) {
		rl.close();
	}
});

rl.on('close', () => {
	const [T, ...mathExpressions] = input;
	
	const answer = calculateTotalOfExpressions(mathExpressions);
	
	console.log(answer);
})


const calculateTotalOfExpressions = (mathExpressions) => {
	return mathExpressions.reduce((prev, cur) => {
		const [num1, mathCode, num2] = cur.split(' ');
		
		if (mathCode === "/") {
			return prev + Math.floor(eval(cur));
		}
		
		return prev + eval(cur);
	}, 0);
}

4. 완벽한 햄버거 만들기

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

const input = [];
rl.on('line', (line) => {
    input.push(line);
	
    if (input.length === 2) {
        rl.close();
    }
});

rl.on('close', () => {
	const [N, arr] = input;
	
	const ingredients = arr.split(' ').map(Number);
	
	const answer = getPerfectTaste(ingredients);

	console.log(answer);
});

const isSameSorted = (arr, descending = false) => {
	const sortedArr = [...arr].sort((a, b) => (descending ? b - a : a - b));
	
	return arr.join('') === sortedArr.join('');
};

const getPerfectTaste = (ingredients) => {
	const maximum = Math.max(...ingredients);
	const maximumIndex = ingredients.indexOf(maximum);

	const leftArr = ingredients.slice(0, maximumIndex);
	const rightArr = ingredients.slice(maximumIndex + 1);

	if (isSameSorted(leftArr) && isSameSorted(rightArr, true)) {
            const sumLeft = leftArr.reduce((prev, cur) => prev + cur, 0);
            const sumRight = rightArr.reduce((prev, cur) => prev + cur, 0);

            return maximum + sumLeft + sumRight;
	}

	return 0;
};

5. 이진수 정렬

const readline = require('readline');

const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
const input = [];
rl.on('line', (line) => {
	input.push(line);

	if (input.length === 2) {
		rl.close();
	}
});

rl.on('close', () => {
	const [NK, arr] = input;

	const [N, K] = NK.split(' ').map(Number);

	const intArr = arr.split(' ').map(Number);
	const binaryArr = intArr.map((int) => int.toString(2));

	const answer = getAnswerNumber({ K, intArr, binaryArr });

	console.log(answer);
});

const getAnswerNumber = ({ K, intArr, binaryArr }) => {
	const combinationIntAndBinaryArr = intArr.map((int, i) => [
		getCountOfOne(binaryArr[i]),
		int,
	]);

	const sortedArr = combinationIntAndBinaryArr.sort((a, b) => {
		if (a[0] === b[0]) {
			return b[1] - a[1];
		}
		return b[0] - a[0];
	});

	return sortedArr[K - 1][1];
};

const getCountOfOne = (binary) =>
	binary.split('').filter((v) => v === '1').length;
728x90
반응형