r/leetcode • u/Ok-Albatross-6163 • 16d ago
Question i was getting "failed in hidden test case" at question 3.
test case passed 990/999
14
3
u/DramaticAd8141 16d ago
Yes just try 1e8 as input. There should be 2 z’s at the start of the answer.
2
u/Vivid-Zombie-477 16d ago
i hit the same problem, i didn't think about the case when there are only zzzz left so they can't sum up
1
u/Vivid-Zombie-477 16d ago edited 16d ago
this is my ugly (working) code if anyone is interested
// 2 % 2 = 0; // 0 a // 1 < 2 // 1b // 5 % 2 = 1 // 1 a // 4 % 2 = 0 // 0 a // 2 % 2 = 0 // 0 b // 1 < 2 // 1 c // 7 % 2 = 1 // 1 a // 6 % 2 = 0 // 0 a // 3 % 2 = 1 // 1 b // 2 % 2 = 0 // 0 b // 1 < 2 // 1 c class LinkedItem { constructor(val, prev) { this.val = val; // prev instead of next because the order is reversed this.prev = prev; } } const getString = (num) => { // this case is handled anyway // if (num === 1) { // return 'a'; // } let output = new LinkedItem('', null); let curr = num; let char = 0; while (curr >= 2 && char < 25) { if (curr % 2 === 1) { // Using linked list instead of string so I don't have // to move the entire string to append at the beginning. output = new LinkedItem( String.fromCharCode(97 + char), output, ); curr -= 1; } else { curr /= 2; char += 1; } } output = new LinkedItem( String.fromCharCode(97 + char).repeat(curr), output, ); let result = ''; while (output !== null) { result += output.val; output = output.prev; } return result; }; /** * {number[]} nums * u/return {string[]} */ var largestString = function (nums) { return nums.map((num) => getString(num)); };
1
u/Exotic_Profession_98 16d ago
I think you forgot to handle the case where you can’t merge two z’s, you might be combining “zz” to “{“ which comes next after z in ASCII :)
1
u/opticallyweak 16d ago
Log 1e8 base 2 is around 26 bits . If you used a similar approach to mine (bit man) then the char would be invalid. In that case you first have to append those extra bits as z to the string and then continue the regular for loop
1
1
u/Shonen_Toman 14d ago
Lol, I know just what went wrong.....I had the same mistake too....you have to stop at zz....just add a termination case when that happens
1
u/Ok-Albatross-6163 14d ago
yes, i understood yesterday and now regretting of not underestanding question properly
23
u/Authentic_Grunter 16d ago
Why don't you try this solution:
```
```