121,509 questions
-1
votes
1
answer
43
views
How can I optimize the time complexity of my brute-force solution for finding all unique triplets with sum zero?
I'm solving the "3Sum" problem where I need to find all unique triplets in an array that sum to zero.
Here’s my brute-force Java code:
public List<List<Integer>> threeSum(int[] ...
-3
votes
2
answers
89
views
LeetCode - 23. Merge k Sorted Lists algo issue [closed]
I was trying to solve this LeetCode Problem, and have been failing at it.
The problem
You are given an array of k linked-lists lists, each linked-list is
sorted in ascending order.
Merge all the ...
-3
votes
0
answers
59
views
matching pair with constraint [closed]
I have the following question that I have been asked in my interview. I gave a simple solution (see below) but apparently that was not correct. I have been told that there is a greedy solution for ...
1
vote
2
answers
129
views
How can I optimize a Python loop that counts maximum frequency of elements?
I'm working on a Python function to find the most frequent element in a list and handle ties in a specific way.
a = [1, 2, 2, 3, 3, 3, 4]
s = 0
count = 1
for i in set(a):
if a.count(i) == s:
...
-2
votes
0
answers
33
views
Puzzle swap system alghorithm [closed]
I make a swap system puzzles. So, I have a gid (n x n). So, lets take 3 x 3. And I need to swap the group of tiles. It's easy if the figures are the same height and width.
But there are cases when ...
-1
votes
0
answers
43
views
Median Position in a Min-Heap can be found at any level - need proof [closed]
I need to prove that the median value in a minimum heap with distinct values can be found at any level.
It sounds right intuitively and I can give examples, but not sure how to approach this proof, ...
3
votes
3
answers
140
views
Count how many adjacent elements satisfy a predicate
Given a std::vector named v and a predicate p that takes two elements and returns a bool, what is the idiomatic way to count how many adjacent elements in v satisfy p?
The obvious way is to use a loop:...
0
votes
2
answers
205
views
What is the time complexity of the following algorithm:
int k = 0;
for (int a = n; a >= 1; a /= 2)
for (int b = a; b >= 1; b--)
k++;
cout << k;
I would say that the answer is O(n), but the book where I have found the exercise says that ...
3
votes
2
answers
106
views
C How to generate an arbitrary permutation within range [i,j]?
Does C standard library provide any function that generate a random permutation of consecutive numbers within a range?
How to make a function that efficiently does it? I'm thinking of making a random ...
0
votes
1
answer
102
views
Is there a faster method to compute these distance-based sums over a nonuniform grid of cells?
I'm attempting to implement an algorithm to compute an array of values. Unfortunately, I've found the literal implementation of that algorithm to be too slow for my use case. I suspect there's an ...
0
votes
0
answers
130
views
How to implement a generalized N-Sum algorithm for variable k (user input) in Java?
I’ve already solved 2-Sum and 3-Sum problems:
2 sum uses a HashMap to check if target - nums[i] exists in HashMap.
3 Sum sorts array and then uses 2 pointers.
Yesterday, during Flipkart GRID Coding ...
-1
votes
0
answers
39
views
Shapez2's Shape Crafting Optimization Problem [closed]
This is an algorithmic challenge from the factory game Shapez2, essential for building what's known as a TMAM (True Make Anything Machine).
Shapez2's Shape Crafting Optimization Problem
Problem ...
2
votes
1
answer
79
views
how to decide forward or backward computation in dynamic programming tabulation
I do not know when to use backward or forward computation for tabulation in solving a dynamic programming problem.
I would like to know the thinking process to decide which one to use or which one ...
8
votes
3
answers
189
views
Ordering points that roughly follow the contour of a line to form a polygon
I have a list of points (x,y). A line is drawn somewhere 'inside' these points (within the area they form). I have been trying different algorithms for ordering these points to create a polygon, with ...
1
vote
1
answer
89
views
TicTacToe win checking logic [duplicate]
I made a method to check for a win in a simple TicTacToe I made, it is very long and has a bunch of if statements, I want to know if I can make it in loops efficiently. I searched but only found ...