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
3
votes
0answers
84 views

Find potentional sponsor for every animal

This is a programming challenge in Czeck. My translation follows. Does anyone know what the time complexity of my code is? Input: The first line contains 2 integers: the number of animals 1 ≤N ≤100 ...
1
vote
0answers
41 views

ASCII-based OOP blackjack game in Python

I'm new to programming and have been teaching myself to code for about 6 months. Since I have no one to ask about any of coding-related questions, I may have developed bad habits in the meantime, but ...
3
votes
2answers
62 views

Finding Number of Objects in Image

I wrote a program that, given a 2D array representing pixels in a birds eye view image, it returns the number of Objects in the image. Pixels that are connected in the up, down, left, and right ...
0
votes
0answers
44 views

Function that measure time of functions

Hello everyone! I would like to introduce you to my function that measures complexity-time of functions, and also their N's operations. I would like to enhance my function (as separate file f.e ...
2
votes
1answer
59 views

Divide array into three disjoint sets with equal sum

Problem definition : Array partition problem Given an array of positive integers, divide it into three disjoint subsets having equal sum. These disjoint sets cover the complete array. Example Input: [...
1
vote
3answers
99 views

Count how many iterations of deletion until array is ordered

I'm trying to write a program whose input is an array of integers, and its size. This code has to delete each element which is smaller than the element to the left. We want to find number of times ...
-5
votes
0answers
26 views

How do I express the right bounds for binary search? [closed]

There are lots of questions in leetcode that make use of binary search or a slight variant of it. Examples include: https://leetcode.com/problems/kth-largest-element-in-an-array/ https://leetcode.com/...
5
votes
2answers
9k views

Constraint Programming: Map color problem

I've written some python code to solve the map coloring problem. In my code, I represent the problem using Territory and MapColor...
1
vote
0answers
9 views

Mean and variance of element-wise distances in a set of multi-dimensional arrays in Matlab - follow-up

This is a follow-up question for Calculate distances between two multi-dimensional arrays in Matlab and Mean and variance of element-wise distances in a set of multi-dimensional arrays in Matlab. For ...
0
votes
1answer
43 views

Hackerrank Gridland Metro - using Interval Cover Solution

Hi there I was hoping to get a better grasp of greddy algorithms while trying to solve this problem: Hackerrank: Gridland Metro and I was trying to apply an algorithm where each grid could be ...
2
votes
1answer
100 views

Projecteuler.net Problem 2 using collection pipeline Pattern

I solve projecteuler.net Problem 2 deferent way Generate number from 1 to range ex 100 and get the even number Get Fibonacci numbers from list Reduce array I have one problem with a large set of ...
2
votes
2answers
58 views

Leetcode First Unique Character in a String code optimisation

I was working on First Unique Character in a String Question Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. Example 1: ...
3
votes
1answer
61 views

Segmented Sieve of Eratosthenes with wheel factorisation

Problem I have a project in which I implemented variants of the Sieve of Eratosthenes as well as benchmarking and profiling harnesses for these (which can be ran with ...
1
vote
2answers
110 views
2
votes
1answer
52 views

Please verify Jump searching algorithm

Please verify jump searching algorithm that I have written and let me know if any bugs are present. I have tested it from my end, but still would like to get it verified for any missing corner ...
0
votes
1answer
68 views

Calculate square of (sum of even-indexed) - (sum of odd-indexed) subarrays from an array

Find all subarrays from a given array in the least possible time complexity. Requirement: calculate the square of (sum of even-indexed elements) - (the sum of odd-indexed elements) of al the subarrays....
3
votes
1answer
102 views

Recursive solution of ordered Coin Combinations II (CSES)

Question Link Consider a money system consisting of n coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ordered ways you can produce a money sum x using ...
3
votes
3answers
111 views

Comparing 8 different Disjoint-Set data structure variants in Java

The Wikipedia page on Disjoint-Set data structures presents \$4\$ distinct algorithms for finding the root node of the tree, and \$2\$ distinct algorithms for performing the union operation. In this ...
3
votes
1answer
277 views

LeetCode 767: Reorganize String

Here I'm posting my code for the Reorganize String problem on LeetCode. If you have time and would like to review, please do so, I'd appreciate that. On LeetCode, we are only allowed to change the ...
4
votes
1answer
301 views

A Basic HashMap (Python)

A hashmap is a data structure that implements an associative array abstract data type using keys and values and has a hash function to compute an index into an array, from which the desired value can ...
-1
votes
1answer
65 views

Order array based on rating in a specific order

I have to sort the an array based on rating in a specific pattern. 3 star = 5, 4star = 3 , 5 star =7 [3 star hotel, 4 star hotel, 5 star hotel] Note: Replace 3 star hotel with 4 star hotel if 4 star ...
1
vote
0answers
49 views

Dictionary based non-local mean implementation in Matlab

Given a dictionary including multiple X-Y pairs where X, Y are both three dimensional structure. dictionaryBasedNonlocalMean function return a ...
1
vote
2answers
104 views

checking is ellipse is filled or not

following code below works fine but feels kind of a mess. Any one has a better way to do this? I am creating a basic "Enter Pin" to unclick screen on an app. So if the user enters the 1st ...
2
votes
2answers
67 views

C++ Splay Tree implementation

I am coding a Splay Tree class in C++. I am looking for code review. Currently, my Splay Tree class has search and rotate function. I feel that I'm doing things overcomplicated. ...
1
vote
1answer
61 views

Sink algorithm in Max Heap Correctness

I am trying to determine the correctness of a helper method within my MaxHeap class. The goal of this helper method is, given the index of any node in the Max Heap, sink it to the correct level in the ...
0
votes
0answers
19 views

Order array based on rating in a specific pattern [duplicate]

I have to sort the array based on rating in a specific pattern. 3 star = 5, 4star = 3 , 5 star =7 [3 star hotel, 4 star hotel, 5 star hotel] Note: Replace 3 star hotel with 4 star hotel if 4 star ...
2
votes
2answers
48 views

Find the 'n' most frequent words in a text, aka word frequency

I'm doing freeCodeCamp's Coding Interview Prep to improve my JavaScript skills. This challenge is called "Word Frequency", and is based on the Rosetta Code's entry of the same name. The ...
3
votes
1answer
141 views

Algorithm Optimization -- Automatic Dimensionality of PCA

I have implemented (rather, edited the implementation of) a technique to automatically detect the optimal number of dimensions for PCA, based off of this paper. This was inspired by ...
0
votes
0answers
20 views

Library To Help Looping Redesign [duplicate]

I have changed the design of my library to help looping with generators. The new version uses iterables which allow a data structure or generator to be passed to the functions. The iterators are then ...
3
votes
1answer
1k views

Shortest way to form a string out of a subsequence of a string

The task is taken from LeetCode (subscription required) - From any string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions). Given two strings ...
0
votes
2answers
68 views

Python Quicksort Implementation with duplicates

Please critique my implementation of Quicksort in python 3.8: ...
1
vote
0answers
80 views

Parallel 3D Discrete Cosine Transformation Implementation in Matlab

I am trying to implement 3D Discrete Cosine Transformation calculation in Matlab with parallel computing parfor. The formula of 3D Discrete Cosine Transformation is ...
0
votes
1answer
64 views

Graph coloring problem with Spark (JAVA)?

I am trying to create the algorithm described in the image from this link Simply put any two adjacent nodes must not share tuples and if they do they are colored with the same color. If there's a ...
2
votes
2answers
87 views

Algorithmic Efficiency: GetShortestUniqueSubstring implementation

I came upon this Youtube video and the problem they discussed piqued my interest, so I decided to take a stab at it. Below is the question: Given an array of unique characters ...
10
votes
6answers
4k views

Enhanced Hashmap - Add a number to all keys/values

I had the below problem in a coding test and I got 28/30 tests passes and 2 failed due to a time-out. Problem You have created a programming language and now you have decided to add ...
0
votes
0answers
88 views

Simple Inverted Index made in python

So I made an inverted index following a tutorial online and was wondering if I can improve it by implementing one or more algorithms or reduce code or maybe make the processing files faster. I am ...
2
votes
2answers
127 views

C++ - Tic Tac Toe AI powered by minmax algorithm

This was my previous post on Tic Tac Toe in C++. I received very good feedback and tried to implement all of the improvements. I could not implement some due to the AI needed functions or complexity. ...
2
votes
1answer
199 views

Bitcoin Hash (Merkle) Tree implementation in Java

I'm learning about how full nodes can send a Merkle root + a list of hashes, so that a light client can verify a transaction. I couldn't find any good resources on how to implement it. I just knew ...
3
votes
1answer
133 views

Bancroft's method implementation

Background I've written an algorithm to solve the three-dimensional Time Difference of Arrival (TDoA) multi-lateration problem. That is, given the coordinates of n ...
1
vote
1answer
125 views

Priority Queue implementation for top k frequent elements leetcode in Ruby

While working on the following leetcode problem: https://leetcode.com/problems/top-k-frequent-elements/ I used a Priority Queu implementation in Ruby. I have learned how to implement a Priority Queu ...
2
votes
0answers
127 views

Kruskal's and Prim's algorithm (Minimum spanning tree)

I completed my class assignment, and I would like to have a critical code review of this implementation. How can I make this more concise, pythonic and readable? Also, while writing docstrings, is ...
5
votes
4answers
367 views

Disjoint-set data structure (C++)

I'm an intermediate-level C++ programmer aiming to write reference-quality code. I hope to learn about any mistakes in this piece of code and blind spots in my understanding of concepts. Notes: The ...
0
votes
0answers
9 views

Socket.io: Pairing from two rooms without repeated pairing?

I'm working on a user-pairing algorithm using socket.io, and it is running on Node.js. I want to make sure that no repeated pairings will happen when people connect in. The steps of the algorithm as ...
1
vote
0answers
63 views

3D Non-local Mean Algorithm in MATLAB

This is a follow-up question for GetNeighborhood function for 3D cells structure in MATLAB. Based on the existing 2D non-local algorithm, I am attempting to implement 3D non-local mean algorithm in ...
2
votes
1answer
47 views

Python: lru_cache make leetcode's dungeon game more slower

I'm trying to implement a recursive+momoize version for leetcode's dungeon game question. I tried to use @lru_cache(): ...
2
votes
0answers
27 views

Unsupervised competitive learning algorithm from scratch in javascript

I'm a mathematician who is new to programming and I'm currently reading the book "Theory of Neural Networks" by Rojas. To become better, I try to program every algorithm that is described in ...
5
votes
7answers
4k views

Aho-Corasick for multiple exact string matching in Java

I have this program that compares performance of two algorithm for multiple exact string matching: Given a set of patterns and the text, find in the text all occurrences of any pattern. For example: <...
1
vote
2answers
56 views

Identifying indices of non-delimited characters in a string, supporting greater than 1 length of delimiters

The code below takes a string that potentially contains a true match for a delimiter. There can be multiple delimiters located anywhere in the string. We need to obtain the indices that are not a ...
1
vote
1answer
77 views

Find Kth Element in the merged two sorted arrays?

We have been given two sorted arrays and a number K . We have to merge the two sorted arrays in the sorted manner and return the element at the Kth position. My approach is to use two variables ...
2
votes
2answers
103 views

Simple credit card validation with Luhn's Algorithm

Originally written in C (which was abysmal, you may check here if you want), I rewrote my simple credit card validation program using Python ...

1
2 3 4 5
96