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
0
votes
1answer
79 views

Binary search to find the closest element

I wrote a binary search to find the closest element to x in an array. How can show the correctness of the algorithm for any cases? Because it handles with floating points and variable length arrays, ...
0
votes
0answers
66 views

How to do kendalls tau in O(n log n)? O(n^2) is shown

I am trying write a method that will calculate kendalls tau in O(n log n) time. I have a managed to it in O(n^2) quite easily however I cannot find any GOOD explanation online that will describe with ...
6
votes
2answers
141 views

Python backtracking algorithm to solve sudoku

I have recently gotten back into writing python and i decided to take up a project of writing a backtracking algorithm in python to solve Sudoku puzzles How it Works A matrix is declared to represent ...
0
votes
3answers
50 views

Javascript: Reducing redundancy of notation nested value

Hello I have this function that looks for nested value 3 levels deep currently ...
4
votes
4answers
143 views

Better way to minimize array elements

I would like the array elements to minimize by preserving their orders. For example: 3,7,9,7 is given 1,2,3,2 is yielded ...
2
votes
2answers
82 views

An implementation of Uber's “Fare Estimator”

CodeSignal put out a challenge by Uber that involves writing a "fare estimator" that involves a function dependent on cost per minute, cost per mile, ride time and ride distance. The formula ...
0
votes
1answer
49 views

Shortest Path to Get All Keys in JavaScript

I build the Shortest Path to Get All Keys algorithm in JavaScript. It's working properly, but with a really bad performance. The problem: You are given an m x n grid grid where: '.' is an empty cell. '...
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 ...
1
vote
1answer
65 views

Remove duplicated multiline blocks in text

A quick and dirty line-based approach for detecting duplicated text blocks. Did not find any references as to what (deterministic) algorithms are there for detecting duplicated text blocks, so went ...
4
votes
1answer
145 views

A different approach to string pattern matching algorithm

Although there exists the most efficient "Knuth-Morris-Pratt" algorithm of string pattern matching. I tried to achieve the same result with a different approach. But I am not sure about it's ...
1
vote
2answers
95 views

Leetcode kth largest element without using heaps

I was working on kth largest element problem on leetcode Question Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in ...
5
votes
2answers
501 views

Given an array of integers, return the minimum required increment to make each element unique

