Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 105198

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.

3 votes

Finding the number of longest contiguous increasing subsequences in a given array

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 wor …
Rainer P.'s user avatar
  • 2,855
0 votes

Longest word in dictionary that is a subsequence of a given string

The code can be made quite a bit more readable by moving parts of the logic into a separate class. Also, sorting the candidates by length is not helpful, even harmful in certain situations. If the sho …
Rainer P.'s user avatar
  • 2,855
2 votes

Find all trains which overtake another train

The algorithm is overly complex. First, some general remarks. 1. If you use find() in a loop, you're doing it wrong. A search operation in a loop can be replaced by a grouping operation. … Kruskal's and Prim's algorithm both calculate the minimum spanning tree of a graph, but their complexity depends on edges (Kruskal) vs. vertices (Prim). …
Rainer P.'s user avatar
  • 2,855
3 votes
Accepted

Given a list of integers, write a function that returns the largest sum of non-adjacent numb...

What you implemented is the recursive solution: sum(k) | k < 0 = 0 | otherwise = max( sum(k-1), sum(k-2) + arr[k] ) You can also implement it iteratively using an O(n) extra space: sum = int[n …
Rainer P.'s user avatar
  • 2,855
7 votes

Get consecutive integer number ranges from list of int

Your code takes a list, ensures it's sorted and distinct, then assembles and returns another list. This approach is problematic when the input is in fact a generator, in which case your code forces th …
Rainer P.'s user avatar
  • 2,855
1 vote
Accepted

Calculating total maintenance costs of a car

This is a textbook example asking for dynamic programming. The time limit is chosen such that success is impossible without dynamic programming. Dynamic programming can be applied to recursive proble …
Rainer P.'s user avatar
  • 2,855
1 vote

Code for calculating number of variations

loadTestCases(args); sort(nums.begin(),nums.end()); int cases = countPairs(nums, k); printf("%i\n",cases); return 0; } If you want to improve it any further, you must focus on the sorting algorithm … You may consider using an Integer Sorting algorithm instead of the generic comparison sort. …
Rainer P.'s user avatar
  • 2,855
5 votes
Accepted

Project Euler #1 in Go (counting multiples of 3 or 5 up to 1000)

func Lcm(a, b int) int { return a * b / Gcd(a, b); } The least common multiple can be calculated from the greatest common divisor, which itself can be found via Euclid's algorithm. …
Rainer P.'s user avatar
  • 2,855
0 votes

Checking if an integer permutation is cyclic in Java

I see several flaws here: It's wrong. It returns false for [0,1,2] or [1,0,2], which are a valid cyclic permutations. A permutation is cyclic if it contains at most one nontrivial cycle. [1,0,3,2] w …
Rainer P.'s user avatar
  • 2,855
1 vote

Union find using unordered map and templates in C++

There is no need for unordered maps, especially not for two of them. Also, since you are dealing with indices and counts, you should make them std::size_t, not int or generic. The datatype for storing …
Rainer P.'s user avatar
  • 2,855
2 votes

Given two words, transform the first one into the second

The algorithm is wrong and can't be fixed. It's not dynamic programming, it's greedy: it iterates over the strings once and decides immediately which operation to perform. … Brute force The naive way to overcome this problem is a brute force algorithm. Evaluate all possible sequences of operations (insertions, deletions, replacements). …
Rainer P.'s user avatar
  • 2,855
5 votes

Minimum swaps algorithm terminated due to timeout

Your algorithm can be summarized by the pseudocode: for each position in the array if a position is occupied by the wrong number find the number that fits into the position perform … a swap A better algorithm would be: for each position in the array if a number is in the wrong location find the position the number should go perform a swap This is because finding …
Rainer P.'s user avatar
  • 2,855