Category "algorithm"

How to approach this Dynamic Programming Meal Plan Problem?

I came across this problem in a practice interview. I don't want a direct answer, just some help with the intuition of the problem, both your brute force thinki

Is there a known algorithm for finding which K elements out of N elements have a sum that is closest to a whole number?

As a small example, say I have N=6 elements { 0.03, 0.25000039, 1.391, 500.1, 0.5000001, 1.75001 } and K=3 then the combination { 0.25000039, 0.5000001, 0.

count words in a string without using split

I have a problem on which I am working where I need to count the number of words in a string without using the split() function in Python. I thought of an appro

how to calculate XOR (dyadic) convolution with time complexity O(n log n)

“⊕” is the bitwise XOR operation. I think Karatsuba’s algorithm may be used to solve the problem, but when I try to use XOR instead of

Java: How to implement wildcard matching?

I'm researching on how to find k values in the BST that are closest to the target, and came across the following implementation with the rules: '?' Matches

How to generate the worst case for disjoint set with only path compression?

A disjoint set with only path compression implemented is like this: // In cpp. int Find(int x) { return f[x] == x ? x : f[x] = Find(f[x]); } int Union(int a, i

Amazon interview: Min stack

I recently had my Amazon interview for SDE. I was asked to design a stack which does push, pop and min in O(1). I got the logic and implemented the push of the

How to reduce the time complexity of this algorithm?

I provide two data structures, cart_info and map_case, and get the result real_combinationby the algorithm. This algorithm is implemented in python import itert

Finding the shortest path with only passing specific edge less or equal to one time in Graph

Given a undirected graph that it has ordinary edges and specific edges, our goal is to find the sum of the shortest path's weight between two vertices(start ve

Algorithm to find counterfeit coin amongst n coins

So this is the classic problem of finding a counterfeit coin among a set of coins using only a weighing balance. For completeness, here is one example of such a

Count of combinations with repetitions

I have a very inefficient way of counting the combinations of N/2 items from an array of size N. What I do is sort the array to begin with and then loop through

Convert camelCase to snakeCase

I have a dictionary something like this: { 'firstName': 'abc', 'lastName': 'xyz', 'favoriteMovies': ['Star Wars', 'The lone ranger'], 'favo

Does the size of the prime number in Shamir's Secret Sharing affect the security of the sharding?

I've been working on an implementation of Shamir's Secret Sharing, and was wondering if the prime number selected will impact on the security. This is mainly be

Resizing an array by a non-constant, continually

I’d like to perform amortized analysis of a dynamic array: When we perform a sequence of n insertions, whenever an array of size k fills up, we reallocate

Optimization of a short-length cyclic convolution

I have two sequences of 8 unsigned bytes and I need to compute their cyclic convolution, which yields 8 unsigned 19 bits integers. As I repeat this million time

A program to evaluate arithmetic expression

Here's an interesting problem I haven't managed to deal with yet. Given an arithmetic expression in Reverse Polish Notation, write a program to evaluate it. T

When can we have duplicate nodes in the heap in dijikstras algorithm?

So this question came to my mind when solving some dijikstra's based leetcode problems. We have node, distance pairs in priority queue at each step. Having dupl

Longest K Sequential Increasing Subsequences

Why I created a duplicate thread I created this thread after reading Longest increasing subsequence with K exceptions allowed. I realised that the person who wa

Count how many iterations of deletion until array is ordered

I'm trying to write a program whose input is an array of integers, and its size. This code has to delete each element which is smaller than the element to the l

which algorithm does google keyboard uses for automatic suggestions (personal vocab included)?

I am confused since google cannnot train their text generation models with each individuals personal vocabulary. I was trying to develop something similar but