Server Side Programming Articles - Page 1828 of 2650

Minimize Rounding Error to Meet Target in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:09:51

452 Views

Suppose we have an array of prices P [p1, p2..., pn] and a target value, we have to round each price Pi to Roundi(Pi) so that the rounded array [Round1(P1), Round2(P2)..., Roundn(Pn)] sums to the given target value. Here each operation Roundi(pi) could be either Floor(Pi) or Ceil(Pi).We have to return the string "-1" if the rounded array is impossible to sum to target. Otherwise, return the smallest rounding error, which will be (as a string with three places after the decimal) defined as −$\displaystyle\sum\limits_{i-1}^n |Round_{i} (???? ) - ????$So if the input is like [“0.700”, “2.800”, “4.900”], and the ... Read More

Shortest Way to Form String in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:04:56

336 Views

Suppose we have a string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions). So if there is two strings source and target, we have to find the minimum number of subsequences of source such that their concatenation equals target. If the task is impossible, then return -1. So if source is “abc” and target is “abcbc”, then the output will be 2.To solve this, we will follow these steps −Define a string called possible, this will take s and t as inputcreate a map mfor each character c in s mark ... Read More

Last Stone Weight II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:01:51

1K+ Views

Suppose we have a collection of rocks, now each rock has a positive integer weight. In each turn, we choose any two rocks and smash them together. If the stones have weights x and y with x = 0, decrease j by 1dp[j] := false when (dp[j] and dp[j – stones[i]]) both are false, otherwise trueif dp[j] is true, then reach := max of reach and jreturn total – (2 * reach)Let us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution {    public:    int lastStoneWeightII(vector& stones) {       ... Read More

Longest String Chain in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:57:50

549 Views

Suppose we have a list of words, here each word consists of lowercase letters. So one word word1 is a predecessor of another word word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For the example of the predecessor is like, "abc" is a predecessor of "abac". Now a word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. We have to find the longest possible length of ... Read More

Robot Bounded In Circle C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:54:21

324 Views

Suppose we have an infinite plane, a robot initially stands at position (0, 0) and faces north. The robot can receive one of three instructions −G − go straight 1 unit;L − turn 90 degrees to the left direction;R − turn 90 degrees to the right direction.The robot performs the instructions given in order, Instructions are repeated forever. We have to check whether there exists a circle in the plane such that the robot never leaves the circle. So if the input is like [GGLLGG], then the answer will be true. from (0, 0) to (0, 2), it will loop ... Read More

Minimum Score Triangulation of Polygon in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:49:19

365 Views

Suppose we have a value N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] are in clockwise order. Now suppose we want to triangulate the polygon into N-2 triangles. For each triangle, the value of that triangle is the product of the labels of the vertices, and the total score of the triangulation will be the sum of these values over all N-2 triangles in the triangulation. We have to find the smallest possible total score that we can achieve with some triangulation of the polygon. So if the input is [1, 2, 3], then the ... Read More

Uncrossed Lines in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:45:20

244 Views

Suppose we have written the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that −A[i] == B[j];The line we draw that does not intersect any other connecting (non-horizontal) line.We have to keep in mind that connecting lines cannot intersect even at the endpoints − each number can only belong to one connecting line. Find the maximum number of connecting lines. So if the input is like [1, 4, 2] and [1, 2, 4], then the output ... Read More

Longest Arithmetic Sequence in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:39:57

369 Views

Suppose we have an array A of integers, we have to return the length of the longest arithmetic subsequence in A. As you know that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0

Video Stitching in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:30:27

283 Views

Suppose we have a series of video clips from a sporting event that lasted T seconds. Now these video clips can be overlapping with each other and have varied lengths. Here each video clip clips[i] is an interval − it starts at clips[i][0] time and ends at clips[i][1] time. We can cut these clips into segments freely − We have to find the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event ([0, T]). If the task is impossible, return -1. So if the input is like [[0, 2], ... Read More

Number of Enclaves in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:24:45

230 Views

Suppose we have given a 2D array A, now each cell is 0 (representing sea) or 1 (representing land) Here a move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid. We have to find the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves. So if the grid is like −0000101001100000The answer will be 3, as there are three ones enclosed by 0s, and one 1 is not enclosed.To solve this, we will follow these ... Read More

Advertisements