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
21 views

LeetCode 37: Sudoku Solver

I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time! Problem Write a program to solve a Sudoku puzzle by filling the empty cells....
3
votes
0answers
26 views

LeetCode 924: Minimize Malware Spread

I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time! Problem In a network of nodes, each node i is directly connected to another ...
0
votes
0answers
26 views

Kotlin Stack using an ArrayList to compare and remove elements

What is the most performant strategy to implement a stack in Kotlin to compare and delete values? Expect Create a stack in order to compare an array of asteroids (ASTs) and handle collisions (...
1
vote
0answers
13 views

Find Invalid User Relationships in User to Manager Map (validate map as valid tree structure with no circular relationships)

We have a new service where an admin can upload a spreadsheet of new users they want to add to our application. During our processing we make a union map of all the users both existing and new ...
-2
votes
0answers
52 views

How to get out of if/else hell and avoid casting from nullable to non-nullable types? [closed]

I have a method called GetActionCode() which sets a new action code (A, C, R or D) for a user based on: userState which has a <...
2
votes
2answers
75 views

LeetCode 273: Integer to English Words

I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time! Problem Convert a non-negative integer to its English words representation. ...
1
vote
1answer
45 views

LeetCode 639: Decode Ways II

I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time! Problem A message containing letters from A-Z is being encoded to numbers ...
1
vote
1answer
19 views

Write your implementation of Ruby's String#count method

The task is to write a simplified version of Ruby's String#count method oneself. Simplified because it doesn't have to have the negation and sequence parts of the original. Here my solution: ...
3
votes
2answers
335 views

How to optimize bitwise get/set/clear of ranges of bits in JavaScript?

I have been working for a few days on writing get, set, and clear bitwise functions in ...
-3
votes
0answers
49 views

Worst-Case Scenario [closed]

Estimate the worst-case complexity for the auto-completion of a query if you use the following data structure: Your Trie. A linked list in which each node contains a training query with number. In ...
1
vote
2answers
69 views

How to make Print() method memory & CPU efficient?

Problem Statement You are receiving n objects in a random order, and you need to print them to stdout correctly ordered by sequence number. The sequence numbers start from 0 (zero) and you have to ...
2
votes
2answers
50 views

LeetCode 862: Shortest Subarray with Sum at Least K

I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time! Problem Return the length of the shortest, non-empty, contiguous subarray ...
3
votes
2answers
86 views

HybridSort of QuickSort and MergeSort

I'll try to publish a paper about the programming language I've made (ArithmeticExpressionCompiler, short AEC) in Osječki Matematički List and, in order to demonstrate its usability for implementing ...
3
votes
1answer
44 views

LeetCode 894: All Possible Full Binary Trees

I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time! Problem A full binary tree is a binary tree where each node has exactly 0 ...
8
votes
2answers
598 views

Optimize function that returns length of a subarray

I did an exercise today on where the task is to write a function that returns the length of the longest subarray where the difference between the smallest and biggest value in the subarray is no more ...
1
vote
1answer
34 views

LeetCode 410: Split Array Largest Sum

I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time! Problem Given an array which consists of non-negative integers and an ...
2
votes
1answer
29 views

SelectionSort Algorithm using Python

I am a rookie and I need help optimizing this SelectionSort() algorithm which I have tried using Python. ...
5
votes
0answers
59 views

K nearest neighbours algorithm

Here is a project that I worked on for a few days in June 2020. Since the algorithm is extremely slow, I looked into methods in order to parallelize operations but did not obtain any satisfactory ...
0
votes
0answers
35 views

LeetCode 212: Word Search II

I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time! Problem Given a 2D board and a list of words from the dictionary, find all ...
0
votes
1answer
31 views

LeetCode 652: Find Duplicate Subtrees

I'm posting my code for a LeetCode problem copied here. If you have time and would like to review, please do so. Thank you! Problem Given a binary tree, return all duplicate subtrees. For each kind ...
10
votes
1answer
165 views

Create a build order from a list of projects and their respective prerequisites

Another graph algorithm, this time to create a priority or build order. Provided with a starting List<Project> and ...
3
votes
1answer
30 views

Bubble Sort in Forth (for strings)

I wrote the following Forth code for sorting a string with the Bubble Sort method. It looks nice to my eyes, but I'd like your experienced opinion and any comments about the code you might have. In ...
4
votes
2answers
95 views

LeetCode 1032: Stream of Characters

I'm posting my code for a LeetCode problem copied here. If you have time and would like to review, please do so. Thank you! Problem Implement the StreamChecker ...
2
votes
3answers
79 views

Project Euler 50: Consecutive prime sum

I just finished solving Project Euler's 50th problem, but it's awfully slow. I'd like to hear your thoughts on my code's efficiency and practices. Problem Statement The prime 41, can be written as the ...
2
votes
1answer
70 views

Super greedy algorithm for Exact Three Cover

I had trouble solving instances of Exact Three Cover with 100 units in input C. All in a reasonable amount of time. Mainly because the problem is NP-complete. So I came up with an approximate solution ...
3
votes
3answers
107 views

Algorithm to determine if binary tree is a Binary Search Tree (BST)

Continuing with algorithms I've implemented a binary search tree validator. I don't like the two boolean variables within NodeFollowsBSTContract as it feels too ...
2
votes
1answer
53 views

LeetCode 753: Cracking the Safe

I'm posting my code for a LeetCode problem copied here. If you have time and would like to review, please do so. Thank you! Problem There is a box protected by a password. The password is a sequence ...
1
vote
0answers
26 views

A word generator meant to be piped to hashcat/jrt which would then use amplificators (rules)

This piece of code does a linear generation with two 'tweaks': It sets a global loop as an index to reference variables that are holding values that need to be ...
1
vote
2answers
80 views

LeetCode 1293: Shortest Path in a Grid with Obstacles Elimination

I'm posting my code for a LeetCode problem copied here. If you have time and would like to review, please do so. Problem Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In ...
4
votes
1answer
61 views

A mathematical expression parser with custom data structures

I recently wrote a mathematical expression parser in C++. The software can read valid mathematical expressions and evaluate them. An example of an expression the code can parse is ...
3
votes
1answer
66 views

Algorithm to find path between nodes

First time working with any type of graph traversal. The algorithm determines whether a path exists between two nodes using breadth first searching (BFS). The BFS method returns a tuple; indicating ...
2
votes
2answers
76 views

Knapsack performance issue

I'm solving a knapsack problem here. It works, but gives time limit exceeds on a certain test case. Problem statement There are N items, numbered 1,2,…,N. For each i (1≤i≤N), Item i has a weight of ...
4
votes
2answers
85 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 ...
5
votes
3answers
253 views

Find all numbers that are factors of one array and factor into the second array, then print their count

I'm working on a solution to the Following problem on Hackerrank, and I seem to have found a function that works for the purpose. However, I feel as though this is a very overdesigned solution to a ...
1
vote
1answer
60 views

C - 2D peak finder in

This is an implementation of a 2D peak finder from MIT's Intro to Algos. I'm a C beginner and I would really like some feedback on coding style, documentation and any other general C bad practices. ...
1
vote
1answer
51 views

LeetCode 552: Student Attendance Record II

I'm posting my code for a LeetCode problem copied here. If you have time and would like to review, please do so. Thank you! Problem Given a positive integer n, return the number of all possible ...
1
vote
0answers
31 views

Sorting Algorithm Visualiser in Javascript

I'm new to Javascript and Web Dev in general, so they'll likely be some conventions that I'm not aware of. I'm at an odds of whether to use Javascript's functional features more. In my opinion it's ...
2
votes
0answers
65 views

Python's quicksort algorithm

I was playing around with sorting methods, brushing up on my algorithms. I have made the following implementation of quicksort: ...
-1
votes
1answer
59 views

LeetCode 1320: Minimum Distance to Type a Word Using Two Fingers II

I'm posting my Python code for LeetCode's 1320. If you have time and would like to review, please do so. Problem You have a keyboard layout as shown above in the XY plane, where each English ...
7
votes
1answer
86 views

Dijkstra algorithm C#

I have implementing a simple version of Dijkstra's algorithm in C#. I found it really confusing, and I'm not 100% if I've implemented it correctly. Could this be made more efficient? Does it need to ...
2
votes
1answer
66 views

LeetCode 146: LRU Cache III

I'm posting my Java code for LeetCode's LRU Cache. If you have time and would like to review, please do so. Thank you! Problem Design and implement a data structure for Least Recently Used (LRU) ...
1
vote
0answers
46 views

LeetCode 146: LRU Cache ||

I'm posting my Python 3 code for LeetCode's LRU Cache. If you have time and would like to review, please do so. Thank you! Problem Design and implement a data structure for Least Recently Used (LRU) ...
5
votes
1answer
82 views

LeetCode 146: LRU Cache I

I'm posting my C++ code for LeetCode's LRU Cache. If you have time and would like to review, please do so. Thank you! Problem Design and implement a data structure for Least Recently Used (LRU) cache....
1
vote
1answer
32 views

Caesar Cipher, done in Ruby

It's an assignment from the Odin Project. Here's my implementation: ...
1
vote
2answers
115 views

LeetCode 1044: Longest Duplicate Substring

I'm posting my C++ code for LeetCode's Longest Duplicate Substring. If you have time and would like to review, please do so. Thank you! Problem Given a string S, consider all duplicated substrings: (...
6
votes
1answer
105 views

Summing digits of a singly linked list

I want to familiarize myself with more data structures. As such I implemented a Node, singly-linked-list, as well as an algorithm to sum the digits of two such lists where each node contains the ...
5
votes
1answer
68 views

How to merge two descending singly linkedlist into one ascending linkedlist using Recursion without any helper method?

Here is my solution: I want to know if my solution fits the requirement 100%? Or if there is any better solution? Constraint of the question: must be singly linkedlist must use recursion, and no ...
0
votes
1answer
45 views

LeetCode 1146: Snapshot Array

I'm posting my C++ code for LeetCode's Snapshot Array. If you have time and would like to review, please do so. Thank you! Problem Implement a SnapshotArray that supports the following interface: ...
3
votes
1answer
87 views

Hackerrank problem: Climbing the Leaderboard (Java)

I am solving the following Hackerrank problem: Climbing the Leaderboard. The problem statement: Alice is playing an arcade game and wants to climb to the top of the leaderboard and wants to track her ...
1
vote
1answer
43 views

Recursive Matrix Multiplication Algorithm

Can I improve on this any further. Is there a prettier way of passing the array sizes? ...

1
2 3 4 5
88