例如,我想减少相同元素的链的最大长度的长度,直到链的长度为最小,例如:
["A","A","B","B","A"] to ["A","B","A","B","A"] (max chain from 2:AA,BB to 1:A,B)
["A","A","A","B","B","C"] to ["A","B","A","B","A","C"] (max chain from 3:AAA to 1:A,B,C)
["A","A","A","A","B","C"] to ["A","A","B","A","C","A"] (max chain from 4:A to 2:A, not ["A","A","B","A","A","C"] because if max length of chains are the same, select one that contains less number of max length of chains)
["A","A","A","A","B","B"] to ["A","B","A","A","B","A"] (also not ["A","A","B","A","A","B"] because it has 2 AA instead of 1)
Run Code Online (Sandbox Code Playgroud)
如何编写函数来排序?我试过:
["A","A","B","B","A"] to ["A","B","A","B","A"] (max chain from 2:AA,BB to 1:A,B)
["A","A","A","B","B","C"] to ["A","B","A","B","A","C"] (max chain from …Run Code Online (Sandbox Code Playgroud)