Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
0answers
25 views

Machine learning, kNN and Naïve Bayes algorithm

This is the task I am working on: In this assignment you will implement the K-Nearest Neighbour and Naïve Bayes algorithms and evaluate them on a real dataset using the stratified cross validation ...
1
vote
0answers
36 views

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

The task is taken from LeetCode - From any string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions). Given two strings source and target,...
2
votes
1answer
38 views

A Connect Four evaluation function in Java is not smart enough [on hold]

I have a couple of projects: GameAI ConnectFour GameAI implements a couple of algorithms: Minimax, Alpha-beta pruning and Alpha-beta pruning with state ordering. ...
2
votes
0answers
24 views

Collect maximum fruits in two baskets of at most 2 types of fruits

The task is taken from LeetCode In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly ...
4
votes
2answers
61 views

Find 4 points which form a square with the biggest area

I have set of points on the coordinate plane. I need to find the 4 points which form a square with the biggest area. I'm new in JavaScript so I'd like to get any suggestions about code style, ...
4
votes
1answer
52 views

Iterable hashtable implementation in C

I'm just finished my version of iterable hashtable. I want to review code in general (code style, data structures, remarks about the algorithms) and I have some questions: I don't know how to ...
3
votes
0answers
32 views

Deduping files using hardlinks

I'm trying to organize my gallery, and I have certain folders rife with copies. My daily work is mostly javascript and php, being your generic web developer. However I took this as an opportunity to ...
-2
votes
0answers
13 views

Bin Packing Js implementation using box rotation for best fit [on hold]

I have used the bin packing js implementation here https://github.com/jakesgordon/bin-packing When I specify the frame size as 800x600 and Blocks size as 150x700,150x700 it would say that, it cant ...
3
votes
2answers
111 views

Optimal burger-eating challenge

Little history to give context to the problem: The Krusty-Burgers Homer Simpson is a smart guy who likes to eat Krusty-Burgers, and it takes m minutes for Homer ...
-2
votes
0answers
38 views

Looping through this list takes time [on hold]

Below is the function which takes in list containing 3million elements and then creates another dictionary out of it for further lookups. This is an extremely time consuming function which after ...
6
votes
4answers
3k views

“What is the maximum that Player 1 can win?”

This is a question that I encountered in one of the competitive coding tests. The question goes as follows: A 2-player game is being played. There is a single pile of stones. Every stone has an ...
11
votes
2answers
1k views

Implementation of the Jacobi Symbol in C

I am new to C programming and wrote the following algorithm for the calculation of the Jacobi Symbol. Though there are some faster versions of this algorithm available I am only looking for some ...
4
votes
1answer
46 views

Convert to and from variable length quantities

As part of small MIDI library i've implemented the conversion of a VLQ (variable length quantity) for byte[] -> bigint and ...
6
votes
1answer
45 views

Updating generated file

With this algorithm I update an automatically generated verilog-file. The update within this file is done by commenting the assigned wires to specific ports of module instances. Is there a better, ...
3
votes
0answers
33 views

Strongly Connected component algorithm implementation(Python)

My goal is to implement Strongly Connected Components algorithm using python. I have splitted up my code on 3 parts: Data Load: ...
5
votes
1answer
77 views

Shortest pathway across a snakes and ladders board (Update)

Earlier I posted a fairly inefficient recursive solution to the problem of getting across a snakes and ladders board in the smallest number of moves. I have created a much faster solution to this ...
5
votes
2answers
902 views

Fastest path on a snakes and ladders board

