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
5
votes
1answer
59 views

Breaking a line segment into two where it encounters a rectangle

I have developed a function that takes a list of line segments and a rectangle that breaks the line segment into two where it encounters the rectangle. Here is a picture demonstration of what it does: ...
1
vote
1answer
45 views

Arduino algorithm

I have 3 push buttons, 3 leds, and 2 relays. When you push button1 once, the led1 turns on and timer is set to 10 sec. When push button1 twice, led2 turns on and timer is set to 20 sec. When push ...
4
votes
1answer
63 views

Python Genetic Algorithm Implementation

I've made this to pick the LQR parameters for my self balancing robots simulation. So to break down what I have done, Randomly Create Population I create a random population. The population is a list ...
3
votes
0answers
49 views

Breadth First Search Block Puzzle

I am creating a program that will be given a text file as output with the board's dimension and the piece id along with the piece width and length. The goal is to arrange all blocks in the board ...
2
votes
0answers
34 views

Dijkstra from scratch

I implemented the Dijkstra algorithm from scrath and wonder if I can save some code? The whole algorithm is here. n is the number of nodes, and m the number of edges. 1 ≤ 𝑛 ≤ 10^4, 0 ≤ 𝑚 ≤ 10^5, 𝑢 ...
4
votes
2answers
65 views

Unique Paths II, dynamic programming problem, O(n^2) time, O(n^2) space

This is based on this leetcode question. I get the correct answer, but I know it could be much, much cleaner. I also know there is supposed to be an O(n) space solution, but I'm not sure how to ...
2
votes
1answer
49 views

Hash Table implementation in C using linear probing for collisions

I've written a simple Hash Table implementation in C, in order to use it in an IRC bot, so mostly storing nicknames, channel names, etc (small strings). I'm using linear probind to resolve collision. ...
1
vote
1answer
87 views

How to make this special binary-search algorithm more rusty?

I am solving one binary search problem in LeetCode. I have accomplished such code: ...
15
votes
4answers
4k views

Chess engine for chess without checks in C++

Project Description: Chess without checks: A chess variant where you can take the enemy king. This engine implementation is for chess without checks. Since checks don't exist, expect the code to not ...
0
votes
2answers
235 views

Generate all possible unique combinations of positive numbers these have sum equal to N

This is my function to generate all possible unique combinations of positive numbers these have sum equal to N. For example: If the input is 4 The output should ...
4
votes
1answer
62 views

Find optimum teams given calendar availabilities

I'm designing an algorithm that creates optimum teams based on everyone's availabilities (maximizes the amount of shared availability). To this end, I've made a function that takes in a dictionary of ...
3
votes
2answers
140 views

Tic-Tac-Toe in C language

I'm beginner on doing programming and just started learning C language. Then, i decide to make Tic-Tac-Toe as my first program. My Tic-Tac-Toe summary: User can choose whether want to play 2 player or ...
2
votes
1answer
48 views

PNG steganography tool in C

This is a steganography tool enabling you to conceal any file within a PNG file. In order to compile the program you need libpng to be installed on the system. It is one of my personal projects and I ...
3
votes
0answers
124 views

Quicksort using Lomuto partition scheme in C

This is my implementation of the divide-and-conquer Quicksort algorithm using the Lomuto partition scheme in C. This is part of a personal project and I'm following Linus Torvalds's coding style. <...
1
vote
1answer
73 views

Iterative Random Depth First Search implementation in Java

I implemented the iterative randomized depth-first search, with my goal for it to be performant, and have code that made complete sense. It utilizes ...
0
votes
0answers
34 views

Enumerating all fixed-length paths from a vertex in a graph

Enumerating all fixed-length paths from a vertex in a graph 1. Graph-related classes Base implementations of the relevant classes in my graph library. I'm using C#9 in a .NET 5 project. I opted to use ...
3
votes
1answer
2k views

Count the number of distinct subarrays

I want to determine the number of distinct subarrays that can form having at most a given number of odd elements. Two subarrays are distinct if they differ at even one position. The subarray is a ...
5
votes
2answers
103 views

Compressing a vector into a specific range

I want to solve the following task --- I am given a vector of integers. I want to 'compress' this list, where the elements will be replaced with numbers 0 through <...
5
votes
2answers
222 views

Tokenize arithmetic expression in C

This program tokenizes an arithmetic expression specified in infix notation. It is one of my personal projects and I would love to receive expert advice. After compiling, you can run the program with ...
0
votes
0answers
41 views

Shortest path algorithm for weighted graphs, targeting web-assembly

I have a C code, that takes edges of weighted graph and some point a It returns minimal distances from point a to others and previous point in way, which have minimal length (best way). For example, ...
4
votes
3answers
506 views

Split function using C and dynamic memory allocation

I have programmed a function to split strings, and it gives the expected output. I am looking forward to write better code and I was told here is a good place to start. Here's my program: ...
0
votes
0answers
25 views

Three Sum Algorithm

I made a randomized algorithm to solve the three sum problem. There are two key parameters: number of solutions, and total. Notably, the algorithm might generate a duplicate solution. This is not ...
2
votes
1answer
40 views

Linked list that support modifications during iteration

Update: Fixed last item is not removed during iteration Fixed clear() during iteration does nothing Fixed addition of removed item is ignored The lasted code can ...
2
votes
1answer
59 views

Leetcode 3 sum code optimisation

I was working on 3sum problem on leetcode Question Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the ...
1
vote
2answers
129 views

Counting numbers that have an even number of digits

I solved this problem: given an array/vector of numbers, return how many of them contain an even number of digits. Constraints: ...
5
votes
1answer
116 views

