Programming Articles - Page 1978 of 3366

Toss Strange Coins in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:05:59

677 Views

Suppose we have some coins. The i-th coin has a probability prob[i] of facing heads when tossed. We have to show the probability that the number of coins facing heads equals target if you toss every coin exactly once. So if the prob array is like [0.5, 0.5, 0.5, 0.5, 0.5] and target is 0, then the output will be 0.03125.To solve this, we will follow these steps −n := size of prob arraycreate one 2d array of size n x (target + 5)set dp[0, 0] = 1 – prob[0] and dp[0, 1] := prob[0]for i in range 1 to ... Read More

Meeting Scheduler in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:03:11

1K+ Views

Suppose we have the availability time slots lists slots1 and slots2 of two people and a meeting duration d, we have to find the earliest time slot that works for both of them and is of duration d. If there is no common time slot that satisfies the requirements, then show an empty array. Here the format of a time slot is an array of two elements [start, end] representing an inclusive time range from start to end. we can assume that no two availability slots of the same person intersect with each other. That is, for any two time ... Read More

Dice Roll Simulation in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:58:42

688 Views

Suppose a die simulator generates a random number from 1 to 6 for each roll. We want to introduced a constraint to the generator such that it cannot roll the number i more than rollMax[i] (1-indexed) consecutive times. Consider we have an array of integers rollMax and an integer n, we have to return the number of distinct sequences that can be obtained with exact n rolls. The two sequences are considered different if at least one element differs from each other. So if n is 2, then rollMax = [1, 1, 2, 2, 2, 3], then the output will ... Read More

Path with Maximum Gold in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:55:21

540 Views

Suppose in a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 means that is empty. We have to find the maximum amount of gold that you can collect under the conditions −Every time we are pointing a cell we will collect all the gold in that cell.From our position we can walk one step to the left, right, up or down.We cannot visit the same cell more than once.Never visit a cell with 0 gold.So if the input is like [[0, 6, 0], ... Read More

Stepping Numbers in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:50:13

373 Views

Suppose we have two integers low and high, we have to find and show a sorted list of all the Stepping Numbers in the range [low, high] inclusive. A Stepping Number is an integer means all of its adjacent digits have an absolute difference of exactly 1. For example, 321 is a Stepping Number but 421 is not. So if the input is like low := 0 and high := 21, then the result will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21]To solve this, we will follow these steps −create one array tempmake ... Read More

Remove All Adjacent Duplicates in String II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:47:11

709 Views

Suppose a string s is given, a k duplicate removal consists of choosing k adjacent and equal letters from string s and removing them causing the left and the right side of the deleted substring to concatenate together. We will repeatedly make k duplicate removals on the given string s until we cannot change any remaining. We have to find the final string after all such duplicate removals have been made. So if the input is like s = “deeedbbcccbdaa”, and k = 3, then the output will be “aa”, at first delete the “eee” and “ccc” and we will ... Read More

Stone Game II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:44:15

462 Views

Suppose there are two persons Alice and Bob, they are continuing their games with piles of stones. There are a number of piles placed in a row, and each pile has a positive integer number of stones in an array piles[i]. Our objective of the game is to end with the most stones. Alice and Bob take the turns, with Alice starting first. Initially, M = 1. On each player's turn, that player can take all the stones in the first X remaining piles, here 1

Longest Well-Performing Interval in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:40:28

259 Views

Suppose we have hours list, this is a list of the number of hours worked per day for a given employee. Here a day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. One well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. We have to find the length of the longest well-performing interval. So if the input is like [9, 9, 6, 0, 6, 6, 9], so then then the output will be ... Read More

Corporate Flight Bookings in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:38:07

216 Views

Suppose we have n flights, and they are labeled from 1 to n. We have a list of flight bookings. The i-th booking indicates using bookings[i] = [i, j, k] this means that we booked k seats from flights labeled i to j inclusive. Find an array answer of length n, showing the number of seats booked on each flight in order of their label. So if the input is like [[1, 2, 10], [2, 3, 20], [2, 5, 25]] and n = 5, then the output will be [10, 55, 45, 25, 25].To solve this, we will follow these ... Read More

Car Pooling in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:35:10

852 Views

Suppose there is a vehicle that has capacity empty seats initially available for passengers. The vehicle only drives east, so we cannot turn around and drive west. We have given a list of trips, trip[i] = [num_passengers, start_location, end_location], that contains information about the ith trip:, so that is the number of passengers that must be picked up, and the locations to pick them up and drop them off. Here the locations are given as the number of kilometers due east from our vehicle's initial location. Our module will return true if and only if it is possible to pick ... Read More

Advertisements