Skip to main content

Posts

Showing posts from August, 2017

Non-decreasing Array - LeetCode

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n). Example 1: Input: [4,2,3] Output: True Explanation: You could modify the first 4 to 1 to get a non-decreasing array.   Example 2: Input: [4,2,1] Output: False Explanation: You can't get a non-decreasing array by modify at most one element. Note: The n belongs to [1, 10,000]. Source: LeetCode At first glance, it looks like an easy and straightforward problem, but believe me it's greedy problem. In the problem, you are allowed to make atmost "one" modification to make a non-decreasing array. Nondecreasing means that every element is greater than or equal to the one before it e.g. [1,3,4,4,5,6,9]. For example, [2,2,3,2,4] can this become non-decreasing? Yes, by replacing 3 with 2 =>[2,2,2,2,4]. What about this:

C++ Program to Print Pascal's Triangle

Pascal's Triangle # include < iostream >     using namespace std ; unsigned int factorial ( unsigned int const num ) { if ( num < = 1 ) return 1 ; else return num * factorial ( num - 1 ) ; } int main ( void ) { int row = 0 ; cout < < " Enter the number of rows: " ; cin > > row ; //Method 1 for ( unsigned int n = 0 ; n < row ; + + n ) { //spacing before row depends on how many rows there are //row-i = number of space needed before row starts for ( unsigned int j = 1 ; j < ( row - n ) ; + + j ) { cout < < " " ; } unsigned int n_factorial = factorial ( n ) ; //printing the binomial coefficient using combination formula //Number of binomial coefficient on each line // is equivalent to the current row number (i.e., the counter i) //n choose k or nCk = n!/(k!(n-k)!) for ( unsigned int k

Python while and for Loop Statments

Loops are indeed very powerful as it allows us to do a repetitive task that is to execute statement or group of statements multiple times. Python provides following types of loops to handle repetitive tasks: for and while loops. The semantics of the while loop: while condition: body containing (statment / s) The semantics of the for loop: for item in list : statement / s Example 1 (for): #Program to print each character in a string (which is a sequence of characters) string = "Python" #for each charachter in a string do this: print(char,end = '') for char in string: print (char,end = '' ) #Output:Python Example 2 (for): #Program to print the first 5 positive even numbers #Note:loops runs from 0-9 increment by 1 for number in range ( 0 , 10 ): #even if evenly divisible by 2 if ( number % 2 == 0 ): print (number, ' is even' ) #Alternative way, the range function also takes "

Python Common String Operations

A string is most popular type in Python. A string can be created by enclosing a sequence of characters in a single or double quotes (' ' or " "). A string module contains a number of powerful methods/function to perform operations on a string. NOTE: string in Python is IMMUTABLE (it cannot be changed of the string) as in many other languages (Java). Length of a string Range slice Accessing a character of a string Converting all string characters to all uppercase letters Converting all string characters to all lowercase letters Capitalizing string String concatenation Fining the index where the sub-string begins/occurs in a string  Check if string contains sub-string String comparison (Equality, less than, greater than, not equal) Breaking a string by ",", "-","  " (space), etc.                                                       #Let's start by declaring a string #a string object is created and assig

Reverse a Singly Linked List

Recursive Solution: class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* newHead =NULL; if( head ) reverse (head,newHead); return newHead; } void reverse (ListNode* head, ListNode*& newHead){ if( head->next == NULL ){ newHead = head; }else{ reverse(head->next,newHead); // equivalent to head->next->next = head->next; ListNode* tmp = head->next; tmp->next = head; // point next of the node to previous node head->next = NULL; } } };