Programming Articles - Page 1931 of 3363

Text Justification in C++

Arnab Chakraborty
Updated on 26-May-2020 12:18:51

2K+ Views

Suppose we have an array of words and a width maxWidth, we have to format the text such that each line has exactly maxWidth number of characters and is fully justified. We should pack our words in a greedy approach; so that is, pack as many words as we can in each line. We will pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.Here extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, empty slots on the left ... Read More

Insert Interval in C++

Arnab Chakraborty
Updated on 26-May-2020 12:08:42

2K+ Views

Suppose we have a set of non-overlapping intervals; we have to insert a new interval into the intervals. We can merge if necessary. So if the input is like − [[1, 4], [6, 9]], and new interval is [2, 5], then the output will be [[1, 5], [6, 9]].To solve this, we will follow these steps −Insert new interval at the end of the previous interval listsort the interval list based on the initial time of the intervals, n := number of intervalscreate one array called ans, insert first interval into ansindex := 1while index < n, last := size ... Read More

Jump Game II in Python

Arnab Chakraborty
Updated on 26-May-2020 12:01:30

925 Views

Suppose we have one array of integers, where all elements are positive. The initial starting point is at index 1. Each element in the array represents our maximum jump length at that position. Our goal is to reach to the final cell with less number of jumps. So if the array is like [2, 3, 1, 1, 4], and then the output will be 2, as we can jump to index 1 from 0, then jump to index 4, that is the last index.To solve this, we will follow these steps −end := 0, jumps := 0, farthest := 0for ... Read More

Wildcard Matching in Python

Arnab Chakraborty
Updated on 26-May-2020 11:58:18

4K+ Views

Suppose we have an input string s and another input string p. Here is the main string and p is the pattern. We have to define one method, that can match pattern in the string. So we have to implement this for a regular expression, that supports wildcard characters like ‘?’ And ‘*’.Dot ‘?’ Matches any single characterStar ‘*’ Matches zero or more characters.So for example, if the input is like s = “aa” and p = “a?”, then it will be true, for the same input string, if the patter is “?*”, then it will be true.To solve this, ... Read More

Trapping Rain Water in Python

Arnab Chakraborty
Updated on 26-May-2020 11:55:41

1K+ Views

Suppose we have an array of n non-negative integers. These are representing an elevation map where the width of each bar is 1, we have to compute how much water it is able to trap after raining. So the map will be like −Here we can see there are 6 blue boxes, so the output will be 6.To solve this, we will follow these steps −Define a stack st, water := 0 and i := 0while i < size of heightif is stack is empty or height[stack top] >= height[i], then push i into stack, increase i by 1otherwisex := ... Read More

First Missing Positive in Python

Arnab Chakraborty
Updated on 26-May-2020 11:51:41

940 Views

Suppose we have one unsorted integer array; we have to find the smallest missing positive number. So if the array is like [4, -3, 1, -1], then the result will be 2.To solve this, we will follow these steps −set i := 0 and update array nums by adding one 0 before all numbersfor i in range 0 to length of numswhile nums[i] >= 0 and nums[i] < length of nums and nums[nums[i]] is not nums[i] −nums[nums[i]] := nums[i]nums[i] := nums[nums[i]]num := 1for i in range 1 to length of numsif num = nums[i], then increase num by 1return numExampleLet ... Read More

Sudoku Solver in C++

Arnab Chakraborty
Updated on 26-May-2020 11:48:01

17K+ Views

Suppose we have a Sudoku grid and we have to solve this famous number maze problem, Sudoku. We know that Sudoku is a 9 x 9 number grid, and the whole grid are also divided into 3 x 3 boxes There are some rules to solve the Sudoku.We have to use digits 1 to 9 for solving this problem.One digit cannot be repeated in one row, one column or in one 3 x 3 box.Using backtracking algorithm, we will try to solve Sudoku problem. When some cell is filled with a digit, it checks whether it is valid or not. ... Read More

Longest Valid Parentheses in Python

Arnab Chakraborty
Updated on 26-May-2020 11:41:17

1K+ Views

Suppose we have a string, with opening and closing parentheses. We have to find the longest length of the valid (well-formed) parentheses. So if the input is like “))(())())”, then the result will be 6, as the valid string is “(())()”.To solve this, we will follow these steps −Make a stack, and insert -1., set ans := 0for i in range 0 to length of stack – 1if s[i] is opening parentheses, then insert i into stackotherwiseif stack is not empty and top of stack is not -1 and s[stack top] is opening parentheses, thentop element from stackans := max ... Read More

Substring with Concatenation of All Words in C++

Arnab Chakraborty
Updated on 26-May-2020 11:38:04

207 Views

Suppose we have a string, s, and we also have a list of words, words present in the array are all of the same length. We have to find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.So if the input is like “barfoothefoobarman” and words are [“foo”, “bar”], then the output will be [0, 9]. This is because the substring starting at index 0 and 9 are “barfoo” and “foobar”.To solve this, we will follow these steps −Define a method called ok(), this will take ... Read More

Reverse Nodes in k-Group in C++

Arnab Chakraborty
Updated on 26-May-2020 11:34:39

292 Views

Suppose we have a linked list, we have to reverse the nodes of the linked list k at a time and return its modified list. Here k is a positive integer and is less than or equal to the length of the linked list. So if the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.So if the linked list is like [1, 2, 3, 4, 5, 6, 7] and k is 3, then the result will be [3, 2, 1, 6, 5, 4, 7].To solve this, we will ... Read More

Advertisements