Find Next State of Cell Matrix in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:54:17

184 Views

Suppose we have a 2D binary matrix where a 1 means a live cell and a 0 means a dead cell. A cell's neighbors are its immediate horizontal, vertical and diagonal cells. We have to find the next state of the matrix using these rulesAny living cell with two or three living neighbors lives.Any dead cell with three living neighbors becomes a live cell.All other cells die.So, if the input is like1100010001011101then the output will be1100010001001100To solve this, we will follow these steps:n := row size of matrix, m := column size of matrixres := a matrix of size n ... Read More

Remove Nodes with Only One Child from a Binary Tree in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:51:15

700 Views

Suppose we have a binary tree root; we have to remove all nodes with only one child.So, if the input is likethen the output will beTo solve this, we will follow these steps:Define a method called solve(), this will take tree rootif root is null, thenreturn rootif left of root is null and right of root is null, thenreturn rootif left of root is null, thenreturn solve(right of root)if right of root is null, thenreturn solve(left of root)left of root := solve(left of root)right of root := solve(right of root)return rootExampleclass TreeNode:    def __init__(self, data, left = None, right ... Read More

Find Length of Longest Possible Stick in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:47:08

280 Views

Suppose we have a list of integers sticks. Here each element in the list represents a stick with two ends, these values are between 1 and 6. These are representing each end. We can connect two sticks together if any of their ends are same. The resulting stick's ends will be the leftover ends and its length is increased. We have to find the length of the longest stick possible.So, if the input is like sticks = [ [2, 3], [2, 4], [3, 5], [6, 6] ], then the output will be 3, as we can connect [2, 3] and ... Read More

Find Matrix for Maximum Height Increase of Condominium in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:44:10

267 Views

Suppose we have a 2D matrix, where matrix [r, c] represents the height of a condominium in a city. The west-east skyline is visible by taking the maximum of each row in the matrix. And the north-south skyline can be visible by taking the maximum of each column. We have to find a new matrix where each condominium's height is increased to the maximum possible height while keeping the same west-east and north-south skyline.So, if the input is like23456789104447778910as the west-east skyline is [4, 7, 10] and north-south skyline is [8, 9, 10]. We can increase everything in the first ... Read More

Sum of Concatenated Pairs of Elements in a List in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:41:57

572 Views

Suppose we have a list of numbers called nums. We have to find the sum of every concatenation of every pair of numbers in nums. Here the pair (i, j) and pair (j, i) are considered different.So, if the input is like nums = [5, 3], then the output will be 176, as We have the following concatenations: (nums[0] + nums[0]) = (5 concat 5) = 55, (nums[0] + nums[1]) = (5 concat 3) = 53, (nums[1] + nums[0]) = (3 concat 5) = 35, (nums[0] + nums[0]) = (3 concat 3) = 33, then the sum is 55 + ... Read More

Length of Concatenated String of Unique Characters in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:36:22

262 Views

Suppose we have a list of strings words. We have to make a string that is constructed by concatenating a subsequence of words such that each letter is unique. We have to finally find the length of the longest such concatenation.So, if the input is like words = ["xyz", "xyw", "wab", "cde"], then the output will be 9, as we cannot pick any word since they contain duplicate characters.To solve this, we will follow these stepsans := 0Define a function recur() . This will take i:= 0, cur:= blank stringif i is same as size of words , then   ... Read More

Find Minimum Number of Groups in Communication Towers in C++

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

211 Views

Suppose we have a 2D binary matrix where 1 represents a communication tower, and 0 represents an empty cell. The towers can communicate in the following ways: 1. If tower A, and tower B are either on the same row or column, they can communicate with each other. 2. If tower A can talk with tower B and B can talk with C, then A can talk to C (transitive property). We have to find the total number of tower groups there are (here a group is a list of towers that can talk with each other).So, if the input ... Read More

Find Angle Between Hour and Minute Hands of a Clock in C++

Arnab Chakraborty
Updated on 10-Nov-2020 08:26:28

2K+ Views

Suppose we have two values hours and minutes. We have to find a smaller angle formed between the hour and the minute hand.So, if the input is like hour = 12 minutes = 45, then the output will be 112.5To solve this, we will follow these steps:if h = 12, then set h := 0if m = 60, then set m := 0hAngle := 0.5 * (60h) + mmAngle := 6mret := |hAngle - mAngle|return minimum of ret and (360 – ret)Let us see the following implementation to get better understanding:Example Live Demo#include using namespace std; class Solution { public: ... Read More

Maximum Number of Courses Based on Interval Time in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:24:08

264 Views

Suppose we have a list of intervals in the form [start, end], this is representing the start and end time of a course. We have to find the maximum number of courses we can take, assuming we can only take one course at a time and the start of a course needs to be later than the end of the last course.So, if the input is like times = [[3, 6], [6, 9], [7, 8], [9, 11]], then the output will be 3, as we can take courses [[3, 6], [7, 8], [9, 11]]To solve this, we will follow these ... Read More

Find Number of Boxes that Form Longest Chain in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:21:39

228 Views

Suppose we have a list of boxes, here each entry has two values [start, end] (start < end). We can join two boxes if the end of one is equal to the start of another. We have to find the length of the longest chain of boxes.So, if the input is like blocks = [ [4, 5], [5, 6], [4, 8], [1, 2], [2, 4] ], then the output will be 4, as we can form the chain: [1, 2], [2, 4], [4, 5], [5, 6]To solve this, we will follow these steps:if boxes are empty, thenreturn 0sort the list ... Read More

Advertisements