Old Top Coder riddle: Making a number by inserting +

后端 未结 6 1544
借酒劲吻你
借酒劲吻你 2021-02-06 05:41

I am thinking about this topcoder problem.

Given a string of digits, find the minimum number of additions required for the string to equal some target n

6条回答
  •  伪装坚强ぢ
    2021-02-06 06:04

    Because input length is small (10) all possible ways (which can be found by a simple binary counter of length 10) is small (2^10 = 1024), so your algorithm is fast enough and returns valid result, and IMO there is no need to improve it.

    In all until your solution works fine in time and memory and other given constrains, there is no need to do micro optimization. e.g this case as akappa offered can be solved with DP like DP in two-Partition problem, but when your algorithm is fast there is no need to do this and may be adding some big constant or making code unreadable.

    I just offer parse digits of string one time (in array of length 10) to prevent from too many string parsing, and just use a*10^k + ... (Also you can calculate 10^k for k=0..9 in startup and save its value).

提交回复
热议问题