Category "algorithm"

Directed SPT vs SPT?

Question: If a graph contains no negative-weight cycles, does there exist an ordering of edges such that Bellman-Ford computes shortest paths by relaxing each e

Find if two strings are anagrams

Faced this question in an interview, which basically stated Find if the given two strings are anagrams of each other in O(N) time without any extra space I tr

Generating a shuffled range in python

I need to generate (iterator) a random sequence of 32 bit integers with absolutely no repeats. The naive solutions to this problem don't work for me because of

Find the interval that contains the largest amount of intervals and enter their number

Given lots of intervals [ai, bi], find the interval that contains the most number of intervals. I easily know how to do this in O(n^2) but I must do it in O(nlo

Looping problem Elden ring runes per level calculation

This code is currently showing the total amount needed to reach the next level, but what I would like it to do is show the complete total amount so for example

How to insert records into a Nested HashMap?

I want to create a nested HashMap of: var myHashMap = new HashMap<String, HashMap<String, int>>(); And I want to insert records into the inner Hash

Finding maximum difference between consecutive elements of an array

Say I have an array: [44, 30, 24, 32, 35, 30, 40, 38, 15] Here, every number represents a stock value on a certain day. I want to find the maximum profit that

My segment tree update function doesn't work properly

The problem: In this task, you need to write a regular segment tree for the sum. Input The first line contains two integers n and m (1≤n,m≤100000), the s

Python modulo returning wrong answer

I was attempting this question on leetcode using python3. My solution for this problem is as follows: class Solution: def __init__(self): self.memo

How to apply Kleene star on automata?

I know how to apply Kleene star on language but I'm not sure how would I apply it to DFA or NFA. I'm pretty sure it would need to be epsilon NFA with initial st

how to check whether specific word are included in dictionary value - Python

I want to make a search program but i stuck in specific alogrithm. First, I will get any word from users Then check wheter user's words are included in any keyw

How to bitwise rotate left and rotate right on arbitrary sized number ranges?

How to rotate right and left over an arbitrary number of bits. So say I am looking at 5 bits. How do I rotate around 5 bits, though I am in JavaScript. Or 8 bit

Find the maximum number of distinct elements that can be achieved in array a after at most k operations

My assignment: Consider two arrays a and b where each consists of n integers. In one operation: Select two indices i and j (0 <= i,j < n) Swap integers a

Given an array find all the pairs with product between a given range

Given an array having n elements, find all the possible pairs of elements whose product is between x and y provided by the user, i.e. all i and j less than n, s

Reasonable neighbours for a bees algorithm on a not-complete digraph

I am trying to solve an optimization problem for a graph, where basically there is a cost for transporting "package" over a single edge, and I have multiple sou

Island perimeter problem - cannot count all neighbor cells

I'm working on the Island Perimeter Problem from LeetCode. I tried to use the DFS algorithm, based on LeetCode#200, to solve this problem. For example 1, the ou

How to calculate median of an unsorted frequency table in O(n)?

I have a dataset comprised of n unsorted tuples representing numbers (let's say specific color codes) and their frequency (number of times of appearance). I wan

Is it right to implement queue with single linked list?

My Code ↓ Some macros defined by #include <stdio.h> #include <malloc.h> #define ElementType int #define ERROR 0 // 队列ஷ

Extracting data from a .txt file without using modules

I am taking a course in python and one of the problem sets is as follows: Read in the contents of the file SP500.txt which has monthly data for 2016 and 2017 a

How to solve TSP problem using pyGAD package?

Using PyGAD Package, how to generate the item of populations with element not duplicate between 1 and 12? It is always has duplicate value in random population.