Found 26504 Articles for Server Side Programming

Task Scheduler n C++

Arnab Chakraborty
Updated on 30-Apr-2020 08:47:54

4K+ Views

Suppose we have a char array representing tasks CPU need to do. This contains uppercase letters A to Z where different letters represent different tasks. The tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one job or just be idle. However, there is a non-negative cooling interval called n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle. We have to find the least number of intervals the CPU will take to finish ... Read More

Find All Anagrams in a String in C++

Arnab Chakraborty
Updated on 29-Apr-2020 13:07:56

2K+ Views

Suppose we have a string s and a non-empty string p, we have to find all the start indices of p's anagrams in s. The strings consist of lowercase letters only and the length of both strings s and p will not be larger than 20 and 100. So for example, if s: "cbaebabacd" p: "abc", then the output will be [0, 6], at index 0, it is “cba”, and another is “bac”, these are the anagrams of “abc”.To solve this, we will follow these steps −Define a map m, n := size of s, set left := 0, right ... Read More

Insert Delete GetRandom O(1) in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:34:50

564 Views

Suppose we have a data structure that supports all following operations in average O(1) time.insert(val) − This will Insert an item val to the set if that is not already present.remove(val) − This will remove an item val from the set if it presents.getRandom − This will return a random element from current set of elements. Each element must have the same probability of being returned.To solve this, we will follow these steps −For the initialization, define a parent map, and elements arrayFor the insert() function, it will take val as inputif val is not present in parent map, or ... Read More

Flatten Nested List Iterator in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:33:50

442 Views

Suppose we have a nested list of integers; we have to implement an iterator to flatten it. Each element is either an integer, or a list. The elements of that list may also be integers or other lists. So if the input is like [[1, 1], 2, [1, 1]], then the output will be [1, 1, 2, 1, 1]To solve this, we will follow these steps −In the initializing section, it will take the nested list, this will work as follows −set res as empty list, index := 0, call getVal(nestedList)The getVal() will take nestedIntegers, this will work as −for ... Read More

House Robber III in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:32:55

438 Views

Suppose one thief has found himself a new place for his thievery again. There is only one entrance to this area, the entrance is called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief felt that "all houses in this place forms a binary tree". And It will automatically contact the police if two directly-linked houses were broken into on the same night. We have to find the maximum amount of money the thief can rob tonight without alerting the police. So if the tree is like −Then the ... Read More

Super Ugly Number in C++

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

338 Views

We have to create one function to find the nth super ugly number. The super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. So if the n is 12 and primes are [2, 7, 13, 19], then the output will be 32, this is because [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of 12 super ugly numbers.To solve this, we will follow these steps −Create a data structure triplet, with num, prime and idxif n is 1, then return 1, create ... Read More

Course Schedule II in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:26:29

351 Views

Suppose there are a total of n courses, these are labeled from 0 to n-1. Some courses may have prerequisites, given the total number of courses and a list of prerequisite pairs, we have to find the ordering of courses that we should take to finish all courses. There may be multiple correct orders, we just need to find one of them. If it is impossible to finish all courses, then return an empty array.So if the input is like 2, [[1, 0]], then the result will be [0, 1]. There are a total of 2 courses to take. To ... Read More

Course Schedule in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:25:39

1K+ Views

Suppose there are a total of numCourses courses we have to take, labeled from 0 to numCourses-1. Some courses may have prerequisites, for example to take course 0 we have to first take course 1, which is expressed using a pair: [0, 1]. Suppose there are total number of courses that is provided and a list of prerequisite pairs, we have to check whether is it possible for you to finish all courses?So if the input is like − numCourses = 2 and prerequisites = [[1, 0]], then the result will be true, because there are a total of 2 ... Read More

Word Ladder in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:23:44

870 Views

Suppose we have two words (beginWord and endWord), and we have dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that −Only one letter can be converted at a time.In each transformed word must exist in the word list. The beginWord is not a transformed word.We have to keep in mind that −Return 0 when there is no such change sequence.All words have the same length.All words contain only lowercase characters.We can assume no duplicates in the word list.So if the input is like: beginWord = "hit", endWord = "cog", and wordlist = ["hot", ... Read More

Coroutines in C/C++

Ayush Gupta
Updated on 16-Mar-2020 10:14:42

260 Views

In this tutorial, we will be discussing a program to understand coroutines in C/C++.Coroutines are control instructions which switch the execution control between two routines which returning any of them.Example Live Demo#include int range(int a, int b){    static long long int i;    static int state = 0;    switch (state){    case 0:       state = 1;       for (i = a; i < b; i++){          return i;       //returning control       case 1:; //resuming control       }    }    state = 0;    return 0; } int main(){    int i;    for (; i=range(1, 5);)       printf("control at main :%d", i);    return 0; }Outputcontrol at main :1 control at main :2 control at main :3 control at main :4

Advertisements