The task is: Given an array of integers, return the minimum required increment to make each element unique. Sample Input 1: [3, 4, 2, 2, 6] Sample Output 1: 3 (One of the 2s should be incremented to ...
10
votes
4answers
3k views

First non-repeating Character, with a single loop in Python

I recently tried to solve the first non-repeating character problem. Please tell me if my solution is valid. I'm aiming for O(n) with a single loop. My thinking is, it would be easy to tell you what ...
1
vote
1answer
65 views

Maximum Profit in Job Scheduling - Performance Issue

I implemented the Maximum Profit in Job Scheduling algorithm in JavaScript, but I'm having performance issue. The problem: We have n jobs, where every job is scheduled to be done from startTime[i] to ...
2
votes
2answers
84 views

Find the highest product of three numbers in a list (Python)

I wrote the below as a solution to: Problem Find the highest product of three numbers in a list Constraints Is the input a list of integers? Yes Can we get negative inputs? Yes Can there be ...
5
votes
2answers
93 views

C++ Connect-four game with little AI (Version two)

Here is the improved code ...
3
votes
0answers
81 views

Simple Leaky Bucket Async and Low Footprint

I'm working on a simple Leaky Bucket algorithm. I have found a lot of samples on the internet, but something always bothers me. Most of them use Collections and DateTime to track current actions and ...
2
votes
1answer
99 views

In how many different ways can a group of 9 people be divided into 3 groups, with each group containing 3 people?

To divide 9 persons into 3 groups, when the ordering of groups is not important can be done in (9C3 * 6C3 * 3C3) / (3!) = 280 ways Here, we divide by (3!) because the ordering of 3 groups is not ...
2
votes
2answers
108 views

Sort three numbers

So, I wrote this function to sort three numbers in Julia. Any feedback on readability, performance, and Julian-ness would be appreciated. ...
1
vote
0answers
48 views

Genetic Algorithm Typescript

I learnt a little about genetic algorithm. And I made a program from typescript that tries to evolve and match word/sentence using genetic algorithm. But if I give a little longer sentence it takes ...
2
votes
1answer
54 views

Turn an array of pairs of start and end points into an ordered series

Given an array of pairs, for example: [["Denver", "Miami"], ["Miami", "Tulsa"], ["LA", "Okmulgee"], ["Mobile", "Portland"...
2
votes
2answers
90 views

Print Binary coded decimal Numbering of a given input number

Example 1 input:3 output:0011 Example 2 input : 15 output: 1111 in the below example code 15 is input. I have taken 8 4 2 1 as array for Binary coded decimal numbering this code is working as ...
1
vote
2answers
91 views

Find largest palindromic substrings in a string

I have written some code to find the largest palindromic sub-strings in a string without repeating any character. Example input abappabaxhhh output ...
3
votes
3answers
1k views

Printing rotations of an array 7 times

Loop through a given array 7 times and print the following output: int[] arr = { 9, 2, 7, 4, 6, 1, 3 }; ...
2
votes
1answer
79 views

Process Tasks Using Servers [closed]

A problem from leetcode: https://leetcode.com/problems/process-tasks-using-servers. I had a simple solution for it, that's using min heap to store all servers and dictionary to check whether there're ...
0
votes
0answers
50 views

Sliding Puzzle Problem in JavaScript

I'm trying to solve the Sliding Puzzle problem in JavaScript. The problem: ...
2
votes
0answers
32 views

Convolution layer using numpy in python

I'm trying to make a CNN in python using numpy. I have a finished product but it seems that it can be improved. On testing the convolutional layer is the biggest bottleneck ...
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 ...
1
vote
1answer
109 views

Cheapest flights within k stops algorithm in JavaScript

The problem: There are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is ...
3
votes
2answers
63 views

Javascript best way to filter out empty values and simplify reusable function

Hello I have this function that takes array of objects and takes a specific value from the object and creates array of strings that can make mapped to dropdown selector. ...
1
vote
1answer
44 views

PureScript breadth-first search implementation

For context, I wrote a function, get_servers, for the game Bitburner which is usually played by writing JavaScript code. Instead, I used PureScript to write the ...
0
votes
0answers
24 views

"Smoothing" colors in mutliple-color gradient in fragment shader

TL;DR How to better avoid some of the colors in the gradient looking like brighter/darker lines and make them blend in with neighboring colors. Not too concerned with performance/color accuracy but ...
0
votes
1answer
48 views

Calculate the minimum amount of billing hours

Let's say I'm implementing a method that calculates the number of hours I'm billing a client. I only work in blocks of K hours: ...
4
votes
1answer
80 views

Rust Persistent Red Black Tree Implementation

Below is my implementation of a persistent red black tree in Rust. I have a few questions about potential improvements. Currently the data and nodes are stored in referenced counted pointers. Is this ...
6
votes
1answer
90 views

Assembler. Algorithm for incrementing a decimal number

I share an algorithm that I have written myself. It is useful for incrementing any digit of a decimal number. That way, a conversion to decimal is not necessary. Please excuse the mistakes. I am ...
1
vote
2answers
87 views

Unsigned 64bit subtract-with-borrow in Standard C++

I'm writing a (compile-time-)fixed-width unsigned big integer. To implement subtraction, a subtract-with-borrow primitive is required. When efficient platform-specific implementation isn't available (...
0
votes
0answers
26 views

Calculate minimum buy/sell amount that exchange will allow the order to be placed based on current price

I have created calculateMinimumAmount to calculate the minimum buy/sell amount that exchange will allow the spot order to be placed based on the current price. The ...
4
votes
2answers
141 views

Two dimensional bicubic interpolation implementation in C

This is a follow-up question for Two dimensional bicubic interpolation implementation in Matlab and Two dimensional gaussian image generator in C. Besides the Matlab version code, I am attempting to ...
0
votes
0answers
40 views

Two dimensional bicubic interpolation implementation for dlarray in Matlab

This is a follow-up question for Two dimensional bicubic interpolation implementation in Matlab. While I am working on neural network designing, I want to compare the effects between using padding and ...
6
votes
1answer
77 views

Improving product configurator "algoritm"

I have created a product configurator in VueJS and the whole functionality of it runs on one single function. I've put the word algoritm in the title in quotes because I don't really know if it is ...
2
votes
0answers
116 views

Two dimensional bicubic interpolation implementation in Matlab

I am attempting to implement two dimensional bicubic interpolation algorithm in Matlab. The input is a two dimensional array and the output is the interpolated result. The test input matrix: ...
4
votes
1answer
188 views

Java Polynomial, while I know this works, help me make it better

As stated, I know this works but I want to improve this code! I don't expect anyone to re-write this but if you have some "try this instead" then great! ...
2
votes
1answer
100 views

a prototype of finding the (almost) best learning rate and initial weights so that a perceptron converges with the minimal iteration

First of all, I chose the nearest data points/training examples ...
4
votes
2answers
87 views

Two dimensional gaussian image generator in C

This is a follow-up question for Image Processing Median Filter in C. I am attempting to create a two dimensional gaussian image like below in C. The formula is as follows. The experimental ...
2
votes
2answers
371 views

Print all combinations from dictionary which will result in number N

Print all combinations from dictionary which will result in the number N ...
1
vote
0answers
106 views

C# Debiasing from Good PRNG

I am in need of a "debiased" clamp function, to generate a uniformly-distributed random number from a good PRNG. Let's assume that RNG in the below code ...
4
votes
2answers
155 views

Find all subranges of consecutive values in an array

I was looking at a dataset with a year column. I wanted all the years with matching a particular event. I then get a list of years which is not easy to read: ...
1
vote
2answers
141 views

Possible speed improvements for a Sudoku board validation algorithm?

I tested this algorithm on 9x9 boards and on average, if the board passed to the function is a (valid) solution, it takes 0.13-0.14 seconds for 1 million executions on my machine. I ran my code in ...
1
vote
1answer
83 views

C code in Data structures

Can someone give me any suggestions over my code that I made for this task? Also how much of 10 would rate it ? Suppose a simply linked list whose node is of type node and contains a student's name, ...
1
vote
2answers
66 views

Huffman compressor in plain C

I recently picked up C, because I am getting tired of the hell that is Java Enterprise development. I decided that writing a Huffman compressor will be a great little exercise, and I have always ...

1
2
3 4 5
96