Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 35368

An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.

2 votes
Accepted

Binary Search in Ruby

Same concept as yours, but slimmed down a bit, and avoiding an explicit loop. You might also consider making this a utility function, or a mixin with which you can dynamically extend Enumerable objec …
Jonah's user avatar
  • 4,390
6 votes
Accepted

Hackerrank: "Cut the sticks" Javascript solution

Once the array is sorted, as you have it, the crux of this algorithm is simply: subtract the smallest element from every element, then remove elements that equal 0 With es6 and method chaining, this … arr.length) return; console.log(arr.length); cut(arr.map(x => x - arr[0]).filter(x => x > 0)); // <-- the essence of the algorithm } } I should also point out that the above, as well as your …
Jonah's user avatar
  • 4,390
10 votes

Trapping Rain Water

Here's a more functional approach you might like: function trappedWater(heights) { const maxSoFar = arr => arr.reduce((m, x) => m.concat(Math.max(...m, x)), []) const leftWall = arr => [0, ...max …
Jonah's user avatar
  • 4,390
1 vote

Next, greater permutation of digits of a number

From this perspective, I think it becomes clear that the algorithm you're implementing is by nature procedural, and there is no way around those k and l temporary variables (certainly not k). … The only question that remains would be: Is there some clever equivalent version of this algorithm which wouldn't require the use of temporary variables or their equivalent? …
Jonah's user avatar
  • 4,390
4 votes
Accepted

Convert decimal to binary and find the maximum number of consecutive 1's in the binary

You solution looks good for a procedural one -- something similar would be the correct way to do this in, say, C. That said, and assuming this isn't considered "cheating" for the problem at hand, if …
Jonah's user avatar
  • 4,390
7 votes

Selection sorter

To be true to the spirit of the algorithm, you really should not make any array copies. … What I've done below is keep to the spirit of the original algorithm, while taking advantage of some of ruby's syntactic sugar, so that the algorithm is still slightly briefer and more readable than it …
Jonah's user avatar
  • 4,390
3 votes

Text justification program

Interesting problem. I think your answer is quite a bit longer than it needs to be. Consider rethinking what the solution entails, essentially. Also, your code will be quite a bit more readable if …
Jonah's user avatar
  • 4,390
3 votes

Find the majority element, which appears more than half the time

@200_success's suggestion seems like the right play here. That said, I thought it was worth pointing out a couple small improvements to your approach: major need only be the element itself (since y …
Jonah's user avatar
  • 4,390
0 votes

Add one to a very large integer

Here's an approach which uses reduce to build up the result number, and avoids the special case "if...else" logic. Note that as soon as we hit a digit that doesn't carry over, add becomes 0 and the r …
Jonah's user avatar
  • 4,390
1 vote

Prefix Sum in Ruby, Genomic Range Query from Codility

Here's a take on the algorithm you used that takes advantage of more ruby sugar. …
Jonah's user avatar
  • 4,390
2 votes

What a crooked way to compute the next straight string (advent of code 11)

The official reddit thread has really nice solutions: https://www.reddit.com/r/adventofcode/comments/3wbzyv/day_11_solutions/ Here's the best javascript one imo: var pwd = 'cqjxjnds'; while(!/(abc| …
Jonah's user avatar
  • 4,390
4 votes

Find all letter Combinations of a Phone Number

I think your functional approach is very nice, and scores high points for readability. As long as the recursion wasn't causing performance issues, that's how I'd approach it. However, it's not very …
Jonah's user avatar
  • 4,390