Questions tagged [algorithm]

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.

Filter by
Sorted by
Tagged with
4
votes
3answers
1k views

C++ basic bank money class

I am trying to create a basic money class that fits into a small console banking application I am making. Money.h ...
1
vote
0answers
35 views

Leetcode 662: maximum width of binary tree in Java

I have the following solution to this problem. The idea is to compute the maximum width of a binary search tree. The width of a BST on a particular depth is defined as the distance from the leftmost ...
1
vote
1answer
41 views

Reverse a sublist of a singly-linked master list in constant space and maximum of one pass (Java)

So I found this funky programming challenge somewhere on Quora. The idea is to take the head of a singly-linked list, and reverse a specific sublist. The requirements are: runs in constant space, ...
1
vote
2answers
50 views

Implementing graph for pathfinding

I need a graph for multiple pathfinding algorithms,so I wanted to ask whether my new graph implementation is ok. Would someone who works as a developer in related fields(Graphs, Pathfinding, ...
3
votes
2answers
58 views

Cleaning a game logs list to find the frequent action triplets and the busy user

I have these game logs which I need to clean, process and find the frequency of game actions. These game actions should be a triplet. In the below given list, ...
6
votes
4answers
93 views

Nelder-Mead optimization in C++

I've implemented the Nelder-Mead optimization algorithm in C++. I found this document to be a very good explanation of how the algorithm works, but I'll try my best to explain it (with an example) ...
5
votes
1answer
340 views

Simple IP address subnet calculator

In this task I had to create simple IP address / subnet calculator in Python. I'm just wondering how you see this problem. There is my code: ...
3
votes
1answer
95 views

Binary Search of Array in C#

I am an algo-newbie and this is my first attempt at writing binary search and it worked on the first try. But something about it tells me that its far from perfect. Please tell me how I can improve. <...
2
votes
1answer
23 views

Minimum Spanning Tree in Rust

As a project I have worked on implementing and benchmarking two different minimum spanning tree algorithms in rust. My main concern is adhering to good style and not programming any glaring ...
1
vote
0answers
21 views

Non-allocating k-nearest-neighbours between two matrices in Julia

I played around with some optimizations when answering this SO question. Sought is a function which given matrices x of size (m, l) and ...
3
votes
1answer
65 views

Optimize "Fill magic square" in python with backtracking

I have written a python program with backtracking that fills a n * n magic square. It solves for n = 4 in 4.5 seconds but gets stuck when I run it for n = 5 on my ...
2
votes
2answers
19 views

Transforming a data of key value pairs to label and key

I have the following data ...
9
votes
1answer
429 views

3D Inverse Discrete Cosine Transformation Implementation in C++

This is a follow-up question for 3D Discrete Cosine Transformation Implementation in C++. In this post, I am trying to follow user17732522's answer to update ...
0
votes
0answers
18 views

Efficient algorithm to track all the direct parents and transitive parents(parents of parents and grand parents) in a directed acyclic graph

I am preparing for online coding assessment, where I have come across a question to write an algorithm to find the list of direct parents and transitive parents for each vertex in a directed acyclic ...
0
votes
2answers
142 views

Good practices for parsing this text file

I have the following .txt file structure that i want to parse in Java(8): ...
1
vote
0answers
49 views

Algorithm to fit people in a monthly calendar

I have an interesting algorithm to build and I have tried a couple of things but couldn't get it just right. I have the following set of rules: 16 people to fill every day of the month (let's call it ...
2
votes
1answer
99 views

red black tree implementation in cpp

I am learning algorithms and trying to implement them in c++, I have chosen to try to implement a red-black tree due to its self-balancing properties and its ability to stop the worst case of O(n) ...
6
votes
1answer
183 views

3D Discrete Cosine Transformation Implementation in C++

This is a follow-up question for Parallel 3D Discrete Cosine Transformation Implementation in Matlab and Operator overloading in Image class implementation in C++. I am trying to implement 3D Discrete ...
1
vote
0answers
41 views

Java reimplementation of the Python difflib.SequenceMatcher class

I'm rewriting a Python script into an Android app. This script uses difflib.SequenceMatcher to compare sequence of strings. I haven't been able to find something ...
0
votes
1answer
28 views

Find the shortest path from bottom left to top right in a maze using dfs and bfs algorithm

The following code is applying the depth-first search and breadth-first search algorithm to find the shortest path from bottom left to top right in a 10 x 10 grid. ...
0
votes
2answers
102 views

c++ Most clean way to implement duplicate Binary Search Tree for complex type

I have tried to make my own implementation for a BST task I was given for a complex type (Transaction), that should be able to store duplicate values as well. However, I am not sure if I have gone ...
3
votes
1answer
94 views

Dictionary based non-local mean implementation in C++

This is a follow-up question for Manhattan distance calculation between two images in C++ and Dictionary based non-local mean implementation in Matlab. For learning C++20 and researching purposes, I ...
1
vote
2answers
58 views

Binary Search using recursive approach

Is the following implementation of recursive function correct? How about the time complexity of the code; is it good in terms of performance? ...
3
votes
2answers
65 views

Parsing a std::string to a dictionary (std::map)

As always - the context ! Preliminaries Recently I needed a way to transport data from one environment to another. Though the proper way (probably) to do this would be to use databases (which I don't ...
-1
votes
1answer
185 views

A New Sorting Algorithm

I have designed an Algorithm for sorting numbers. This algorithm works by sorting an array of any length with random, non-repeating whole numbers in a linear manner by ascending order. Under the ...
3
votes
1answer
89 views

(Google Foobar XOR Checksum Challenge) How do i optimize this function to run for much larger values?

I wrote a function that accepts 2 integers start and length as input and returns the bitwise XOR of certain numbers as described below: ...
0
votes
1answer
60 views

Generating floats from integers

I wanted to make an algorithm that is a counter-example of Cantor's diagonalization argument. Given an integer, this Python code will produce a unique rational number. Fed the sequence of positive and ...
3
votes
1answer
129 views

Deque class for .Net (C#)

As .Net has no Deque class (Double-Ended Queue) in the Frameworks and I couldn't find an example implementation that I was happy with, I have written the following with the intent that I may post it ...
2
votes
0answers
113 views

Parallel Breadth First Traversal in Rust

Overview I have been writing my own graph API for Rust and one thorny problem I wished to tackle was a parallel breadth first traversal of the graph. The API itself will have different graph flavours, ...
1
vote
0answers
47 views

Eulerian Circuit for directed graphs

Summary It's been quite a while since I last touched algorithms and Python. This piece of code finds the eulerian circuit for the directed graph. A bit convoluted and its structure leaves much to be ...
2
votes
0answers
29 views

Maximizing Efficiency of Collatz Conjecture Program Python

My question is very simple. I wrote this program for pure entertainment. It takes a numerical input and finds the length of every Collatz Sequence up to and including that number. I want to make it ...
3
votes
2answers
100 views

Rabbit Searching Problem in Rust

This is my Rust implementation of the rabbit searching algorithm I found yesterday in this video. The problem statement is as follows. There are 100 holes in a line. A rabbit is in one of the holes. ...
1
vote
1answer
61 views

Ordered unique union function

I made a user-defined function union in VBA, such that: it could take variable parameters each parameter is a one-column range like ...
1
vote
2answers
89 views

Find most occurring word in a sentence

Here is my C code for finding the most occurring word in a sentence: ...
1
vote
2answers
77 views

Bottom-up merge sort

I think I programmed a bottom-up merge sort, but I'm a little sceptical that it will work under any data set. By now, I've tested it with many random arrays of massive lengths, and it seems to work; ...
0
votes
0answers
48 views

Cookbook app in next.js

To start I'd like say that I'm a beginner writing my first Next.js app though I feel this question is more React related. I'm building an app where an user can add/edit stored food, recipes etc. which ...
1
vote
2answers
240 views

Given an integer N, returns the smallest integer greater than N, the sum of whose digits is twice as big as the sum of digits of N

Write a function: class Solution { public int solution(int N); } that, given an integer N, returns the smallest integer greater than N, the sum of whose digits is twice as big as the sum of digits of ...
0
votes
1answer
37 views

Safely and Quickly Trim a Byte Array of Audio in Java

I'm trying to take a raw byte array of audio and trim it. The user selects a start and end value using a range slider. ...
2
votes
1answer
55 views

A Lowest Common Ancestor (LCA) Tree Algorithm

My implementation of the lowest common ancestor (LCA) algorithm for my tree data structure. I selected the non-preprocessed (naïve) implementation for this first implementation. Support for any number ...
2
votes
2answers
71 views

Shunting Yard Calculator - Extended to negative, complex, etc

This project is based off the shunting yard algorithm and has additional features such as negative value parsing. It's like a scientific calculator. I am looking to improve this project by getting rid ...
0
votes
0answers
24 views

OCaml implementation of two-sum

I'm implementing some simple algorithms (e.g. two-sum) in OCaml and I am unsure why my OCaml solution is running much slower than the equivalent Python version. My OCaml version: ...
1
vote
0answers
55 views

In a search of perfect batch script for editing the hosts file

I want to edit Windows hosts file in the best way for the end user, using a batch-only solution. I ended up with this script, which works fine, but since I don't have enough knowledge, the syntax is ...
4
votes
2answers
114 views

DNA Challenge from CS50

Added the problem link below and my implementation of it. Took me a few hours to program. I have added comments to explain my thought process. Problem Link Goal - To implement a program that ...
3
votes
0answers
39 views

How to remove a lot of the duplication in this JavaScript AST transformer?

I have put together this complete runnable source code with an example set of test expressions which it parses and prints. The code requires acorn, which is a JS ...
0
votes
1answer
44 views

Assigning Tasks to Entities (or some other entity) based on Priority

Main Questions: Is my runtime analysis correct? Is my approach optimal or close to it? Is there a better way and/or improvements? Objective: Write an algorithm to assign ...
1
vote
2answers
93 views

Getting the total image value

In a previous question that I asked: Get image value total, I learned to use LockBits to speed up my code execution's time. Since then I have changed my code to Ham the distance instead of just ...
4
votes
1answer
88 views

Determine whether an array contains duplicate values

My solution for the leet code problem to search an array of ints for duplicate values and return a boolean seems quite efficient (< 90% of submissions runtime). However, I am currently reviewing ...
-1
votes
1answer
42 views

is my Prim's MST implementation terse enough?

I did research because I want to learn Kruskal's and Prim's MSTs. I did research and after a solid understanding, I went to my textbook and got their libraries: https://algs4.cs.princeton.edu/code/...
0
votes
0answers
23 views

Clarity and optimization of kmeans algorithms

I have written this k-means algorithm without using any class structure. The audience of my code are students who are pretty new to Python. Is it possible to review the code so that it is more ...
1
vote
1answer
48 views

A possible implementation of std::rotate

I've implemented this rotate_ algorithm mimicking STL's : The algorithm left-shifts all the elements n times where n is the distance between the first element and the axis (middle): ...

1
2 3 4 5
97