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.
4,472
questions
-5
votes
0answers
17 views
can u explain how to solve [closed]
`>int j=0,k=0;String[] y=new String[q];
> int[] result = colors[0];
> for (int row = 1; row < n; row++) {
...
-3
votes
0answers
16 views
How do you make a scrabble game in java without graphics? [closed]
I was able to figure out the letter point system when you put your letters on the grid, however, I don't know how to add in a 2d array of a scrabble board (without graphics) with the doubling and ...
3
votes
1answer
37 views
Contains all elements and duplicates are taken into account
The idea is to have a function which checks if all elements in a list are contained in another list. But not just the containedness has to be true, but also the times of containedness.
One could say ...
4
votes
2answers
268 views
ASCII Random Walk Algorithm
I would like some advice on improving/optimizing a small program I wrote. What it does is run a random walk on a torus manifold with 8 colors that loop around.
...
1
vote
1answer
40 views
Soduku Generator and Solver
This is my approach on creating a sudoku generator and solver with backtracking.
Is right for sudoku generator to inherit from soduku solver?
What is your general overview on the class structure, data ...
4
votes
2answers
108 views
Merge/Insertion sort program taking longer than an hour to finish - C++
My program uses merge and insertion sort to sort a set of numbers.
It works perfectly fine for small inputs, less than 2 seconds for 1,000 ints. But I need to sort 1,000,000 ints. When I try with 1 ...
7
votes
1answer
88 views
Polynomial multiplication using Karatsuba method
I am trying to solve a problem, which requires me to output the xor of all coefficients in the product of 2 input polynomials. Having seen that the normal O(n^2) ...
0
votes
0answers
36 views
djikstra vs floyd warshall C++ [closed]
I was trying to implement a solution to find the shortest path between all pairs of nodes in a directed graph.
I didn't know Floyd-Warshall in the beginning, so my approach was to create a stack and ...
7
votes
1answer
357 views
Combinatorics, C++ and optimization
Note: I have a working solution, my question is about optimization and other approaches.
Problem Description
Hello, I'm re-writing an old program I made that solves nonograms.
As part of solving it, I ...
7
votes
1answer
69 views
AI for OpenTTD - A* Path Finder
I'm writing an AI in C++ for OpenTTD, and the first component is a path finder. If you'd like to run it yourself, it's on Github here: https://github.com/marlonsmith10/empire_ai. Currently, it will ...
0
votes
0answers
44 views
How to simplify and optimize bitwise get/set operations on a large bit buffer in JavaScript?
tl;dr: How could you rewrite the buf_get and buf_set functions below to be most optimal?
It took me a long time and a lot of ...
4
votes
2answers
135 views
JavaScript implementation of Symbol Table (Dictionary)
I'm currently going over Robert Sedgewick Algorithms book. Here I'm implementing a Symbol Table using a Linked List. The Sequential Search Symbol table is implemented in JavaScript. The book mentiones ...
1
vote
0answers
33 views
Instantiation logic within a specific object vs factory object
I want to program file lines transformation in a game initialization context and I am asking about best OOP practice.
I have a MockConfigFile that implements a <...
1
vote
1answer
54 views
LeetCode 863: All Nodes Distance K in Binary Tree
I'm posting two similar solutions for LeetCode's "All Nodes Distance K in Binary Tree". If you'd like to review, please do so. Thank you!
Problem
We are given a binary tree (with root node <...
3
votes
2answers
34 views
Using Token Bucket to rate limit function calls
This program is my attempt at solving the below problem:
We have a function which is going to be called by multiple threads. The problem is to find a way to limit the number of times this function is ...
3
votes
2answers
211 views
10 Kinds of People Open Kattis Problem Time Limit Exceeded C++
I am trying to solve the open kattis problem '10 kinds of people' (https://open.kattis.com/problems/10kindsofpeople) using a best-first search algorithm and c++.
10 Kinds of People
The world is made ...
3
votes
2answers
182 views
Finding sum in an array
I have this program finding 2 numbers in the array (unsorted) so that its sum equal to the target sum. Here is my program:
...
3
votes
1answer
60 views
Number of inversions on a segment
I was trying to do this question and I get TLE on one the test cases (the actual link to the question will ask you to login).
What is an inversion?
An inversion of an element is the number of ...
2
votes
1answer
74 views
Algorithm to find bucket and item indices from power of two bins?
So this is building off of Algorithm for dividing a number into largest "power of two" buckets?. Here is a slight modification of the answer from there:
...
3
votes
1answer
59 views
Marching Square algorithm (2)
The following source code is a solution to the Marching Square problem.
The explanation of using random numbers in ambiguous cases can be found here.
...
3
votes
1answer
80 views
Mergesort in python
My mergesort implementation currently takes a very long time, for a list with 1 million elements it takes over 130 seconds to get the list sorted.
Could someone kindly have a look at my code and ...
4
votes
1answer
64 views
Checking if a value is in the RGB range and then increment/decrement
Is there a better way to check if the variable x is in the 0-255 range and:
if x is less than 255, add ...
2
votes
1answer
51 views
Binomial Expansion Calculator
So i wrote a program with the documentation for my fx cg50 calculator's micropython to calculate various items, each of which are:
Pascal Triangle Entry
Pascal Triangle Level/Entire Row
Binomial ...
2
votes
3answers
71 views
Efficiently calculate the value of an arithmetic sequence
I need to calculate the value for the following function:
$$f(n) = -1 + 2 - 3 + ... + -1^n$$
The time limit to calculate a given value of \$n\$ is less than 1 second. My approach does not meet that ...
2
votes
1answer
61 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 ...
3
votes
2answers
81 views
Is the following divide and conquer recursive algorithm for the exponentiation more efficient than the iterative one for large numbers?
I have the two following algorithms. My analysis says that both of them are \$\mathcal O(m^24^n)\$ i.e they are equivalent for big numbers (more than 32 bits). Is this right? Note that ...
3
votes
1answer
65 views
Sudoku Solver using rules of Sudoku and backtracking
This code tries to solve the sudoku board using the rules of sudoku. When it does not make progress in solving, it assumes a cell value and tries again. Please review my code and help me understand ...
3
votes
1answer
54 views
Data analysis code (finds coincidences in three sets of data)
I've written the following data analysis code (using just a couple lines from CERN's ROOT data analysis framework). It's designed to find coincidences in three sets of data (timestamps from an ...
4
votes
1answer
80 views
Multiple Pandas Ranking Operations within a Loop - Better Optimization and Performance
I have implemented the following code which works as intended. However, I would like to improve my code in terms of performance and efficiency
Code in Question
...
2
votes
2answers
79 views
Different approach Union Find
I am studying algorithms and I did this "Union Find Like" algorithm.
I have one array of objects with a reference and I make the union pointing to the same reference instead of have two int[]...
3
votes
1answer
75 views
Sorts and data structures: a bucket sort
I am currently going through and implementing basic algorithms and data structures as I feel I didn't learn enough about this during my Data Structures and Algorithms unit. So far I've done the ...
0
votes
0answers
69 views
Kattis: “watering Grass” Interval covering problem, why is my code so slow?
im currently practicing, doing a python implementation of this challenge: https://open.kattis.com/problems/grass
Problem described here:
...
3
votes
0answers
34 views
Insertion sort for std::forward_list
I'm practicing writing insertion sort for std::forward_list. But I find myself creating many variables and using many if statements. Looking for inputs on how to make the function more efficient, ...
15
votes
2answers
2k views
C++ chess game engine using Minimax and alpha-beta pruning;
My chess game is over, everything has been finished, except for some special (like en passant)moves.
The main part of the game is its engine which I have coded using the Minimax algorithm with alpha-...
0
votes
0answers
23 views
Finding strongly connected components in a directed graph using Kosaraju's algorithm
I'm working through Stanford's algorithms course on edX.
One of the programming assignments is to find strongly connected components using Kosaraju's algorithm, on a graph with ~875,000 vertices. My ...
2
votes
2answers
75 views
Exponentiation by squaring in x64 Linux Assembly
I am learning some assembly for a compiler project I am working on and I have come across the Exponentiation by Squaring algorithm when it came to calculating x ^ n. To get a grasp on how the ...
4
votes
2answers
91 views
JavaScript Priority Queue implementation using a binary heap
I'm currently going over Robert Sedgewick's Algorithms book. For the implementation of A priority queue using a binary heap I implemented the code using ES6. I believe to have more experience with ...
3
votes
1answer
85 views
Bubble Sort In C++
I'm learning modern C++ and also algorithms, and am doing some practice by coding up some simple algorithms. Also trying to use templates whenever possible as I'm quite unfamiliar with generic ...
5
votes
1answer
149 views
6
votes
1answer
154 views
Python: Astar algorithm implementation
I have implemented Astar algorithm for a problem on an online judge relating to maze given start and end positions along with a grid representing the maze. I output the length of the path along with ...
3
votes
1answer
176 views
Memory/Time usage on substring search code
I have implemented a substring search, which works as expected and provides the right output. On some edge cases tho, I get either Memory or Time limit exceeded because:
the string length is too big
...
0
votes
2answers
167 views
Sum of 2 numbers in an array [closed]
I have this problem to solve:
Given an integer \$z\$ and an array \$a\$ of distinct integers sorted in ascending order, find two numbers, \$x\$ and \$y\$, belonging to the array such that \$x + y = z\...
3
votes
1answer
112 views
Sentinel linear search
I'm pretty new to algorithms, and it's my first attempt to implement sentinel linear search in JS. I'm wondering if it can be improved.
...
9
votes
2answers
1k views
Can I draw a square with pixels more efficiently?
I'm drawing a square on a window using an implementation of Xlib. I put a colored pixel with my_pixel_put at a specific (...
2
votes
0answers
63 views
Marching Square algorithm
The following source code is a solution to the Marching Square problem.
The explanation of using random numbers in ambiguous cases can be found here.
...
4
votes
2answers
254 views
Floyd-Warshall Path Reconstruction
Below is the implementation for the Floyd-Warshall algorithm, which finds all-pairs shortest paths for a given weighted graph.
The function floyd_warshall takes a ...
1
vote
0answers
44 views
LeetCode 146: LRU Cache (Go)
Posting my code for LeetCode's LRU Cache, if you'd like to review, please do so, thank you for your time!
Problem
Design and implement a data structure for Least Recently Used (LRU) cache. It should ...
5
votes
1answer
38 views
Selection Sort Java and Analysis
I have written a selection sort code in java. I know its very elementary algorithm, but since i am learning, so wanted your input about the quality of the code. Please have a look at the code:
package ...
1
vote
1answer
64 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
1answer
28 views
Update UNIX-like system user profile picture with its GitHub account's one
Seeking to learning and have fun, I wrote out this script (what I expected to be) self explanatory and would appreciate some comments on that.
I published it as a gist and tried to do my best on what ...