Tagged Questions
An algorithm is a sequence of well-defined steps that defines an abstract solution to a problem. Use this tag when your issue is related to algorithm design.
-1
votes
0answers
5 views
Asynchronous Multiplayer game invite players approaches
I am developing an asynchronous multiplayer game which needs to find players whom are online . So basically I am targeting android devices for this game . What are the best ways you think to invite ...
1
vote
1answer
21 views
Pick random key based on weight and picked times
I am designing an advertising system that rotates randomly between ads depending on their weight (bid).
local ads = local ads = {
["a"] = {
views = 0,
bid = 10
},
["b"] = ...
3
votes
1answer
43 views
Median of a Matrix with sorted rows
I am not able to solve the following problem optimally nor finding an approach to do this anywhere.
Given a N × M matrix in which each row is sorted, find the overall median of the matrix. Assume N*...
0
votes
1answer
18 views
KMP algorithm worst case analysis
I can't understand how KMP maintains O(m+n). I'm looking for a pattern "aaaab" in "aaaaaaaaaa...".
pattern : aaaab (length n)
string : aaaaaaaaaa... (length m)
Then the prefix table would be
...
0
votes
2answers
21 views
ng class using modulus in angularjs
I used $index with modulus
<div ng-repeat="day in days" ng-class="{'col-xs-2':$index % 3 === 0}">
<div class="days-group">
<input id="{{day.value}}"type='checkbox' value="{{...
0
votes
0answers
26 views
Genetic Algorithm Fitness Stop Increasing at Some Point
I am trying to implement a Genetic Algorithm which will recreate a given image by positioning, sizing and colouring 1000 circles. However, I found that when the fitness reaches a certain value (around ...
-4
votes
0answers
30 views
How to make a static array in a class, it's size is based on the number passed by constructor?
I want to make a static array called "ls". its size is based on the number the constructor of class Percolation, which is n.
So here is part of my code:
public class Percolation {
static int [][]...
1
vote
1answer
21 views
Is it possible to optimize the “nearest common ancestor in a binary tree” challenge using parallelism/asynchronicity?
I'm referring to the problem that's like if you have
8 = root.Val
/ \
5 31
/ \
30 51
/ \
40 60
and want to find
int nce = ...
0
votes
2answers
40 views
string replace in Python 2.7
Using Python 2.7 and working on below string replace problem, wondering if any better ideas in terms of algorithm space complexity and algorithm time complexity?
I create an additional list to ...
0
votes
3answers
54 views
Algorithm or Code for creating array, rule is as specified?
I want to create 2D array as below:
For Example:
For level 3:
7 => Array[2]
3 6 => Array[1]
1 2 4 5 => Array[0]
i.e. Array = [[1,2,4,...
3
votes
1answer
40 views
hide show custom filter in ng-repeat that uses table
How can I use custom filter to hide inventory column of color pencil special on date 2-10-2017 in the view?
color pencil special's inventory is depends on color pencil's inventory which in this case ...
0
votes
1answer
63 views
Two largest contiguous subarray
I'm currently doing a problem that's similar to the maximum contiguous sub-array problem. However, instead of finding just one contiguous sub-array, I can find up to two non-overlapping contiguous ...
0
votes
0answers
21 views
Optimizing the running time for hierarchical clustering?
The running time for my implementation of hierarchical clustering is too slow and takes forever to analyse data with about 3000 clusters. I have tried optimizing some small parts e.g. combining sort ...
2
votes
3answers
56 views
Find the highest ranked item of a list from another list
I have a list of items already ordered by highest rank that I store in variable topList.
Then I have current list of items that I store in variable currentList
The goal is to find the element of ...
1
vote
1answer
37 views
Is it possible to realize column-wise operation and row-wise operation by one template function?
Here is my problem, I am dealing with a n-dimensional data. Let's say n=2 for simplicity. Also I have an algorithm for 1D data. In order to extend this algorithm for 2D problem, I can do
for each row
...
0
votes
2answers
24 views
Runtime of double sorting algorithm
In this picture, I'm having trouble understanding why sorting sorting the array requires understanding that "Each string comparison takes O(s) time", therefore multiplying the a*log(a) section. Why ...
8
votes
3answers
395 views
What is the space complexity of this code?
int f(int n)
{
if (n <= 1)
{
return 1;
}
return f(n - 1) + f(n - 1);
}
I know that the time complexity is O(2^n) and I understand why.
But I don't understand why the ...
-1
votes
1answer
41 views
stuck on longest palindrome algorithm in python 2.7
Hey everyone I have been struggling on the longest palindrome algorithm challenge in python 2.7. I am getting close but have a small error I can't figure out. I have the palindrome working, but cannot ...
0
votes
2answers
22 views
Non-repeating combination of specific count in PHP
This is a particular variation of an oft-repeated question, but try as I might, I couldn't find this exact situation anywhere on Stack Overflow.
Long story short, I want to take an array like this:
$...
0
votes
0answers
13 views
CTCI 4.3: Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth
I'm solving this problem with Ruby, and I used a modified DFS algorithm to do it. The idea is that, every time DFS has to look at adjacent nodes, then it's looking at the children and therefore that's ...
-4
votes
0answers
28 views
Is there any efficient way to calculate how many times the nested for loop runs? [on hold]
Given1 <= a <= b <= c <= d,time complexity to calculate how many times the loop runs is o( a * b * c * d ).
But, is there any efficient way to calculate this?
Try to give answer with ...
0
votes
1answer
17 views
Automatically find a given number of colors which match to each other
Given a number n, what is the best way to find n colors which match to each other in an harmonic way? My current approach is as follows:
In the HSV model I start with a color color1 = (h1,s1,v1).
For ...
0
votes
2answers
29 views
Labeling vertex to create minimum cost
I have this problem:
you are given a rooted tree T, representing the company hierarchy, and
you want to label each node in T with an integer 1, 2, or 3, so that every node has a
different label from ...
0
votes
3answers
50 views
Infinite loop in binary search variant (Java)
My aim is to input a key and an array, and then output the number of values in that array which are less than or equal to the key, using binary search.
This is my code:
import java.util.*;
import ...
1
vote
2answers
50 views
Moving maximum variant
Yesterday, I got asked the following question during a technical interview.
Imagine that you are working for a news agency. At every discrete point of time t, a story breaks. Some stories are more ...
0
votes
1answer
22 views
BufferedImage resize algorithm
i cant find any solution for my problem, hope you can help me. I have a Buffered image (e.g. img), und i want to resize it (with its array of pixels) to fit exactly 100px (100 x 100). All the ...
1
vote
3answers
47 views
Finding nearest element in a set of integers
I have following problem to solve:
Given N integers forming set S and another integer A (not neccesarily same as any of the N integers given), find integer from set S that is nearest to integer A.
...
1
vote
0answers
28 views
Runtime error SIGSEGV on SPOJ ACPC10D
I was solving ACPC10D problem from spoj LINK but I am constantly getting RTE SIGSEGV while submitting. I have tried all the testcases which are mentioned in the forum and my solution gives correct ...
0
votes
2answers
22 views
Big O: How to determine runtime for a for loop incrementation based on outer for loop?
I have the following algorithm and the runtime complexity is O(N^2) but I want to have a deeper understanding of it rather than just memorizing common runtimes.
What would be the right approach to ...
0
votes
0answers
32 views
Closed Point Algorithm [migrated]
User enters a nxn Matrix (with each position set either to 0 or to 1).
An 0 value at a position impiles absence of the point and a 1 value implies presence of the point.
Now the user enters a value (...
1
vote
1answer
29 views
What are the pros and cons of a hexagon-based grid system as opposed to square-based?
In a two-dimensional world simulation, for example the internal model of a robotics engine, or the representation of a world in a role-playing game, ground space is typically separated into a grid ...
2
votes
3answers
32 views
how to find set of distinct strings from a given string after cyclic shifts?
I am solving a [QUESTION][1] in Codeforces where the problem statement asks me to find the set of all distinct strings from a given string after cyclic shifts.
like for example :
Given string :"abcd"
...
-3
votes
0answers
16 views
how to solve over segmentation in JSEG?
[ ]
this code for JSEG algorithm ,JSEG is one of the most robust techniques to identif homogeneous and non homogeneous regions of a color image, when i used it i faced over segmentation problem
<...
1
vote
4answers
64 views
why is my little python fibonacci detector failed?
for some reason I have to determine if a big number is a fibonacci number, so I copy some code from internet and modified it a little, it seems not operate well when it's big input. here is code:
# ...
1
vote
2answers
33 views
Heapsort: Why is the right-most node of the 2nd to last level at index n/2-1?
I was studying BST tree for heap sort.
void heapSort(int arr[], int n){
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
-----------
-------...
0
votes
1answer
21 views
Google Foobar, maximum unique visits under a resource limit, negative weights in graph
I'm having trouble figuring out the type of problem this is. I'm still a student and haven't taken a graph theory/linear optimization class yet.
The only thing I know for sure is to check for ...
1
vote
3answers
41 views
how to delete adjacent entries from a set while iterating over it
How can I erase all adjacent entries from a set while iterating over the set. In my case I have a custom comparator that defines adjacent entries as those that differ by 1 from left to right. Thus ...
2
votes
2answers
48 views
Monte Carlo pi method with fixed x
In all Monte Carlo example codes I have found which calculate pi, both x and y are generated randomly between 0 and 1. For example, an example code looks like
Ran rdm(time(NULL));
double x, y;
...
1
vote
3answers
46 views
Print linkedlist reversely
I'm working on the problem of print a linked list reversely without destruction. My specific questions are,
wondering if any other better ideas, space complexity improvement, my current space ...
-1
votes
2answers
22 views
Does using separate function for every modules increases the time-complexity & space of that program
Algorithm-Time complexity & Space Complexity. Writing code in just one main() without using separate function().(competitive programming).Does it makes any changes regarding complexities.
1
vote
3answers
49 views
Closest pair algorithm from (n log^2 n) to (n log n) time
I am trying to understand how to go from n log^2 n time to n log n time for the closet pair algorithm. I get the below part (from http://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairDQ.html)
Divide ...
0
votes
0answers
6 views
How can I generate U-Blox UBX Sentences?
How can I generate U-Blox UBX Sentences? For example UBX NAV-POSLLH and NAV-SOL? The UBX Protocol and the binary encoding makes it very difficult for me.
-1
votes
1answer
40 views
Does a word checking algorithm exist?
I've been thinking of if this was created already but image a function that can validate a string and determine if it's a word or not. eg
print(validateWord("Hello")) --> true
print(validateWord(...
-1
votes
0answers
21 views
Mapping GPS locations on a picture
Let's say I have an image, taken from an airplane. It shows a city. I don't know the location where the image is exactly taken, but I can identify some points on the picture and give them GPS position....
1
vote
2answers
70 views
Time complexity of program determining if two strings are permutations of each other
I have written a program to determine if two strings are permutations of one another. I am trying to do so using a hash table. Here is my code:
bool permutation(string word1, string word2) {
...
0
votes
1answer
31 views
Computing Predecessor and Successor
I come across an interesting question and I want to discuss it in order to see how it will be approached by different people:
Let n be a natural number, the task is to implement a function f so that
...
0
votes
1answer
16 views
Finding the image from a list used in a second image that has an added background
I'm currently working on a project where I have a set of small images with a white background. The user will provide me with an image that contains one of the above images (exactly one, guaranteed), ...
-1
votes
0answers
51 views
Is insert into Binary Heap O(log n) or O(n)
I'm trying to understand if inserting into a binary heap (min or max) is O(1) or O(log n) in the average case.
The wikipedia article says O(1) in one place and O(log n) in another place.
Binary ...
-1
votes
0answers
29 views
Is my approach correct?
I am struggling with the following problem:
Given a directed graph with 3<=N<=1000 vertices and 3<=M<=1000000 edges you can choose a simple cycle of this graph and walk it.While you are ...
-3
votes
1answer
24 views
Choice of starting n in proof of big O
In the Question it is given as
Find The Upper bound for f(n)=n^4+100n^2+50
And the solution follows
n^4 +100n^2+50<=2n^4 for all n>= 11
Actually I can't understand why n>=11 is taken?
And why ...