I am not looking for a detailed review of this code as I am aware it is very inefficient. The main reason for posting this is to see if there is a better way to achieve my goal (I am certain there ...
5
votes
1answer
82 views

Find the longest word in a string

I have written code in the language Go. I wonder if anyone can do it in a very simple and effective way, because every language has its own way to solve this problem. My experience is in JavaScript. ...
7
votes
2answers
881 views

Arranging numbers in a circle such that the sums of neighbors and sums of diametric opposites are prime

I need to create a circle with numbers from 1 to n , however it is necessary to respect some rules: All numbers from 1 to n are used in the circle only once. The sum of two consecutive numbers ...
-4
votes
0answers
15 views

Huffman coding in CPP [closed]

I was studying huffman coding ,I faced a problem while dealing with priority queue Here is the code I tried to make some changes that i have learned so far in cpp in above cpp program i ...
6
votes
4answers
931 views

Find all letter Combinations of a Phone Number

The task is taken from LeetCode Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just ...
4
votes
1answer
49 views

Generate all possible combination of n-pair parentheses

The task is taken from LeetCode Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: ...
4
votes
2answers
84 views

Find the in-order successor of a given node in a binary search tree

I have an implementation of an algorithm to return the in-order successor of a given node in a binary search tree. We are allowed to assume that each node in the BST has a reference to its parent node....
0
votes
0answers
36 views

Given time intervals determine if a person could attend all meetings

The task is taken from LeetCode Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
-3
votes
1answer
72 views

Binary Search in C - Pointers and Memory [closed]

I got a warning when I am returning "list" in the function createList "Address of stack memory associated with local variable 'list' returned" I am confused about pointers and memory allocation and ...
0
votes
0answers
46 views

Given time intervals, find the minimum number of conference rooms required

The task is taken from LeetCode Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required....
0
votes
0answers
44 views

Merge Two Sorted Lists in JavaScript

The task is taken from LeetCode Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. ...
6
votes
1answer
85 views

Choosing evenly distributed points from a million points on a sphere

I have \$10^5\$ to \$10^6\$ points on a sphere, and want to choose some points from them which are as close as uniformly distributed as possible. For that reason, I do the following: at each step I ...
1
vote
0answers
34 views

Merge k Sorted Lists

The task is taken from LeetCode Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: ...
4
votes
0answers
66 views

Merge Intervals in JavaScript

This task is taken from Leetcode - Given a collection of intervals, merge all overlapping intervals. Example 1: ...
2
votes
0answers
41 views

Faster k-shortest path algorithm in Java

In the \$k\$-shortest path problem, we are searching for (at most) \$k\$ distinct, shortest paths connecting two given terminal nodes. You can see the previous version here. This implementation, ...
23
votes
7answers
5k views

Trapping Rain Water

This task is taken from Leetcode - Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. ...
2
votes
3answers
59 views

Jewels and Stones

The task is taken from leetcode You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you ...
4
votes
2answers
90 views

JavaScript implementation of insertion sort

I just started reading Introduction to Algorithms and the first algorithm introduced is the insertion sort. Even though the sort seems to work, I thought I'd ask for feedback right at the start since ...
3
votes
1answer
73 views

Non brute force algorithm for Birthday Chocolate

Problem: https://www.hackerrank.com/challenges/the-birthday-bar/problem Lily has a chocolate bar that she wants to share it with Ron for his birthday. Each of the squares has an integer on it. She ...
3
votes
0answers
30 views

Find the starting indices of all occurrences of the pattern in the string - KMP algorithm follow-up

The task was initially solved here, but was too buggy: Given a string and a pattern, find the starting indices of all occurrences of the pattern in the string. For example, given the string "...
1
vote
1answer
41 views

Number of Islands

The task is taken from leetcode Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands ...
1
vote
1answer
55 views

Sequence for Pre-order traversal of Binary Trees

I wanted to make a Sequence that can do a pre-order traversal of Binary Tree. Doing so provides automatically unlocks wonderfully useful methods, like ...
1
vote
3answers
73 views

Find the starting indices of all occurrences of the pattern in the string [closed]

The task Given a string and a pattern, find the starting indices of all occurrences of the pattern in the string. For example, given the string "abracadabra" and the pattern "abr", you should ...
1
vote
0answers
29 views

Tic Tac Toe finish the game function [closed]

currently i am working on javascript. Learning deep etc. So i decided to made my own tic tac toe game 4x4 without any tips. Then i stucked. If anyone can help me will be perfect. Game is playable ...
7
votes
1answer
67 views

Google “Wiggle walk”

I am doing Kickstart's problem Wiggle Walk. I have tried to code with matrix, dictionary and nested dictionary but the time exceeds the limit in each submition. Can anyone help me optimize it better ...
1
vote
1answer
28 views

Transposing a square matrix (n x n)

I wish to transpose a square matrix, permanently overwriting it. This is not the same as creating a 2nd matrix with the transposed contents of the 1st matrix. I call the procedure with 3 parameters: ...
4
votes
0answers
65 views

Rabin-Karp algorithm implementation

This code uses the Rabin-Karp algorithm to compare two text files. It takes a few minutes when both files contain around 1000 words. How can I improve? ...
4
votes
1answer
81 views

Decompose any number to its prime factors raised to their respective powers

A simple code which takes the given number as input and gives all of its prime factors raised to their respective powers...works well upto 106 after that it slows down greatly for some numbers..but ...
3
votes
2answers
164 views

Palindromic partitions solution

Task Find the smallest number of cuts required to break a string into a set of valid palindromes. For example: ababbbabbababa becomes a|babbbab|b|ababa (cuts = 3) partytrapb becomes partytrap|b (...
10
votes
2answers
158 views

Verhoeff check digit algorithm

A recent question on credit card validation here on Code Review, led me down a dark rabbit hole of check digit algorithms. I took a stop at the Verhoeff algorithm and tried to implement it myself. ...
2
votes
1answer
50 views

Length of the longest consecutive run of 1s in its binary representation

The task Given an integer n, return the length of the longest consecutive run of 1s in its binary representation. For example, given 156, you should return 3 My function solution 1 Style 1 ...
0
votes
2answers
62 views

Split an array into m subarrays such that the largest subarray sum is minimized

This is a Leetcode problem - Given an array which consists of non-negative integers and an integer m, you can split the array into ...
6
votes
1answer
81 views

Improved first Tic-Tac-Toe + Minimax AI

Posted my first attempt at Tic-Tac-Toe a second ago here. Got a lot of great and helpful responses. The main things I took away from these responses was to use spell check, and to keep things simple. ...
3
votes
4answers
97 views

Finding the Longest Word in a String

Challenge: Return the length of the longest word in the provided sentence. I've made a solution that works but it is kinda bad since the time complexity is \$O(n \log n)\$ and it involves mutation....