New answers tagged

1 vote

Selection algorithms (order statistic) in Java: linear time vs. sorting selector vs. Quickselect

(I'm no fan of final for interfaces and classes, this seems to carry to sealed.) one alternative to specifying ...
  • 4,958
1 vote

Unidirectional linked list in C

make sure there are no possible memory leaks Advice on testing: Have allocation & disposal functions increasing & decreasing an instance count as ...
  • 4,958
4 votes

Unidirectional linked list in C

A serious error everybody appears to fix without comment: push_back() allocates memory and forgets to initialize some of it. Specifically, it fails to write: ...
  • 1,178
3 votes

Unidirectional linked list in C

Some suggestions follow. 1/ Comment your code. It won't affect at all how the code does its job (unless you do something silly like comment out a vital line of code), but it will affect the ability to ...
  • 141
1 vote

How to using custom colormap with pixel calculation, gray scale value by opencv and make it workable on camera

Looking at just the code, I find it difficult to see its purpose. In particular, I don't find colormap: Document your code. In the code. Define a function for the mapping presented. Find or define a ...
4 votes

Unidirectional linked list in C

Interfaces Consider the work that your contains function does compared to the information it returns. It finds the first position that has the value in question. ...
5 votes

Unidirectional linked list in C

You might consider to abbreviate struct node with a typedef name: ...
  • 3,246
13 votes

Unidirectional linked list in C

Bug Test code better. ...
11 votes

Unidirectional linked list in C

Think about the failure cases. For example: struct node *list = malloc(sizeof(struct node)); list->data = data; list->next = NULL; return list; If ...
  • 60.7k
1 vote

Re-order table columns in HTML dynamically with Javascript

Start with the data in Javascript. Always sort the Javascript, never from the html table. Always create a new table. Throwing away the pre-existing table fantastically simplifies everything. I ...
  • 6,625
5 votes
Accepted

Sort array of associative arrays by the key of the subarrays

Short of simplifying your input array structure, using functional sorting will be most direct with array_multisort() which is fed an array of keys to sort in a ...
  • 7,916
3 votes

Traverse/transform (xml) tree

Consider using a SAX parser I am trying to write a utility for transforming xml trees using single pass [...] But you are using a DOM parser library. This means it will parse the whole XML document ...
  • 35.7k
3 votes

Sort array of associative arrays by the key of the subarrays

Bubble sort algorithm - inefficiently implemented After each iteration of the Bubble sort algorithm "one less element (the last one) is needed to be compared until there are no more elements left ...
2 votes

Sort array of associative arrays by the key of the subarrays

I solved it with usort like recommended by @greybeard. Credits to him! ...
  • 243
1 vote
Accepted

Increase efficiency of stick cutting algorithm

Two reviewers advised how to avoid establishing the length of current longest piece from scratch, as that promises worst case run time at least quadratic in the number m of cuts. In greybeard's ...
3 votes

Increase efficiency of stick cutting algorithm

Consider time complexity When the code works but it runs too slow, it's usually because the algorithm doesn't scale well to larger inputs. It's good to review the time complexity and look for ...
  • 110k
3 votes

Increase efficiency of stick cutting algorithm

Hm. Interview. You tried something simple (good!) and it doesn't fit the time frame (pity). Well, if it seems to work, give it a name and use it as a test for advanced implementations. Make it a ...
  • 4,958
1 vote

Counting the number of patterns that exist in a particular string

Another way to Simplify the loop logic (and explicitly avoid using oob indices): ...
  • 4,958
1 vote

Find factors of a Mersenne number

This code is: Slower than the original (why do you need two loops? why a vector? boolean operations are much faster) Futile (all factors with 64 bits or smaller, of mersenne numbers with exponents ...
  • 11
3 votes
Accepted

General algorithm to calculate sums of all subsets of a given sequence of numbers

This is pretty slick modern C++. I like it. The recursion depth (equal to the input length) is unlikely to become a problem before the exponential explosion does. Naming the all the template types ...
  • 60.7k
1 vote

Counting the number of patterns that exist in a particular string

Simplify the loop logic The nested loop in isSubstr is a bit difficult to understand: The outer loop variable i can be modified ...
  • 110k
0 votes

Refactoring a binary tree used as a tournament ladder

I'm having a hard time understanding the requirements. I understand what a tournament bracket is, but the teams only get filled in as games are played, so it's not clear what exactly we need to do ...
4 votes

Counting the number of patterns that exist in a particular string

Harold gave a good answer addressing the algorithm used. I'll look at other aspects of your code. You say you have a using namespace std; somewhere because you're ...
  • 60.7k
6 votes

Counting the number of patterns that exist in a particular string

The current time complexity I reckon is O(N*M) where N = elems of pattern, M = elems of word. I'd say O(NM) where N = the total size (sum of lengths) of patterns, M = length of word. There is a ...
  • 6,759
3 votes

Get all partitions of an array into k non-empty sub-arrays

Time and space complexity To calculate the time and space complexity, it's useful to consider the output: what must it contain. For every way to split into sub-arrays, the output will contain one ...
  • 110k
3 votes

C++ Binary search using SIMD

Use the same API as std::ranges::lower_bound You mention that you wanted to use std::ranges algorithms to do a binary search. If ...
  • 35.7k
2 votes

Improving time complexity for codechef Que

This code is quite terrible to read. Please think about what your goal is when you do these code challenges. Is it just to get some site to say "AC" instead of "TLE"? Or is it to ...
  • 35.7k
1 vote

Next level Improved Sieve of Eratosthenes

This is similar to a wheel sieve with basis {2,3,5}. You can simplify everything and write it compactly like this: ...
1 vote

Finding first unique character in string

We forgot to #include <string>. This definition looks like C: typedef struct { ⋮ } Tracking; In C++, we ...
  • 60.7k
2 votes
Accepted

Segmentation of list to minimize the largest sum

One could approach this from the goal: What is the business of a Segmentation instance, what are its business methods? You tagged ...
  • 4,958
4 votes
Accepted

Finding first unique character in string

As already mentioned, you can do this in a one-pass algorithm. You only need to keep track of whether you have seen a character before, and what the position of its first occurence is. Here is a ...
  • 35.7k
3 votes

Finding first unique character in string

Consider a simpler algorithm: Pass 1: Build a map of counts of characters. Pass 2: For each index, if the count of the current character is 1, return the index. If pass 2 terminates without returning,...
  • 110k
3 votes

ReversibleList: a list that is "reversed" in O(1) time

I have only one suggestion: remove the forwardAll() method. As I understand it, the point of ReversibleList is to allow for ...
  • 720
5 votes

ReversibleList: a list that is "reversed" in O(1) time

Additional Bugs Your ReversedList has numerous bugs. The reverse() method does not reverse the list when it is already in ...
6 votes
Accepted

ReversibleList: a list that is "reversed" in O(1) time

Consider composition over inheritance Doing inheritance right is very hard. Keep in mind that subclass coupling (when class A is a subclass of class B) is the strongest type of coupling between two ...
  • 110k
16 votes

ReversibleList: a list that is "reversed" in O(1) time

I don't really like the direction toggling stuff. Mostly on the grounds of disliking state if possible. You could end up with the list pointing in the opposite direction to the one intended. What I ...
  • 261
16 votes

ReversibleList: a list that is "reversed" in O(1) time

Bug You have a bug! a[2:5] = ReversibleList([1, 2, 3]) print(a) # [5, 4, 3, 2, 1, 0] ReversibleList([1, 2, 3]) is constructing ...
  • 32.2k
3 votes
Accepted

Queue using circular linked list

There is a bug in the dequeue function: // Retrieve the value before deleting the node int value = node->value; The ...

Top 50 recent answers are included