Minimum Days to Wait for Profit in Python

Arnab Chakraborty
Updated on 26-Nov-2020 08:18:33

267 Views

Suppose we have a list of prices representing the daily stock market prices of a company in chronological sequence. We have to find a same length list where the value at index i will be the minimum number of days we would have to wait until we make a profit. If there is no such way to make a profit the value should be 0.So, if the input is like prices = [4, 3, 5, 9, 7, 6], then the output will be [2, 1, 1, 0, 0, 0]To solve this, we will follow these steps:ans := a list of ... Read More

Find All Possible Strings Typed Using Phone Keypad in Python

Arnab Chakraborty
Updated on 26-Nov-2020 08:15:07

1K+ Views

Suppose we have a string containing digits from 2-9. We have to find all possible letter combinations that the number could generate. One mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does map some characters but no letters.12a b c3d e f4g h i5j k l6m n o7p q r s8t u v9w x y z*0#For an example, if the given string is “49”, then the possible strings will be ['gw', 'gx', 'gy', 'gz', 'hw', 'hx', 'hy', 'hz', 'iw', 'ix', 'iy', 'iz']To solve this, we will follow these steps:Define an array ... Read More

Check Valid N-Queens Solution in Python

Arnab Chakraborty
Updated on 26-Nov-2020 08:12:22

879 Views

Suppose we have a n x n matrix represents a chess board. There are some 1s and 0s, where 1 represents a queen and 0 represents an empty cell. We have to check whether the board is valid solution to the N-Queen puzzle or not. As we know a board is a solution of valid N-queen solution where no two queens are attacking each other.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps:n := row count of matrixrows := a new set, cols := a new set, diags := a new ... Read More

Check Whether We Can Unlock All Rooms in Python

Arnab Chakraborty
Updated on 26-Nov-2020 08:08:16

287 Views

Suppose we have a list of lists called rooms. Each index i in rooms represents a room and rooms[i] represents different keys to open other rooms. The room 0 is open and we are at that room and every other room is locked. We can move freely between opened rooms; we have to check whether we can open every room or not.So, if the input is like rooms = [[2, 0], [3], [1], []], then the output will be True, as we start from room 0 and can go to room 2 with its key 2. From room 2 we ... Read More

Find Nth Ugly Number in C++

Arnab Chakraborty
Updated on 26-Nov-2020 08:06:06

330 Views

Suppose we have a number n; we have to find the nth ugly number. As we know that the ugly numbers are those numbers, whose prime factors are only 2, 3 and 5. So if we want to find 10th ugly number, the output will be 12, as the first few ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 and so on.To solve this, we will follow these steps:Define an array v of size(n + 1)if n is same as 1, then:return 1two := 2, three := 3, five := 5twoIdx := 2, threeIdx := 2, fiveIdx := 2for initialize i := 2, when i

Find Median of Two Sorted Lists in C++

Arnab Chakraborty
Updated on 26-Nov-2020 07:56:38

211 Views

Suppose we have two sorted lists. We have to find the median of these two lists. So if the arrays are like [1,5,8] and [2,3,6,9], then the answer will be 5.To solve this, we will follow these steps:Define a function solve(), this will take an array nums1, an array nums2,if size of nums1 > size of nums2, then:return solve(nums2, nums1)x := size of nums1, y := size of nums2low := 0, high := xtotalLength := x + ywhile low

Generate Tree Using Preorder and Inorder Traversal in Python

Arnab Chakraborty
Updated on 26-Nov-2020 07:51:32

230 Views

Suppose we have the preorder and inorder traversal of a binary tree. We have to recover the tree from these sequences. So if the preorder and inorder sequences are [3, 9, 20, 15, 7] and [9, 3, 15, 20, 7], then the tree will be:Let us see the steps:Define a method called buildTree(), this will the preorder and inorder traversal sequences.if inorder traversal list is not empty, thenroot := create a tree node with value same first element from preorder sequence, and remove first item from preorder traversalroot_index := index of value of root in inorder listleft of root := ... Read More

Check Symmetry of Blocks Over X-Y Line in Python

Arnab Chakraborty
Updated on 26-Nov-2020 07:49:12

389 Views

Suppose we have a list of numbers called nums. And it is representing the height of square blocks, we have to check whether the shape is symmetric over the y = x line or not.So, if the input is like nums = [7, 5, 3, 2, 2, 1, 1], then the output will be TrueTo solve this, we will follow these steps:i := 0j := size of nums - 1while i

Count Number of Consecutive Lists Whose Sum is N in C++

Arnab Chakraborty
Updated on 26-Nov-2020 07:46:21

357 Views

Suppose we have a number n, we have to find the number of lists of positive consecutive values that sum up to n.So, if the input is like n = 15, then the output will be 4, as The possible lists are: [1, 2, 3, 4, 5], [4, 5, 6], [7, 8], and [15].To solve this, we will follow these steps:begin := 1, end := 1, x := (n + 1)sum := 0while end = n, do:if sum is same as n, then:(increase count by 1)sum := sum - begin(increase begin by 1)(increase end by 1)return count + 1Let us ... Read More

Find Unique Indices for Sum Less Than Target from Four Lists in Python

Arnab Chakraborty
Updated on 26-Nov-2020 07:43:54

178 Views

Suppose we have four list of numbers A, B, C and D and also have another number target. We have to find the number of different unique indices i, j, k, l such that A[i] + B[j] + C[k] + D[l] ≤ target.So, if the input is like A = [3, 2] B = [5, 3] C = [1] D = [2, 3] target = 9, then the output will be 3, as We can pick the following combinations: [3, 3, 1, 2] [3, 3, 1, 2] [2, 3, 1, 3]To solve this, we will follow these steps:temp_list := a ... Read More

Advertisements