The King of Written Test Algorithms Hammer Bonus Problem (JS)

Keywords: Javascript

The King of Written Test Algorithms Hammer Bonus Problem

demand

  1. On the basis of seniority bonus: according to the length of service bonus, any two adjacent employees must have more work age.
  2. At least 100 per person.
  3. The minimum amount of bonuses paid

Train of thought

  1. Give each person 100 first
  2. Two traversals:

    1. The first traverse from left to right shows that the right side is longer than the left side, giving the right side more than 100 rounds.
    2. The second traverse from right to left shows that the left side has a higher working age than the right side, but the left side has a higher salary than the right side, giving the left side 100 times more.
  3. Accumulate the values in the bonus array and return the results.

JS Implementation Idea 1

function calcBonus(peopleArr) {
            var resultArr = [];
            var n = peopleArr.length
            for (let i = 0; i < n; i++) {
                resultArr[i] = 100;
            }
            for (let i = 1; i < n; i++) {
                if (peopleArr[i] > peopleArr[i - 1]) {
                    resultArr[i] = resultArr[i - 1] + 100;
                }
            }
            for (let i = n - 1; i > -1; i--) {
              // Judge if the left is older than the right and the salary is not higher than the right.
                if (peopleArr[i - 1] > peopleArr[i] && resultArr[i - 1] < resultArr[i] + 100) {
                    resultArr[i - 1] = resultArr[i] + 100;
                }
            }
            var result = 0;
            for (let i = 0; i < n; i++) {
                result += resultArr[i]
            }
            return result;
        }

// Test cases:
var peopleArr1 = [9, 6, 3] 
calcBonus(peopleArr1) //300, 200, 100 => 600
var peopleArr2 = [1, 4, 5, 9, 3, 2] 
calcBonus(peopleArr2)// 100, 200, 300, 400, 200, 100 => 1300

Time complexity is O(N), space complexity is O(N)

Train of thought two

Travel only once to find out the final reward and:

  1. First give 100 to the first person.
  2. The length of service on the right side is equal to that on the left side, with the same amount of 100.
  3. The right side is older than the left side, plus 100
  4. The length of service on the right side is lower than that on the left, so it needs further discussion:

    1. Assuming that the maximum length of service is present on the left, we will find the last person whose length of service is no longer decreasing.
    2. Then push forward, each forward, add 100, add to the first person who starts to reduce the length of service, equivalent to the sequence of equal difference. So, assuming that n individuals are continuously reduced, the total bonus for these people is 100 n* (n + 1) / 2.
    3. Finally, it is necessary to determine whether the person with the highest working age on the first left needs to be added. If his bonus is not greater than the one on the right, then he needs to add 100n minus his original bonus plus 100.

For better understanding, see the following two test cases:

var peopleArr3 = [1, 5, 4, 2, 3, 1] // 100, 300, 200, 100, 200, 100 => 1000
var peopleArr4 = [1, 2, 3, 4, 5, 3, 2, 1] // 100, 200, 300, 400, 500, 300, 200, 100 => 2100

PS: For convenience of calculation, the unit value can be set to 1, then the final result * 100 is enough.

JS Implementation Idea II

function calcBonus2(peopleArr) {
            var result = 1;
            var n = peopleArr.length;
            var curMax = 1;
            var count = 0;
            for(let i = 1; i < n; i++) {
                if(peopleArr[i] >= peopleArr[i - 1]){
                    if(count){
                        accCount();
                    }
                    curMax = peopleArr[i] == peopleArr[i - 1] ? 1 : curMax + 1
                    result += curMax
                }else {
                    ++count
                }
            }
            if(count) {
                accCount();
            }
            function accCount() {
                result += count * (count + 1) / 2;
                if(count >= curMax){
                            result += count - curMax + 1;
                            count = 0;
                            curMax = 1;
                        }
            }
            return result * 100;
        }

calcBonus2(peopleArr3); // 1000
calcBonus2(peopleArr4); // 2100

Time complexity O(n), space complexity O(1)

Posted by miz_luvly@hotmail.com on Sun, 06 Oct 2019 06:21:30 -0700