All Questions
226
questions
3
votes
1
answer
219
views
Leetcode: Number of Islands - BFS (Queue vs Recursion)
I was playing around with leetcode's Number of Islands.
As per the challenge's description:
Given an m x n 2D binary grid grid which represents a map of '1's
(land) and '0's (water), return the ...
5
votes
1
answer
337
views
Leetcode: Largest Number
I was trying out leetcode's Largest Number.
As per the challenge's description:
Given a list of non-negative integers nums, arrange them such that
they form the largest number and return it. Since ...
4
votes
2
answers
496
views
Leetcode: Steps to Make Array Non-decreasing
I was trying out leetcode's Steps to Make Array Non-decreasing
As per the challenge's description:
You are given a 0-indexed integer array nums. In one step, remove all
elements nums[i] where nums[i ...
9
votes
4
answers
2k
views
Leetcode : First Missing Positive
I was trying out leetcode's first missing positive.
As per the challenge's description:
Given an unsorted integer array nums, return the smallest missing
positive integer.
You must implement an ...
1
vote
0
answers
74
views
A parallel MSD radix sort in Java for integer keys
I have this repository: https://github.com/coderodde/ParallelRadixSort.java/tree/main
It contains a parallel MSD radix sort presented below:
...
4
votes
2
answers
663
views
Merge a list of deeply nested HashMaps
Suppose I have a list of hashmaps (<String, Object>). Values can be either scalar types or a nested HashMap with the same type.
I need to merge a list of this ...
1
vote
0
answers
129
views
Making Robert Tarjan's offline LCA algorithm run (much) faster (Java)
I have produced this GitHub repository that compares the performance of:
Robert Tarjan's off-line lowest common ancestors algorithm,
An improvement of the above algorithm.
Typical demo program ...
2
votes
1
answer
737
views
Traverse a list of tree nodes
I am wondering if there is a better way to traverse a list of trees.
Here is my current algorithm:
Collect the parent IDs in a Map
Traverse all the groups and find ones where their ID is not in the ...
5
votes
4
answers
553
views
Fastest function to find cumulative largest perfect squares of a single number?
I'm attempting to write an algorithm that will find the largest perfect squares of a given integer, subtracting their values from the total each time, as fast as possible. It's somewhat hard to ...
0
votes
3
answers
1k
views
Calculate square of (sum of even-indexed) - (sum of odd-indexed) subarrays from an array
Find all subarrays from a given array in the least possible time complexity.
Requirement:
calculate the square of (sum of even-indexed elements) - (the sum of odd-indexed elements) of al the subarrays....
1
vote
2
answers
626
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 ...
1
vote
1
answer
90
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 ...
2
votes
1
answer
62
views
Distribute items based on the time duration
I just finished a small assignment and I would like to get some feedback about the implementation here. Basically, it was all about distributing items ("talks" in this case) throughout the ...
3
votes
1
answer
268
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) ...
2
votes
0
answers
801
views
LeetCode 218: The Skyline Problem II
Here I'm posting my Java code for the skyline problem. If you have time and would like to review, please do so, I'd appreciate that.
Problem
A city's skyline is the outer contour of the silhouette ...
3
votes
1
answer
468
views
LeetCode 767: Reorganize String
Here I'm posting my code for the Reorganize String problem on LeetCode. If you have time and would like to review, please do so, I'd appreciate that.
On LeetCode, we are only allowed to change the ...
0
votes
1
answer
174
views
LeetCode 76: Minimum Window Substring - Java
I'm posting my Java code for the Minimum Window Substring. If you have time and would like to review, please do so, I appreciate that.
Problem
Given a string string...
5
votes
2
answers
480
views
How to optimize Karatsuba algorithm (using arraylist and java)
I was using Karatsuba algorithm to multiply two polynomials and return the coefficients and I am using java, we are asked to use arraylists here, however, my code is too complicated and it takes much ...
1
vote
2
answers
477
views
How to efficiently find if numbers in a given range contains either 1 or 3, but not both?
I am having a trouble speeding up my implementation. The question is, given a range of numbers from long a to long b, find how many lucky numbers are between a and b inclusive. A number is a lucky ...
5
votes
2
answers
104
views
Performance Enhancement of A Star Path Finder algorithm with 1024 multidimentional array
I have the below code for a A* pathfinder, however it is taking upwards of 10 minutes to find a solution using a simple 1024 x 1024 array.
I had to comment out ...
0
votes
2
answers
612
views
Java Palindrome - Time & Space Complexity
Which of these three methods is the most efficient?
Also, what is the time & space complexity of each?
...
4
votes
2
answers
86
views
Filtering product ids
As regarding the following question I have completed the code as below.
But I am curious about other effective solutions. I am also curious about if there is any solution that could be applicable by ...
1
vote
1
answer
181
views
"Edit Distance" algorithm
I have a piece of code that calculates the edit distance between words and works, but it's apparently not fast enough.
ClosestWords.java:
...
4
votes
5
answers
1k
views
Counting number of `1` digits in the value of \$11^n\$
I am trying to improve the efficiency of my code,
The Question is:
Given a value n, where n is in the range ...
2
votes
2
answers
195
views
Brute-force ID reversal search
EDIT
The random number generator in this code is a blackbox for a similar algorithm that isn't publishable for privacy concern. The algorithm works in similar way, in that it takes in a seed value ...
3
votes
1
answer
252
views
Knuth-Morris-Pratt algorithm in Java Finding substrings in a matrix
Problem
Given two matrices: a pattern p and a text t. Write a program that
counts the number of occurrences of ...
6
votes
1
answer
430
views
Remove elements compared with other elements in a Set based on a condition
Simplified the actual problem to focus on comparing/removing performance.
Given a set S with n elements , find the most optimal way to compare each element to all others until a condition is met that ...
1
vote
1
answer
448
views
Adding a number to an array of digits
I'm a student in my first semester and in my free time and holidays, I do some java training to keep my mind from rusting...so I downloaded this amazing Digital Algorithms practice app
and came across ...
1
vote
0
answers
256
views
Algorithm X implementation for Minimum Dominating Set
As as final project for a MOOC I wanted to write a program that finds the exact cover set/minimum dominating set from the given social network data. So I did end up with writing this. I kinda followed ...
7
votes
2
answers
2k
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 ...
3
votes
1
answer
2k
views
N-puzzle solver using A* search
I have written a Java program to solve a N-puzzle with A* search and manhattan distance. My program can solve every 8-puzzle I have tested under 1 second, but it can't solve a 15-puzzle after 30 ...
5
votes
2
answers
529
views
Pascal's Triangle Java (performance)
I need to get some information about the Pascal Triangle.
What is a pascal triangle ?
It is a triangle of integers with 1 at the top and at the sides. Any number inside is equal to the sum of the ...
5
votes
1
answer
182
views
Goldbach conjecture solution finder
The Goldbach conjecture says: any even number greater than two can be represented by the sum of two prime numbers.
G(n) = { (p1, p2) | p1 + p2 = n, p1 and p2 are prime numbers with p1 < p2 }
...
4
votes
2
answers
2k
views
Trip planning algorithm
I was presented with an interview question described as follows:
Receiving an int[] A of cities, where each A[i] has an appeal ...
1
vote
1
answer
982
views
External polyphase merge sort
For comparison with other external sorting algorithms, I have implemented external polyphase merge sort, purported to be useful to sort large files that do not fit into main memory. More theoretical ...
0
votes
1
answer
72
views
Searching for a sum of any series in array
I'm given this method, and requested to analyze what it does, and minimize its complexity (both time and space).
All values are known to be larger than zero.
...
2
votes
1
answer
52
views
Find the highest result between two elements in an array using their values and indices
I got a programming question on an interview where performance was the focus. I couldnt come up with a better solution than the one below which I think has a time complexity of O(n^2 + n) and scored ...
3
votes
1
answer
639
views
Speed up Magic square program
I have written a Java program to calculate magic squares with recursion and backtracking. A 3*3 Magic square is calculated in about 1 sec, but a 4*4 needs about 50 minutes on my laptop with Intel i5. ...
3
votes
1
answer
241
views
Map Character to Characters Most Frequently Found With it (in list of strings)
For an interview, I was tasked with writing a program that consumes a list of strings, and produces a mapping between every character in the list, and the characters found most frequently with it (let'...
2
votes
1
answer
537
views
Producer consumer design and implementation using Java 8
Please review my design and code implementation and suggest if any optimisation is possible in terms of performance (time complexity / space complexity ) or any better way of design or implementation. ...
2
votes
0
answers
213
views
Longest Palindromic Substring challenge
My code is a mix-up between dynamic programming with the HashMap and brute force with the way I reach out the results and substring. I don't know how to optimize my ...
-1
votes
1
answer
44
views
Extract a box with pixel values from an image, calculate the mean of each possible box [closed]
I have a two-dimensional array with M rows and N columns, with each element having a value between 0 and 255.
I have a second two-dimensional array with m rows and n columns, m < M, n < N.
...
4
votes
1
answer
138
views
INCARDS SPOJ challenge
Question-
In short question says that you have N bus stops and K bus routes.
Every bus routes is linked to two bus stops. Each route has some cost
of travelling. It says one person visit any ...
5
votes
2
answers
666
views
Converting Reverse Polish to Infix Notation in Java
I am trying to solve a programming challange that involves converting reverse polish notation to infix notation. For example: 1 3 + 2 4 5 - + / would be: ((1+3)/(2+(4-5))) My solution so far does work,...
3
votes
2
answers
278
views
Binary Tree max sum conditionally
Problem : A binary tree is given as an input, each node of binary tree contains one integer value. Find the maximum sum of collection of nodes such that following two conditions are met.
if node's ...
2
votes
0
answers
126
views
Tic Tac Toe game becomes more inefficient as time goes on
I designed a tic tac toe using 4 classes. The game class being the main class, the board class being the board, the player class being the decision making, and the AI class keeping the previous game ...
2
votes
1
answer
193
views
Let us schedule a meeting
Description:
Write a method mergeMeetings() that takes a list of multiple meeting time ranges and returns a list of condensed ranges. The integers represent the number of 30-minute blocks past 9:00am....
1
vote
3
answers
2k
views
Merge sorted lists, removing duplicates
I want to merge two sorted lists of Integers but this is not the general mergesort case because the same number may appear in both lists. (However, each number can only appear once in a particular ...
2
votes
1
answer
336
views
Backtracking maze traversal
I have a functional backtracking solution, but I am only able to finish 22/25 test cases before exceeding my specified time limit. Any suggestions on how to improve the speed are greatly appreciated!
...
1
vote
1
answer
203
views
Custom sorting algo / optimized bubble sort
I'm a novice and started diving into data structures and algorithms recently. I was taught bubble sort recently and I found that the original bubble sort algorithm is so inefficient, so I wrote a ...