close
Skip to main content

New answers tagged

Score of 2

How can I generate human-friendly numeric scale values between two float64 values in Go?

A useful approach is to first calculate the ideal mathematical step and then round that step up to a human-friendly value from the sequence: 1 × 10ⁿ 2 × 10ⁿ 5 × 10ⁿ 10 × 10ⁿ This is the same general ...
Score of 2

question at / pivot and >> 2 in C++ Solution for Next Higher Number

This solution is a combination of purely arithmetic operations (binary addition with carry) and purely bitwise manipulations. int pivot = n & -n; Here we get the rightmost set bit. The operation ...
Score of 1

Bejeweled board generation

Once the board is generated, it is checked for matches. If a match is found, the tile type is changed. This continues until there are no matches. In the code, it could look like this: void Board::...
Score of 3
Accepted

Is the Merge sort merely an improved insert sort?

Not really the same thing, but you're onto something that already has a name. Merge sort splits blindly in half regardless of order. What you're describing — spotting already-sorted runs like 4, 8 and ...
Score of 0

Merging two sorted skip lists into a third one can be done in O(n)?

(The sizes of the sorted input arrays are m and n.) Worst-case, you need m + n - 1 comparisons to merge them. O(m+n). Best-case, IF the ranges don't overlap at all, the simplest merge algorithm will ...
Score of 4
Accepted

How do I turn an algorithm accepting a total function into an algorithm that accepts a function returning Maybe and that returns itself Maybe?

Just use sortByM with the Maybe monad.
Score of 0

Calculate number images in a row to fit container while keeping aspect ratio

you should try group images into rows first before calculate the height Keep adding images to the current row until adding the next image would make the row height fall below your desired minimum. ...
Score of 3
Accepted

Longest Increasing Subsequence - failing for a test case

In the if (nums[index] <= nums[pindex]) branch, you should return helper(nums, index + 1, pindex) instead of 0 to continue the search. However, this solution will still cause Time Limit Exceeded, ...
Score of 0

Algorithm to return all combinations of k elements from n

Have you considered using regex "string-powering" instead of loops ? jot -s '' -w '&%c' 8 65 | awk 'BEGIN { RS = "^$" } END { print _1_ = _2_ = $+(_ = (_)_) gsub(/[&...
Score of 0

Merging two Binary Search Trees efficiently and create one BST

Your intuition of O(m*n) is not completely wrong. That actually happens in the worst-case scenario when the tree is unbalanced. However, when people mentions like you told reading other posts it is ...
Score of 2

Tile window placement

I offer my own version. First the description, then the code in assembler. Also an example of a C program that fills the screen with rectangles. Description first. First, you need to take the square ...
Score of 0

Segment tree with easy shifting

I would recommend an implicit treap data structure, it is pretty much a classic BST, but it also acts as a heap for priorities. If you assign each node a random priority, you effectively get an auto-...
Score of 0

How to use better OOP for a Huffman Compressor

You should give the encoder a simple array or vector of 256 encodings, so for each char c, it can just look up encodings[c] to get the encoding and it'll be super fast. The encoding structure can be ...
Score of 0

Understanding why Floyd's tortoise and hare algorithm works when applied to an array of integers

Perhaps this will help to understand the tortoise/hare algorithm. Imagine each index in the array of length n+1 as a node. This means the nodes are 0,1,2,...n. The value at index i tells you which ...
Score of 3

MD4 Implementation not giving the correct hash output

Your first mistake is on line 2. Word A of the MD buffer should be initialized to 01 23 45 67, low order bytes first. If you're assigning that to a 32-bit unsigned int, that would be 0x67452301, not ...
Score of 2

How to solve solution with binary search

Here are the issues in your code: order.putIfAbsent(b[i], i) causes duplicate dog characteristics being stored only once (e.g. multiple dogs have number 2 in the input example) Sorting a loses the ...
Score of 0

Combination with elements never in the same group more than once

Your problem is almost exactly equivalent to Kirkman's Schoolgirl Problem. That's why we can immediately say, the maximum is seven weeks. Under the usual definition of isomorphism (people may be ...
Score of 1

How to solve solution with binary search

My understanding is that multiple sheep can be sent at the same time by two dogs. This is not a typically binary-search problem. Intuitively we were to get the minimum of bi as bx and the maximum of ...
Score of 12
Accepted

Algorithm for adding large lists of rational numbers

If you can use int128, and the numerator and denominator of the final result can be represented using int64_t, here is an algorithm that avoids overflowing intermediate fractions. Instead of choosing ...

Top 50 recent answers are included