Questions tagged [complexity]
Complexity is the analysis of how the time and space requirements of an algorithm vary according to the size of the input. Use this tag for reviews where the "Big O" is a concern.
419 questions
5
votes
1
answer
363
views
Iterative DFS with Optimized Space Complexity
Background
I can't seem to find an existing answer solving this doubt of mine.
In academic books, it seems that DFS is usually presented in both recursive and iterative forms.
The implementations ...
5
votes
4
answers
558
views
Count number of substrings in less time
Given an input string contains letters from 'a' to 'g'
Count how many substrings are there in the input string such that
frequency of any character inside the substring is not more than the
number of ...
7
votes
4
answers
552
views
Traversal Heap Sort (No Extractions)
I developed a heap sort variant that sorts a heap through traversal. Unlike in the standard heap sort, the algorithm does not remove the min element from the heap instead it traverses all the nodes of ...
2
votes
2
answers
271
views
How to optimize and refactor C# method for validating business rules in terms of readability, maintainability, and performance?
I'm working on a C# project and have a bloater method ValidateBusinessRules that checks a series of business rules for validating the operations. The current code ...
5
votes
1
answer
396
views
Fast and precise summation of random numbers
I'm trying to first formulate this challenge.
In short, we want to sum up an stream of random numbers with the following objective:
The objective is "simply" to sum up as many sequences as ...
5
votes
2
answers
857
views
Simplify complexity [closed]
I came across this question which asks to create a function which will return true/false based on the passed array containing all the letters to make up the passed word. Each letter from array can ...
4
votes
1
answer
168
views
Longest spell to cast from pages of spellbook follow-up
This question is from the PCTC 2022 R2 Past Paper and is a follow-up on my previous question. Previous question
I have implemented several solutions suggested, such as creating an array with pages ...
4
votes
3
answers
727
views
Leetcode 3sum problem solution
The problem is:
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] where ...
-2
votes
2
answers
121
views
complexity reduction (refacto) [closed]
I'm trying to reduce the complexity of this piece of code following a SonarQube error. I have several similar blocks, and even if I try to extract them into private methods, the Sonar error persists.
...
1
vote
1
answer
144
views
Binary search of 2D matrix
I have written code as below:
...
1
vote
1
answer
405
views
Finding highly correlated variables in a dataframe by evaluating its correlation matrix's values
I read data from Excel into a Pandas DataFrame, so that every column represents a different variable, and every row represents a different sample. I made the function below to identify potential ...
2
votes
1
answer
256
views
Pre-calculate attacks in a chess game
I'm writing (or at least trying to write) a chess engine in C++. This is the first time I write code in C++, and I use this project to practice. Anyway, I'm following the general guidelines of a ...
6
votes
1
answer
516
views
Doubly linked list first fit free list malloc/free in Python
As an exercise I've implemented malloc and free in Python as a first fit free list as described here. This tracks which blocks are free in a doubly linked list that is sorted by the address of the ...
0
votes
2
answers
303
views
Binary Search written in python with O(n) [closed]
def search(nums, target):
for i in range(len(nums)):
if nums[i] == target:
return i
if nums[i] == None:
return -1
I think ...
3
votes
2
answers
467
views
Add two numbers represented as digit strings
This algorithm adds two numbers digit by digit using their string representations.
I am wondering what the time and space complexities of the following algorithm are. In particular, I am wondering ...
1
vote
1
answer
414
views
Printing subarrays ⚡
I was trying to to print all subarrays of an array in quadratic time. Here is my code:
...
1
vote
2
answers
492
views
Algorithm for twoSum
This is my PHP algorithm for the twoSum problem in leetCode:
...
3
votes
1
answer
580
views
Finding the minimum element of a stack of numbers
As part of an online programming challenge, I wrote a program that implements a stack of integers, supporting adding and removing elements, finding the last inserted element, and the minimum element. ...
0
votes
2
answers
1k
views
Separating odd numbers and even numbers into two vectors in C++
I'm learning C++, and would like some guidance. I'm pretty sure I'm doing something wrong.
This algorithm runs in O(n) time and uses O(n) external space. Could I make a more efficient algorithm for ...
-2
votes
2
answers
156
views
1
vote
2
answers
344
views
SumList Cracking the coding Interview
Sum of 2 numbers, represented with linked lists, where digits are in backward order or forward order.
Backward order Example
INPUT:(7->1->6) + (5->9->2) = 617+295
OUTPUT: 2->1->9 = ...
3
votes
1
answer
279
views
Find the largest decrease in a sequence of numbers
I have written a function that takes as input a list of 5 numbers, then the program's goal is to return the largest drop in the list. It is important to understand that [5, 3, ...] is a decrease of 2 ...
1
vote
2
answers
1k
views
URLify a given String - replace spaces with %20
I have written a code to replace all spaces in a String with %20.
...
3
votes
1
answer
148
views
Substring search in Java
I am trying to make an algorithm that will find the index of a given substring (which I have labeled pattern) in a given String (...
2
votes
0
answers
130
views
Monotonic stack - complexity analysis
I tried following the official solution of LC975 (https://leetcode.com/problems/odd-even-jump/), using a monotonic stack - i.e., I reimplemented their solution (Approach 1) from python in C++.
TLDR ...