# Tag Info

## Hot answers tagged algorithm

6

If the grader is complaining about "time limit exceeded," it must be because some loop is executing too many times. All of your functions are clearly O(1) — they have no loops. So which loop is executing too many times? It must be the only loop in the entire program: while(!isFull(n)){ cin>>a; push(a); } Is it possible that ...

4

First -- document your code! I read through your d_func function and here's my attempt at writing a docstring for what it does, with Python-2-compatible type hints. Hopefully I got it right. :) def d_func(digit, mode): # type: (Union[str, int], int) -> Union[int, str, None] """Mode 1: give the int value of the hex digit. Other modes: give ...

4

The code Before switching to a better algorithm, let's polish the code first. Almost every line of your code can be improved. Get rid of #define ll long long int. This is a standard way to lower the quality of your code. If you mean long long, use long long. Qualify names from the std namespace with std::. Make the parameter temp a const reference (const ...

4

I have no C/C++ compiler at the moment, but the algorithm only needs one loop, the while+flag being a tiny bit too unreadable. There are two counters: Finding the maximum length: maxLength Sequences with next value >= prior value. Counting the maximum length: maxCount. length > maxLength reset maxCount to 1 length == maxLength increment maxCount So (in ...

3

My main issue with this code is the function interface as you already identified yourself. These are the most glaring issues in my opinion: Parameter names are not clear. Names like i and n might be good enough in very short, self-contained loops/closures, but not as function parameters. I similarly don't like abbreviations like orig, and cur because the ...

3

Don't use #define ll long long int and don't use long long int either. Use auto for values and size_t for indices and cardinalities. Your method should take iterators as parameters. It will still work with vectors, but also with linked lists and other containers. Have a well defined behaviour for corner cases, such as empty lists. This behaviour should be ...

3

In Go, measure performance. Run benchmarks using the Go testing package. For example, $go test transpose_test.go -bench=. -benchmem BenchmarkTranspose-4 2471407 473 ns/op 320 B/op 13 allocs/op BenchmarkTransposeOpt-4 9023720 136 ns/op 224 B/op 2 allocs/op$ As you can see, minimizing allocations is important. Efficient memory ...

2

In addition to Sam I want to point out some other things Avoid typing long list/dict constants Very often you can construct them by code, which is less error prone. Instead of d_dict = {"0" : 0, "1" : 1, "2" : 2, "3" : 3, "4" : 4, "5" : 5, "6" : 6, "7" : 7, "8" : 8, "9" : 9, "A" : 10, "B" : 11, "C" : 12, "D" : 13, "E" : 14, "F": 15} you do import string ...

1

Unnecessary else: if length <= 1: return arr else: pivot = arr.pop() ... If the first path is taken, the method exits. If the second path is taken, then execution continues with the remainder of the method. So you could write the rest of the method inside the else: clause: if length <= 1: return arr ...

1

Care about final Your parameters are never modified, so it is good practice to mark them final. Probably the method too, unless you expect it can be overridden. public final int shortestDistance(final String[] words, final String word1, final String word2) Compare by indices Instead of your for loop, you can use the indices of the elements. public final ...

1

For the algorithm, it can be shortened by only checking if a sub-sequence qualifies when it is finished. using ll = long long; int LongestSubSeq(const std::vector<ll>& arr) { int numSeq = 0; int longest = 0; int length = 0; size_t size = arr.size(); for(size_t i = 1; i < size; ++i) { ++length; if(arr[i] &...

1

Δ​t Since you have the timestamps of the velocity measurements, there is no need to calculate the step size. For all you know, the steps are not even all equal. You can use np.diff to calculate the differences between all the data points: time_differences = (np.diff(time)) or in pure python: time_differences = [b - a for a, b in zip(time, time[1:])] or ...

1

Just a few things I noticed. Utilize built in functions This S = 0 for k in range(1, N): S += function[a + k * h] can be this S = sum(function[a + k * h] for k in range(1, N)) Python3's sum takes an iterable, and returns the sum of all the values in that iterable. So, you can pass in the for loop and it will return the sum for you. Looks neater, ...

Only top voted, non community-wiki answers of a minimum length are eligible