Quicksort Algorithm Speed

I have made a sorting algorithm in Python 3. This sorting algorithm is a modified optimized median of 3 quicksort, with network sorting for lists of size up to 16 and for finding the median. I have ...
6
votes
1answer
165 views

Calculator in C - Parsing arithmetic expression using the shunting-yard algorithm

This is a simple arithmetic calculator, which parses mathematical expressions specified in infix notation using the shunting-yard algorithm. This is one of my personal projects and I would love to ...
2
votes
2answers
48 views

Chunk a large set of characters by a specified set of delimiters and a maximum chunk size

Problem I need to be able to grab a subset of characters but ensure that each set doesn't exceed a specified limit and must end with a defined set of characters. For example, say I have an entire book ...
2
votes
0answers
75 views

Design a parking lot [closed]

I have tried to design the parking lot problem. Here is the problem statement. Design a parking lot with multiple floors where customers can park their cars. Each parking floor will have many parking ...
2
votes
1answer
127 views

How can I speed up my IDA* algorithm for N-puzzle?

I'm trying to solve the N-Puzzle problem using IDA* algorithm with a Manhattan heuristic. I already implemented the algorithm from the pseudocode in this Wikipedia page (link). Here's my code so far : ...
-2
votes
2answers
77 views

Hackerrank: Absolute Permutation - How do I make this code more efficient

The problem is basically this: Return the permutation of the set of the first n natural numbers which satisfies the following property: |pos(i) - i| = k ∀ i ∈ (1,n) where pos(i) is the ith number in ...
2
votes
1answer
100 views

Returning maximum number of consecutive 1s in list of binary numbers

This is what I implemented in Rust to get the maximum number of consecutive 1s in list of binary numbers. ...
2
votes
0answers
42 views

minmax evaluation with alpha beta pruning [closed]

I'm making some small board game and wanted to code a very simple greed AI with this algorithm. It turns out it doesn't play the most greedy moves, it is simply not working. I'd appreciate any ...
0
votes
3answers
401 views

sorting vector of pair in efficient way

I'm trying to solve the sorting problem but I always get TLE(Time Limit Exceeded/ 1s) so... can you guys suggest a more efficient way to solve this problem? (i can't link the exercise because it is on ...
1
vote
1answer
74 views

Java array extension/reduction

I work with the Oracle JGeometry method which returns (or takes as method argument) 2D segments as double array (x1,y1,x2,y2 ... xn,yn). I need to extend this array to contain 3D segments as double ...
1
vote
1answer
70 views
3
votes
1answer
80 views

AI for OpenTTD - A* Path Finder: Follow-up and unique_ptr slow performance

Original review here: AI for OpenTTD - A* Path Finder If you'd like to run the code yourself: https://github.com/marlonsmith10/empire_ai (use_smart_pointers branch) Changes have been made based on the ...
2
votes
0answers
34 views

Find all of the paths that lead to the minimum sum [closed]

"Find minimum path sum" in a 2D grid, is a very popular question which is solved by dynamic programming. But what if instead of the minimum sum value, we return all the actual paths that ...
1
vote
0answers
49 views

Making This "Fancy" Sort Algorithm a Little Better

This is my code: ...
-1
votes
1answer
135 views

Finding longest word in a sentence

A problem from Coderbyte. Find the longest word in a sentence after removing the punctuations and if two or more words are of same length return the first word Below is my solution: ...
0
votes
0answers
29 views

LC medium, matrix set zeros, accepted answer but not top

I've solved LC medium problem, set zeros. Below code is accepted and all test cases were passing. But my code came in the last 10% of accepted answers wrt performance. Can someone help me identify ...
5
votes
1answer
1k views

Connect 4 Game Design in C++

I am working on designing a simple version of a Connect 4 game. Here is my first draft code. I would like some feedback on it, and also required modification for these follow up questions: Add some ...
3
votes
1answer
112 views

Optimizing Dijkstra on grid Python (Microsoft Interview)

Given a square grid of size N, each cell of which contains integer cost which represents a cost to traverse through that cell, we need to find a path from top left cell to bottom right cell by which ...
3
votes
2answers
163 views

How to speed up the code for LeetCode "Container with most water" task?

I'm trying to solve the LeetCode question where you need to find out the area of the container with the most water. I have created the solution that seems to work in my testing but it fails for being ...
0
votes
1answer
76 views

Algorithm For Longest Palindrome

I made an brute force algorithm to find the longest palindrome within the string. This palindrome is a substring of the input. Below is the code. ...
2
votes
3answers
85 views

Sort an array of integers with a hashset

I was trying to make a sorting algorithm for an array of integers. Here's are the steps/theory: It turns an array into a HashSet, iterates over every integer value ...
0
votes
2answers
76 views

Next different permutation in C

This is a program which you can get the next "different", i.e. elements in A[] can be identical, permutation at each call. I've written a C++ program for ...
0
votes
0answers
67 views

UVA 101 - "The Blocks Problem"

I have been solving the problem from UVA 101. In this problem, there will be at most 24 position in a row at beginning, and this array is zero-indexed, and for each ...
1
vote
1answer
75 views

return the number of times is state of bulb changed?

Today in an exam I faced a problem which is Toggled Switches(Hacker Earth) as it is an exam question So, I am unable to send the question or its link (exactly). Problem: we have to print a number ...
4
votes
2answers
282 views

implement the merge functionality for 2048 with JavaScript

I am trying to implement the game 2048 using JavaScript. I am using a two-dimensional array to represent the board. For each row, it is represented using an array of integers. Here I am focused on ...

1 2 3
4
5
96