Описание задачи и тестовый набор в комментариях
Last active
April 1, 2023 13:10
-
-
Save simolex/7e81cfb9520807df254376505d7d569f to your computer and use it in GitHub Desktop.
Компания-1. Тестовое задание-4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); |
Author
simolex
commented
Apr 1, 2023


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