C++ Articles - Page 356 of 719

Maximum Profit in Job Scheduling in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:41:34

1K+ Views

Suppose we have n different tasks, where every task is scheduled to be done from startTime[i] to endTime[i], for that task we algo get profit of profit[i]. We know the startTime , endTime and profit lists, we have to find the maximum profit we can take such that there are no 2 tasks in the subset with overlapping time range. If we choose a task that ends at time X we will be able to start another task that starts at time X.So, if the input is like startTime = [1, 2, 3, 3], endTime = [3, 4, 5, 6] ... Read More

Maximum Equal Frequency in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:37:43

280 Views

Suppose we have an array nums of positive integers, we have to return the longest possible length of an array prefix of given array nums, such that it is possible to delete exactly one element from this prefix so that every number that has appeared in it will have the same frequency. After removing one element if there are no remaining elements, it's still considered that every appeared number has the same frequency (0).So, if the input is like [3, 3, 2, 2, 6, 4, 4, 6], then the output will be 7, So if we remove element 6 from ... Read More

Count Vowels Permutation in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:34:36

294 Views

Suppose we have one number n, we have to count how many strings of length n can be formed using these rules − Each character is a lower case vowel Each vowel 'a' may only be followed by an 'e'. Each vowel 'e' may only be followed by an 'a' or 'i'. Each vowel 'i' may not be followed by another 'i'. Each vowel 'o' may only be followed by an 'i' or 'u'. Each vowel 'u' may only be followed by an 'a'. The answer may be too large, so we will find the answer modulo 10^9 + 7.So, ... Read More

Critical Connections in a Network in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:31:44

514 Views

Suppose there are n servers. And these are numbered from 0 to n-1 connected by an undirected server-to-server connections forming a network where connections[i] = [a, b] represents a connection between servers a and b. All servers are connected directly or through some other servers. Now, a critical connection is a connection that, if that is removed, it will make some server unable to reach some other server. We have to find all critical connections.So, if the input is like n = 4 and connection = [[0, 1], [1, 2], [2, 0], [1, 3]], then the output will be [[1, ... Read More

Make Array Strictly Increasing in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:27:45

414 Views

Suppose we have two arrays arr1 and arr2, these can store integer numbers. We have to find the minimum number of operations needed to make arr1 strictly increasing. Here, we can choose two indices 0 size of arr2, then -1, otherwise ret - 1)Let us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution {    public:    int solve(vector& arr1, vector& arr2, int i, int j, int prev, vector& dp){       if (i >= arr1.size())          return 1;       j = upper_bound(arr2.begin() + j, ... Read More

Last Substring in Lexicographical Order in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:24:58

315 Views

Suppose we have s as string, we have to find the last substring of s in lexicographic order.So, if the input is like "abbbcabbc", then the output will be "cabbc"To solve this, we will follow these steps −i := 0, j := 1, k := 0while j + k < size of s, do &minsu;if s[i + k] is same as s[j + k], then −(increase k by 1)Ignore following part, skip to the next iterationif s[i + k] < s[j + k], then −i := j(increase j by 1)Otherwisej := j + k + 1k := 0return substring of ... Read More

Shortest Common Supersequence in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:14:39

217 Views

Suppose we have two strings str1 and str2, we have to find the shortest string that has both str1 and str2 as subsequences. There may be more than one results, so we will return only one of them.As you know a string S is called a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.So, if the input is like "acab", "bac", then the output will be "bacab", this is because two given strings are subsequence of this.To solve this, we will ... Read More

Number of Submatrices That Sum to Target in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:09:58

236 Views

Suppose we have a matrix, and a target value, we have to find the number of non-empty submatrices that sum is same as target. Here a submatrix [(x1, y1), (x2, y2)] is the set of all cells matrix[x][y] with x in range x1 and x2 and y in range y1 and y2. Two submatrices [(x1, y1), (x2, y2)] and [(x1', y1'), (x2', y2')] are different if they have some coordinate that is different: like, if x1 is not same as x1'.So, if the input is like010111010and target = 0, then the output will be 4, this is because four 1x1 ... Read More

Longest Duplicate Substring in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:06:27

671 Views

Suppose we have a string S, consider all duplicated contiguous substrings that occur 2 or more times. (The occurrences may overlap.), We have to find the duplicated substring that has the longest possible length. If there is no such substrings, then return a blank string. As the answer may very large, so return in mod 10^9 + 7.So, if the input is like "ababbaba", then the output will be "bab"To solve this, we will follow these steps −m := 1e9 + 7Define a function add(), this will take a, b, return ((a mod m) + (b mod m)) mod mDefine ... Read More

Stream of Characters in C++

Arnab Chakraborty
Updated on 04-Jun-2020 10:58:38

614 Views

Suppose we want to implement the StreamChecker class as follows −StreamChecker(words) − This is the constructor, this initializes the data structure with the given words.query(letter) − This returns true when for some k >= 1, the last k characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.So, if the input is like word list = ["ce", "g", "lm"], then call query many times for [a, b, c, e, f, g, h, i, j, k, l, m], then the output will be true for e, g, m, and ... Read More

Advertisements