An algorithm is a precise plan (in the format of a sequence of steps) on how to solve a particular problem.
0
votes
0answers
21 views
Get the levels of binary tree
Given a binary tree, return a list of each level.
I'm looking for code-review, best-practices, and optimizations. Also verifying complexity.
Time complexity: \$O(n)\$
Space complexity: ...
3
votes
4answers
50 views
Improving efficiency for two sum
I am trying to solve this problem:
Given an array of integers, find two numbers such that they add up to a specific target number.
and this is my strategy:
For each ...
1
vote
2answers
44 views
Binary Insert Method - handling edge cases
I've created a function that uses binary search to insert an element into an array (similar to this question). I haven't benchmarked it, but based on my knowledge it should get \$O(1)\$ in the best ...
1
vote
0answers
15 views
C code for hash-consing: how could its performance be increased?
I've isolated this function, cons, for hash-consing in C. It is where the bottleneck of my program is. How could its performance be improved?
...
3
votes
1answer
12 views
Bit compressing in JavaScript
I have an bit array var data = []; ... and I have the following function:
jsPerf
...
0
votes
0answers
32 views
How to avoid StackOverflowError caused by the number of recursion calls in Java? [on hold]
I want to solve this problem: https://www.hackerrank.com/challenges/find-median ,i.e. find the median element in unsorted array. To do this I perform quickselect algorithm. My program works correctly ...
11
votes
4answers
171 views
I'm not sure if I still love Fibonacci, but my code is getting better. How much better?
Technically, this is a follow up to these two questions, but I have taken a radically different approach.
Everybody Loves Fibonacci
Does everyone still love Fibonacci
I finally allowed myself to ...
-1
votes
0answers
31 views
Followup to BigInteger Cube Root Optimization
Followup to Followup: How do I optimize this Java cube root function for BigInteger?
Here's my current code for this cube root approximation for BigInteger, which returns the integer part of the cube ...
1
vote
0answers
15 views
NGON (many polygons) on SPOJ - Time Limit Exceeded
I am trying to solve NGON problem. I am using bottom up dynamic programming here. Recurrence function is:
...
3
votes
1answer
32 views
How to generate valid number of combinations of datacenter and its host id?
I have a list of datacenters, which are dc1, dc2 and dc3. And each datacenter has six ids as ...
4
votes
1answer
64 views
Followup: How do I optimize this Java cube root function for BigInteger?
Followup to How do I optimize this Java cube root function for BigInteger?
So I've tried several implementations of this algorithm. The version using only BigInteger sometimes results in a ...
2
votes
2answers
41 views
Merge N sorted list
Given n sorted lists, merge them.
This is a fairly common question, attributed to plenty of interview question websites.
I'm looking for code-review, optimizations and best practices. I'm also ...
-2
votes
0answers
37 views
path in a labyrinth [closed]
i have an algorithm in path in a labyrinth can you help me solve this problem ? I have a problem in my instructor during our discussion
here is the algorithm
- Let the current position in the ...
9
votes
4answers
276 views
Threshed and Malachi'd: Sieve of Eratosthenes
I took a little bit from all the answers to my previous question Threshing: Sieve of Eratosthenes.
This may be bordering on code-golfing, but I think that I have a pretty good piece of code here.
...
3
votes
1answer
36 views
SPOJ is giving TLE for this solution of ROCK
I am solving ROCK problem on SPOJ. I have used bottom up dynamic programming approach for this problem. Time complexity for this is \$O(n^3)\$.
...
0
votes
0answers
21 views
Python code to find unique combinations of factors of a number [closed]
I have the below code which prints the unique combination of factors of a given number.
But I does not give the desired output. The code is a s follows:
...
6
votes
3answers
142 views
Iterator of iterator
Given an iterator of iterators, return all of the elements from the iterators in order. Looking for code review, optimizations, and best practices. Verifying complexity to be \$O(n)\$, where \$n\$ is ...
2
votes
1answer
81 views
Why are these functions slower than BigInteger's included methods?
I tried writing alternative functions to Java's BigInteger.add(BigInteger.ONE), BigInteger.add(BigInteger), and ...
5
votes
4answers
361 views
Threshing: Sieve of Eratosthenes
I would like a complete threshing of this code so that I can see what I did wrong and what I am using incorrectly.
I made this super simple, trying to learn a little bit about ...
6
votes
1answer
48 views
Longest Common Substring solution for a string and a list
Recently I have discovered one thing, which makes me feel frustrated. The Perl is lack of syntax sugar for strings, to do the same things, those are applicable to lists. The implementation I posted ...
5
votes
2answers
158 views
How to generate valid number of combinations basis on the rules?
I have a list of datacenters, which are dc1, dc2 and dc3. And each datacenter has six ids as ...
3
votes
1answer
35 views
Automatically gravitate popover towards page center
I've authored a couple of plugins which show popovers on a given element. One of the objectives for both was no specification of plugin direction. So instead of the implementing dev having to specify ...
1
vote
0answers
32 views
Find K distant nodes
Find all nodes, which are at a distance of k from the input node.
Input:
target = pointer to node with data 8.
...
4
votes
0answers
44 views
Fibonacci number function: convert to tail-recursion?
I've been implementing a function for calculating the nth Fibonacci number in F#. So far the best implementation I could come up with is this:
...
6
votes
3answers
64 views
Searching all diagonals of a 2D M x M array
I've started writing a piece of code to help me search for an object in all the objects found in the diagonals of an M x M 2D array.
Though the code works, I'd like to know if there is a way I can ...
4
votes
1answer
50 views
Product of corresponding elements in 2 arrays
Given two arrays A and B, of length N and length k ...
4
votes
1answer
36 views
Canonicalizing a large set of addresses using many regex substitutions
I have a script that is standardizing a large amount of data in the database. The standardization involves applying over 500 regular expressions to the data.
Here is some quick pseudocode:
...
8
votes
5answers
743 views
Splitting company name into 3 strings
I wrote an algorithm which should cut a companies name into 3 strings.
Input: 1 String
Output: 3 String.
Conditions:
String 1 2 and 3 shall not be longer then 35 signs. If the Input ...
-2
votes
1answer
84 views
Given a BST, transform it into greater sum tree
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node. Diagram is here.
Looking for code review, optimizations and best practices.
...
1
vote
1answer
51 views
Solving the coin change algorithm using 10 coins
I'm solving the coin change problem having target of n and upper bound as n.
Also the maximum number of coins is 10.
For example. if the target is 11. then the possible outcomes are -
...
6
votes
2answers
125 views
Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree
Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree.
Given tree:
...
4
votes
1answer
81 views
Parallelizing scrypt key-derivation function
To review my use of multiprocessing, I don't think it is at all necessary to understand the algorithm, but it's the scrypt key-derivation function.
This uses ...
6
votes
1answer
106 views
Finding all paths from a given graph
I need to find all paths from a given graph. I can do that for now, however my recursive code is not efficient and my graphs are very complicated, hence I need a better algorithm.
...
3
votes
1answer
112 views
Minimum number of letters removed, to make a string Peragram
Here is the link to the problem Peragrams.
Peragrams: If a word is an anagram of at least one palindrome, we call it a Peragram.
Problem: Given a string, find the minimum number of letters ...
7
votes
1answer
125 views
How do I optimize this Java cube root function for BigInteger?
I find that this function is one of the biggest causes of slow program execution. I can write a square root version with BigInteger only, but with the cube root, the algorithm sometimes gets caught in ...
0
votes
0answers
30 views
Detect if a tree is complete binary tree
Detect if tree is complete binary tree. This question is improvement over previously asked here. Looking for code-review, optimizations and best practices.
Verifying both space and time complexity to ...
2
votes
1answer
27 views
Trie SPOJ Problem - PhoneList TLE
In this problem you are given input of phone numbers and you are supposed to determine whether any number is the prefix of another. I have an addchild method that ...
4
votes
0answers
51 views
Thinking in 'Haskell' - when to use (state) Monads
I really enjoy Haskell but feel I still have a total beginner's style, and would like to move beyond that. The code below - for Dijkstra's shortest path algorithm - is a case in point. I feel as ...
2
votes
4answers
95 views
Maximum of all subarrays of size k
Given an array and an integer k, find the maximum for each and every contiguous subarray of size k.
Examples:
Input:
...
5
votes
2answers
140 views
Find max-number div by three to be formed by array elements
Given an array of non-negative integers, find the largest multiple of 3 that can be formed from array elements.
For example, if the input array is {8, 1, 9}, the output should be "9 8 1", and ...
5
votes
1answer
53 views
Generic Graph Library Part 2 : Algorithms
(This is a follow on to this question).
Having given an implementation for some of the graph structures, here is a portion of the algorithms that are defined to work over them:
graph_degree.hpp
...
6
votes
4answers
143 views
Stack with 'getMinimum' operation
Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the ...
4
votes
1answer
46 views
Stack with “getMiddle” and “deleteMiddle” operation
Looking for code review, optimizations and best practices.
...
4
votes
5answers
192 views
Is there a better version for verbosing the output of the euclidean method?
Here is my implementation of the Euclidean Algorithm. My question is how to make it more "professional". It's working right, but isn't this too newbie?
...
11
votes
4answers
1k views
“Two-pass algorithm” implementation in Java
I'm rather new to Java and to algorithms in general. I implemented the so called Two-pass algorithm, and want to know if there are improvements to my way of implementing it. What it does is it takes a ...
3
votes
0answers
126 views
Genetic Algorithm in Python
I'm a new programmer, so any help is advised. Preferably to make it faster, avoid heavy memory usage and so on.
EDIT:
Updated the code, now including a functional test program.
Fixed the PEP-8 ...
3
votes
3answers
863 views
5
votes
0answers
85 views
String matching algorithm
I have been going through algorithms and data structures and recently came across string matching algorithms. I have not yet wrapped my head around the significance KMP algorithm so I came up with ...
2
votes
1answer
51 views
Performance of Codility MaxCounter challenge solution
I'm doing some of the Codility challenges using Ruby. The current challenge is "The MaxCounter," which is described as:
Calculate the values of counters after applying all alternating operations: ...
2
votes
0answers
41 views
Implementation of merge sort and bubble sort
I'm new to Go and looking for a review of my merge sort and bubble sort implementations. What can be done better? Can my code be cleaner/clearer?
...