Given a string of numbers and a number of multiplication operators, what is the highest number one can calculate?

后端 未结 9 1800
醉话见心
醉话见心 2021-01-30 11:22

This was an interview question I had and I was embarrassingly pretty stumped by it. Wanted to know if anyone could think up an answer to it and provide the big O notation for it

9条回答
  •  逝去的感伤
    2021-01-30 11:56

    This came to mind, it's the brute force approach influenced by the bars and stars problem.

    Let's say our number is "12345" and we have 2 * operators we need to use. We can look at the string 12345 as

    1_2_3_4_5
    

    Where we can put the two * operators on any of the underscores. Since there are 4 underscores and 2 * operators, there are 4 choose 2 (or 6) different ways to place the operators. Compare those 6 possibilities and grab the largest number. A similar approach can be used for larger strings and larger number of * operators.

提交回复
热议问题