dynamic-programming

Given a phrase without spaces add spaces to make proper sentence

拜拜、爱过 提交于 2019-12-07 12:59:55
问题 This is what I've in mind, but it's O(n^2): For ex: Input is "Thisisawesome", we need to check if adding the current character makes the older found set any longer and meaningful. But in order to see till where we need to back up we'll have to traverse all the way to the beginning. For ex: "awe" and "some" make proper words but "awesome" makes the bigger word. Please suggest how can we improve the complexity. Here is the code: void update(string in) { int len= in.length(); int DS[len]; string

Number of students with better grades and lower jee rank

半城伤御伤魂 提交于 2019-12-07 11:55:21
We are given n students with cgpa (college grades) and jee ranks (Rank in admission exam) of every student. For every student, we have to calculate the number of students who have better cgpa but worse jee rank. (x1,y1), (x2,y2) ...(xi,yi)... (xn,yn) for each i, we have to calculate no. of j for which xj > xi and yj > yi (worse rank means greater rank.) I could come up with the following nlogn algorithm- Sort them decreasing cgpa. Now start scanning from left. Maintain the students scanned so far in a balanced binary tree (according to their jee rank). For the next student, just find out the

find the longest sequence S that is a subsequence of A,B,C string

三世轮回 提交于 2019-12-07 11:40:51
问题 Give a polynomial time algorithm that takes three strings, A, B and C, as input, and returns the longest sequence S that is a subsequence of A, B, and C. 回答1: Let dp[i, j, k] = longest common subsequence of prefixes A[1..i], B[1..j], C[1..k] We have: dp[i, j, k] = dp[i - 1, j - 1, k - 1] + 1 if A[i] = B[j] = C[k] max(dp[i - 1, j, k], dp[i, j - 1, k], dp[i, j, k - 1]) otherwise Similar to the 2d case, except you have 3 dimensions. Complexity is O(len A * len B * len C) . 回答2: Here's a solution

Understanding regex string matching using Dynamic Programming

风格不统一 提交于 2019-12-07 11:26:31
问题 I came across this problem that asks you to implement a regular expression matcher with support for '.' and '*', where '.' Matches any single character. '*' Matches zero or more of the preceding element. isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true While I was able to solve this in a linear fashion, I came across lots of solutions that used DP,

What's the time complexity of this algorithm for Palindrome Partitioning?

↘锁芯ラ 提交于 2019-12-07 08:39:31
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Personally I think, the time complexity is O(n^n), n is the length of the given string. Thank you Dan Roche , the tight time complexity = O(n* (2^n)), check details below. #include <vector> using namespace std; class Solution { public: vector<vector<string>> partition(string s) { vector<vector<string>> list; vector<string> subList; // Input validation. if (s.length() <= 1) { subList.push_back(s); list.push_back(subList); return list

maximizing profit for given stock data via DP

穿精又带淫゛_ 提交于 2019-12-07 07:39:28
You are given the stock prices for a set of days . Each day, you can either buy one unit of stock, sell any number of stock units you have already bought, or do nothing. What is the maximum profit you can obtain by planning your trading strategy optimally? Now the answer can be obtained through single pass but what if it has to be solved through Dynamic Programming. What will be the recurrence relation then for the problem? I think for any day OPT(i) can denote maximum profit earned so far, so one has to sell all remaining shares on the last day so OPT(i) = max ( Sell all on this day bought

A dance with an aglorithm's complexity

偶尔善良 提交于 2019-12-07 06:56:28
I am about to take part in a dance contest and tomorrow is the big day! I know apriori the list with the n songs of the contest and their order. After much scouting, I was able to determine the judges and my skills so good that I can accurately predict my result if I dance the i-th song of the list, i.e. score(i) . However, after the i-th song, I need time to rest, namely I cannot dance the next rest(i) songs, i.e. songs i + 1, ..., i + rest(i). No other constraint exists for the number of songs I can dance. Give an effective algorithm for computing your ideally maximum total score and its

How to find if a subarray has a specific sum inside a 2D array in Java ?

元气小坏坏 提交于 2019-12-07 05:46:42
问题 I am trying to solve a Image matching problem by comparing the average color of pixels present in both the source and pattern image. I have reduced this problem to a sub array sum problem, but cannot figure out a way to solve it. Lets say i have a 2D array ARR with all positive integers. I have a number x( which is the average of the pixel colors present in a small pattern image). I just need to find any subarray in ARR which has the exact sum x. I found a similar problem which could be

Word segmentation using dynamic programming

孤人 提交于 2019-12-07 02:51:58
问题 So first off I'm very new to Python so if I'm doing something awful I'm prefacing this post with a sorry. I've been assigned this problem: We want to devise a dynamic programming solution to the following problem: there is a string of characters which might have been a sequence of words with all the spaces removed, and we want to find a way, if any, in which to insert spaces that separate valid English words. For example, theyouthevent could be from “the you the vent”, “the youth event” or

Distribution of balls into 'bins with given capacities' using Dynamic Programming

↘锁芯ラ 提交于 2019-12-07 02:45:03
问题 I was wondering how to solve such a problem using DP. Given n balls and m bins, each bin having max. capacity c1, c2,...cm. What is the total number of ways of distributing these n balls into these m bins. The problem I face is How to find a recurrence relation (I could when the capacities were all a single constant c). There will be several test cases, each having its own set of c1,c2....cm. So how would the DP actually apply for all these test cases because the answer explicitly depends on