Skip to content

Instantly share code, notes, and snippets.

@simolex
Last active April 1, 2023 13:10
Show Gist options
  • Select an option

  • Save simolex/7e81cfb9520807df254376505d7d569f to your computer and use it in GitHub Desktop.

Select an option

Save simolex/7e81cfb9520807df254376505d7d569f to your computer and use it in GitHub Desktop.
Компания-1. Тестовое задание-4

Вступительное задание компании "Компания-1"

Задача №4

Язык JavaScript

Описание задачи и тестовый набор в комментариях

// const n = 13;
// const a = [1, 2, 3, 1, 2, 2, 3, 3, 3, 1, 4, 4, 5];
// const n = 5;
// const a = [1, 1, 1, 1, 1];
// const n = 10;
// const a = [1, 2, 4, 2, 3, 1, 3, 9, 15, 23];
// const n = 5;
// const a = [1, 2, 3, 4, 5];
// const n = 14;
// const a = [2, 2, 2, 2, 1, 3, 3, 3, 3, 1, 5, 5, 5, 5];
const n = 18;
const a = [5, 3, 2, 3, 3, 3, 3, 5, 5, 5, 5, 2, 5, 2, 1, 2, 2, 4];
const propsInit = {
lastBalance: 0,
maxLevel: 0,
listenPrimeLevel: {},
listenItemByOne: {},
sizeSetOfItem: 0,
groups: {}
// ,debug: []
};
const res = a.reduce((props, value, index) => {
if (index == 0) {
props.groups[value] = { count: 1 };
props.listenPrimeLevel[value] = props.groups[value];
props.listenItemByOne[value] = props.groups[value];
} else {
if (props.groups[value] === undefined) {
props.groups[value] = { count: 1 };
props.listenItemByOne[value] = props.groups[value];
if (props.maxLevel === 0) {
if (Object.keys(props.listenItemByOne).length > 1) {
props.listenPrimeLevel = {};
props.maxLevel++;
}
}
} else {
if (props.groups[value].count === 1) {
props.sizeSetOfItem++;
delete props.listenItemByOne[value];
}
if (props.groups[value].count === props.maxLevel) {
if (Object.keys(props.listenPrimeLevel).length === 0) {
props.listenPrimeLevel[value] = props.groups[value];
} else {
props.listenPrimeLevel = {};
props.maxLevel++;
}
}
if (props.groups[value].count === props.maxLevel + 1) {
props.maxLevel++;
}
props.groups[value].count++;
}
if (
Object.keys(props.listenPrimeLevel).length > 0 &&
props.maxLevel * (props.sizeSetOfItem + Object.keys(props.listenItemByOne).length) === index
) {
props.lastBalance = index + 1;
}
if (
Object.keys(props.listenPrimeLevel).length == 0 &&
Object.keys(props.listenItemByOne).length > 0 &&
props.maxLevel * (props.sizeSetOfItem + Object.keys(props.listenItemByOne).length - 1) === index
) {
props.lastBalance = index + 1;
}
}
// props.debug.push(JSON.parse(JSON.stringify(props)));
return props;
}, propsInit);
console.log(res.lastBalance);
@simolex
Copy link
Author

simolex commented Apr 1, 2023

task_info
task_tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment