简体   繁体   English

找到最小的 - Codewars 挑战 - Javascript

[英]Find The Smallest - Codewars Challenge - Javascript

Trying to solve this Codewars challenge .试图解决这个Codewars 挑战

You have a positive number n consisting of digits.您有一个由数字组成的正数 n。 You can do at most one operation: Choosing the index of a digit in the number, remove this digit at that index and insert it back to another or at the same place in the number in order to find the smallest number you can get.您最多可以执行一个操作:选择数字中某个数字的索引,在该索引处删除该数字并将其插入到另一个或数字中的同一位置,以便找到您可以获得的最小数字。

Task: Return an array or a tuple or a string depending on the language (see "Sample Tests") with:任务:根据语言返回数组或元组或字符串(请参阅“示例测试”):

1) the smallest number you got 1)你得到的最小数字

2) the index i of the digit d you took, i as small as possible 2)你取的数字d的索引i,i越小越好

3) the index j (as small as possible) where you insert this digit d to have the smallest number. 3) 插入这个数字 d 以获得最小数字的索引 j(尽可能小)。

Example:例子:

smallest(261235) --> [126235, 2, 0] or (126235, 2, 0) or "126235, 2, 0"

Other examples:其他例子:

209917, [29917, 0, 1]
285365, [238565, 3, 1]
269045, [26945, 3, 0]
296837, [239687, 4, 1]

So, in order to get the smallest number possible, we will want to remove the smallest digit from the number and place it at the front of the number, correct?因此,为了获得尽可能小的数字,我们需要从数字中删除最小的数字并将其放在数字的前面,对吗?

 function smallest (n) { //turn n into an array let array = String(n).split("").map(Number); let smallest = Math.min(...array); //find index of smallest in original array let index = array.indexOf(smallest); //remove smallest from original array, move it to front array.splice(index, 1); array.unshift(smallest); let newNumber = Number(array.join("")); //return array of new number, index of where the smallest was, //and index of where the smallest is now return ([newNumber, index, 0]); } console.log(smallest(239687));

My answer is returning the correct number, but, about half the time, it is not returning the correct index i and index j .我的答案是返回正确的数字,但是,大约有一半的时间,它没有返回正确的索引i和索引j

EDIT : Latest attempt:编辑:最新尝试:

 function smallest (n) { let array = Array.from(String(n)).map(Number); let original = Array.from(String(n)).map(Number); let sorted = Array.from(String(n)).map(Number).sort((a, b) => a - b); let swapValueOne = []; let swapValueTwo = []; for (let i = 0; i < array.length; i++) { if (array[i] !== sorted[i]) { swapValueOne.push(sorted[i]); swapValueTwo.push(original[i]); break; } } swapValueOne = Number(swapValueOne); swapValueTwo = Number(swapValueTwo); let indexOne = original.indexOf(swapValueOne); let indexTwo = original.indexOf(swapValueTwo); //remove swapValue array.splice(indexOne, 1); //insert swapValue array.splice(indexTwo, 0, swapValueOne); return ([Number(array.join("")), indexOne, array.indexOf(swapValueOne)]); } console.log(smallest(296837));

^ Sometimes it gives the correct number with the correct swap indices, and sometimes both the number and the swap indices are wrong. ^ 有时它给出正确的数字和正确的交换索引,有时数字和交换索引都是错误的。

Putting the smallest element in the front (let's call it a "greedy" solution) is non-optimal.将最小的元素放在前面(我们称之为“贪婪”的解决方案)是非最优的。 Consider the case where n = 296837 , as in your last test case.考虑n = 296837的情况,如上一个测试用例。 Your code returns [296837, 0, 0] because it finds that 2 is the smallest digit and it moves it to the front (does nothing, essentially).您的代码返回[296837, 0, 0]因为它发现2是最小的数字并将其移到前面(基本上什么都不做)。 As your example illustrates, there's a better approach: [239687, 4, 1] , that is, move 3 to the first index in the array.正如您的示例所示,有一种更好的方法: [239687, 4, 1] ,即将3移动到数组中的第一个索引。

You'll need to reformulate your strategy to be non-greedy to find a global optimum.您需要重新制定策略以不贪婪地找到全局最优解。

If you're still stuck, you can try the following:如果您仍然卡住,您可以尝试以下操作:

Numbers can't contain that many digits--why not try every possible swap?数字不能包含那么多数字——为什么不尝试所有可能的交换?

Here's a small idea that might help.这是一个可能会有所帮助的小想法。

If you have a number like:如果你有一个类似的号码:

239687

The smallest number you can make with this is the sorted digits:您可以用它制作的最小数字是排序后的数字:

236789

In the original number, the 2 and the 3 are already in the correct position.在原来的数字中,2和3已经在正确的位置上了。 If you start from the left of the number and the sorted number, the first difference you find is the number that needs to be swapped.如果从数字和排序后的数字的左侧开始,您发现的第一个差异是需要交换的数字。 It needs to be swapped with the corresponding number in the sorted list:它需要与排序列表中的相应数字进行交换:

orig   2 3 9 6 8 7 -- 6 needs to go before 9
       | | x       
sorted 2 3 6 7 8 9

Above the next sorted digit is 6, but the original has 9. You need to insert 6 before 9.上面下一个排序的数字是6,但原来是9,需要在9前插入6。

For an algorithm you can sort your digits and find the index of the first difference (starting from the left).对于算法,您可以对数字进行排序并找到第一个差异的索引(从左侧开始)。 This is one of your return values (2 in the example).这是您的返回值之一(示例中为 2)。 Now find the index of sorted[2] (ie. 6) in the original (index 3).现在在原始(索引 3)中找到sorted[2] (即 6)的索引。 Insert the value in you original array and you're done.将值插入原始数组中,就完成了。

The approach of finding the first not sorted element doesnt solve correctly all the cases for example if the number is 300200, the first not sorted number is the 3 and if you put a 0 in that place, depending what 0 you move you got:找到第一个未排序元素的方法不能正确解决所有情况,例如如果数字是 300200,第一个未排序的数字是 3,如果你在那个地方放一个 0,取决于你移动的 0 你得到了什么:

(0)30020 (0)30020 (0)30200 (0)30200 (0)30020 (0)30020 (0)30200 (0)30200

All of the answers are wrong because what you have to do is to put the 3 at the end of the number to get所有的答案都是错误的,因为你要做的就是把 3 放在数字的末尾才能得到

(000)2003 (000)2003

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM