dynamic-programming

Google Interview: Find all contiguous subsequence in a given array of integers, whose sum falls in the given range. Can we do better than O(n^2)?

主宰稳场 提交于 2021-02-05 12:42:11
问题 Given an array of Integers, and a range (low, high), find all contiguous subsequence in the array which have sum in the range. Is there a solution better than O(n^2)? I tried a lot but couldn't find a solution that does better than O(n^2). Please help me find a better solution or confirm that this is the best we can do. This is what I have right now, I'm assuming the range to be defined as [lo, hi] . public static int numOfCombinations(final int[] data, final int lo, final int hi, int beg,

Find all possible combinations that overlap by end and start

ⅰ亾dé卋堺 提交于 2021-02-05 11:22:06
问题 In the post find all combinations with non-overlapped regions (code pasted below), the function is given a set of tuples and it recursively finds every possible collection of tuples with non-overlapping values. On the list of tuples T = [(0.0, 2.0), (0.0, 4.0), (2.5, 4.5), (2.0, 5.75), (2.0, 4.0), (6.0, 7.25)] , for example, we get def nonovl(l, idx, right, ll): if idx == len(l): if ll: print(ll) return next = idx + 1 while next < len(l) and right >= l[next][0]: next += 1 nonovl(l, next,

Find all k-size subsets with sum s of an n-size bag of duplicate unsorted positive integers

痴心易碎 提交于 2021-02-04 18:11:10
问题 Please note that this is required for a C# .NET 2.0 project ( Linq not allowed ). I know very similar questions have been asked here and I have already produce some working code (see below) but still would like an advice as to how to make the algorithm faster given k and s conditions. This is what I've learnt so far: Dynamic programming is the most efficient way to finding ONE (not all) subsets. Please correct me if I am wrong. And is there a way of repeatedly calling the DP code to produce

Create a schedule where a group of people all talk to each other - with restrictions

青春壹個敷衍的年華 提交于 2021-01-29 15:31:22
问题 Problem statement I would like to achieve the following: (could be used for example to organize some sort of a speeddating event for students) Create a schedule so people talk to each other one-on-one and this to each member of the group. but with restrictions. Input : list of people. (eg. 30 people) Restrictions : some of the people should not talk to each other (eg. they know each other) Output : List of pairs (separated into sessions) just one solution is ok, no need to know all of the

Find the index of the subarray whose sum is minimum

吃可爱长大的小学妹 提交于 2021-01-29 08:01:15
问题 Given an array of length n, with integers(can be negative or positive). Find the starting and ending index of the subarray with minimum sum possible. 回答1: As you specified this is about Kadanes Algorithm, it is easy to find a lot of references about it. GeeksForGeeks : Smallest sum contiguous subarray kindly explains about the algorithm and provides implementations in a few languages. Based on the python code from that page, I have revised it to return begin/end indices rather than the sum

All possible paths from top left to bottom right of a mXn matrix

久未见 提交于 2021-01-29 05:43:33
问题 I was going through this leetcode problem for going from top left to bottom right. How many possible unique paths are there? I was able to understand this method of dynamic programming by storing the result for every index. public int uniquePaths(int m, int n) { int count[][] = new int[m][n]; for (int i = 0; i < m; i++) count[i][0] = 1; for (int j = 0; j < n; j++) count[0][j] = 1; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { count[i][j] = count[i-1][j] + count[i][j-1]; //+

Calculating How Many Balls in Bins Over Several Values Using Dynamic Programming [closed]

假如想象 提交于 2021-01-27 12:43:39
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . Improve this question Regarding the classic problem of putting N identical balls into M distinct bins and printing all the combinations: What if you would want to extend the problem by printing all cases 0< M, N The brute force method could be done something like this: for (int i

Coin change using dynamic programming

安稳与你 提交于 2021-01-21 09:50:52
问题 I've been working on coin change problem using dynamic programming. I've tried to make an array fin[] which contains the minimum number of coins required for that index and then print it. I've written a code which I think should give correct output but I can't figure out why it is not giving exact answer. For eg: for the input: 4 3 1 2 3 (4 is the amount to change, 3 the number of types of available coins, 1 2 3 is the list of coin values) The output should be: 0 1 1 1 2 (as we have 1,2,3 as

Primitive Calculator - Dynamic Approach

依然范特西╮ 提交于 2021-01-21 05:07:29
问题 I'm having some trouble getting the correct solution for the following problem: Your goal is given a positive integer n, find the minimum number of operations needed to obtain the number n starting from the number 1. More specifically the test case I have in the comments below. # Failed case #3/16: (Wrong answer) # got: 15 expected: 14 # Input: # 96234 # # Your output: # 15 # 1 2 4 5 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234 # Correct output: # 14 # 1 3 9 10 11 22 66 198 594 1782

Speed up search for the smallest x such that f(x) = target

与世无争的帅哥 提交于 2021-01-07 02:45:17
问题 Problem Given n , find the smallest positive x such that f(x) = n . f(x) is the sum of the digit sum of the factorials of the digits of x . For example, f(15) = digit_sum(1!) + digit_sum(5!) = digit_sum(1) + digit_sum(120) = (1) + (1 + 2 + 0) = 4 Breath first search can find the answer. Are there faster ways? Breath First Search def bfs(target, d_map): # Track which values of f(x) have we visited visited = set([0]) # f(x) of the current level of the search tree todo = [0] # Digits of x for