Stack Overflow » Algorithm

94 FOLLOWERS

Read the most recent 30 Algorithm posts from Stack Overflow. The world's largest online community for developers, helping write the script of the future by serving developers and all technologists.

Stack Overflow » Algorithm

11h ago

I am trying to solve a problem, where i have to print all the longest common subsequences. But I am not aware how to print them.
However I know how to print any one of them but, I don't know how to print all of them?
How should i approach this question?
(Recursion I thought, but I am not able to write it and it is giving TLE ..read more

Stack Overflow » Algorithm

11h ago

If I have an algorithm that simply converts an input integer to its binary representation and stores the result, what is the space complexity of this algorithm?
I initially assumed that the space required would be proportional to the number of bits in the binary representation of the number. This would give an O(logn) space complexity, since the number of bits required to represent an integer n in binary is roughly log(2)n.
However, I’ve recently considered the fact that the input would be constrained to a fixed range, such as 2^31 - 1 for a 32-bit signed integer. In this case, the length of t ..read more

Stack Overflow » Algorithm

11h ago

Using this code to identify primes:
sieve:: [Integer] -> [Integer]
sieve [] = []
sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
isPrime' :: Integer -> Bool
isPrime' 2 = True
isPrime' x = x == last (sieve [2..x])
isPrime :: Integer -> Bool
isPrime 2 = True
isPrime p = p > 2 && (all (\n -> p `mod` n /= 0) $ takeWhile (\n -> n * n <= p) [3, 5 ..])
the function isPrime' runs much slower and takes more space than isPrime. Using elem instead of last also runs slowly.
ghci> isPrime' 10007
True
(0.33 secs, 181,461,088 bytes)
ghci> isPrime 10007
True ..read more

Stack Overflow » Algorithm

11h ago

How can you find the maximum possible sum of values by optimally placing tiles in a matrix rxc where each cell has an integer value?
The goal is to maximize the sum by covering cells with 1x2 or 2x1 tiles ..read more

Stack Overflow » Algorithm

11h ago

The problem: G. Sakurako and Chefir
Problem: Given a tree, for each node, find the farthest node you can reach within some number of climb-ups
Here's my C++ code:
#include<iostream>
#include<vector>
using namespace std;
void down_to_leaves(int curr,
vector<vector<int>>& list,
vector<int>& dist,
vector<int>& parent,
vector<bool>& visited) {
visited[curr] = true;
for (int child : list[curr]) {
if (visited[child]) {
continue;
}
parent[child] = curr;
down_to_l ..read more

Stack Overflow » Algorithm

11h ago

UPDATE: thanks for all the suggestions! I was not aware that this problem had a name ("digital root") and that it didn't requre repeated recursion/iteration or extra space.
My revised (working/efficient) code looks like this now:
public static int superDigit_Fast(string n, int k)
{
long sum = 0;
foreach (char c in n)
{
sum += (c - '0') * k;
}
return sum == 0 ? 0 : (int)(((sum - 1) % 9) + 1);
}
A common algorithmic challenge is to take a number and repeatedly sum its digits until you arrive at a single digit.
For example:
9875
-> 9+8+7+5
= 29
-> 2+9
= 11
-&g ..read more

Stack Overflow » Algorithm

11h ago

In trying to understand Wikipedia's pseudocode of the A* search algorithm, I came up with the following doubt regarding the code inside the for loop given by
for each neighbor of current
tentative_gScore := gScore[current] + d(current, neighbor)
if tentative_gScore < gScore[neighbor]
cameFrom[neighbor] := current
gScore[neighbor] := tentative_gScore
fScore[neighbor] := tentative_gScore + h(neighbor)
if neighbor not in openSet
openSet.add(neighbor)
is updating the neighbor's g score only relevant for performance? All I can see that part o ..read more

Stack Overflow » Algorithm

11h ago

I am writing a voxel game in Unity.
The world of the game is a 3d array voxels (cubes).
Each voxel may either be empty or occupied.
I need to cast a ray from any point within the world and figure out where will it first hit a surface of an occupied voxel.
public struct Vector3 {
public float x, y, z;
}
public struct Vector3Int {
public int x, y, z;
}
public enum Voxel {
Empty,
Occupied,
}
public struct RaycastResult {
public bool success; // whether any block was hit by raycast
public Vector3 point; // point where ray hits the surface of first solid voxel
publ ..read more

Stack Overflow » Algorithm

2d ago

I'm having some troubles on creating a relationship-based program, what I need is to create a function to aggregate related nodes together, like this, if I have those 4 nodes:
A
B
C
D
If A -> B, B -> C, C -> D, A -> C, B -> D, like this: nodes
When A -> D, I'd like to create an intermediate Node, replacing all relationships between those nodes with a single connection from each node to this new one. Like this:
@ <- cluster
A ->@, B -> @, C -> @, D -> @
So whoever is connected to @, has a relationship with all of its members
This to avoid 5x5, 10x10, 25x25 rel ..read more

Stack Overflow » Algorithm

2d ago

I've been struggling with the LeetCode problem 334. Increasing Triplet Subsequence:
Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.
[...]
Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space complexity?
So the numbers need not be consecutive, but their indexes need to be ordered; That is, [1,2,0,3] is true since [1,2,3] works, but [3,2,1] is false.
After finding a good enough solution to pass the tests and readi ..read more