Tuesday, 26 July 2016

Maximum and minimum of an array using minimum number of comparisons

Write a C function to return minimum and maximum in an array. You program should make minimum number of comparisons.  Idea : Using tournament algorithm 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include <iostream> using namespace std; struct minMaxPair{ int...
Share:

Monday, 25 July 2016

Two elements sum to x

Given an array of n numbers and another number x, determines whether or not there exist two elements in the array whose sum is exactly x. Approach: Sort the array and use two pointers one at the start and the other at the end. Increment the start pointer if the sum is less than the target, decrement if the sum is greater else break out of the loop. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 /*...
Share:

Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 /** * Return an array of arrays. * The sizes of the arrays are returned as *columnSizes array. * Note: Both returned array and...
Share:

Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Solution { public: vector<int> getRow(int rowIndex) { vector<int> result; ...
Share:

Saturday, 23 July 2016

Merge Overlapping Intervals

Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ class Solution { public: ...
Share:

Spiral Order Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Given n = 3, You should return the following matrix:  [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] #include <iostream> using namespace std; int main() { int n = 4; int **mat = new int*[n]; for(int i = 0 ; i < n; i++) mat[i] = new int[n]; ...
Share:

Count And Say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as one 1 or 11. 11 is read off as two 1s or 21. 21 is read off as one 2, then one 1 or 1211. Given an integer n, generate the nth sequence. Note: The sequence of integers will be represented as a string. Example: if n...
Share:

Friday, 22 July 2016

Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public:...
Share:

Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).” _______3______ / \ ___5__ ...
Share:

Balanced Binary Tree

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two sub trees of every node never differ by more than 1. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL),...
Share:

Tuesday, 19 July 2016

Inplace rotate square matrix by 90 degrees(Clockwise)

/*  Inplace rotate square matrix by 90 degrees */ /* Given an square matrix, turn it by 90 degrees in clockwise direction without using any extra space. Example: Input:  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 Output:  4  8 12 16  3  7 11 15  2  6 10 14  1  5  9 13  */ #include<iostream>...
Share:

Inplace rotate square matrix by 90 degrees(Anti-Clockwise)

/*  Inplace rotate square matrix by 90 degrees */ /* Given an square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space. Example: Input:  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 Output:  4  8 12 16  3  7 11 15  2  6 10 14  1  5  9 13  */ #include<iostream>...
Share:

Monday, 18 July 2016

Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom. For example, Consider the following matrix: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], ...
Share:

Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] /** * Definition for a binary tree node. * struct TreeNode { * int val; *...
Share:

Sunday, 17 July 2016

Spiral Order Matrix I Solution

Given a matrix of m * n elements (m rows, n columns), return all elements of the matrix in spiral order. Example: Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1, 2, 3, 6, 9, 8, 7, 4, 5] /** * @input A : Read only ( DON'T MODIFY ) 2D integer array ' * @input n11 : Integer array's ( A ) rows * @input n12 : Integer array's ( A...
Share:

Min Steps in Infinite Grid

You are in an infinite 2D grid where you can move in any of the 8 directions : (x,y) to (x+1, y), (x - 1, y), (x, y+1), (x, y-1), (x-1, y-1), (x+1,y+1), (x-1,y+1), (x+1,y-1) You are given a sequence of points and the order in which you need to cover the points. Give the minimum number of steps in which you can achieve it. You start from the first...
Share:

Beautiful Soup for Crawling

Beautiful Soup for web crawling import urllib2 from bs4 import BeautifulSoup import requests deptCodes = ["AE", "AG", "AR", "BT", "CH", "CM", "CE", "CS", "EE", "EC", "MG", "HS", "IM", "MM", "ME", "MT", "MI", "NA", "MP", "ED", "CR", "MS", "N2", "PK", "RE", "RT", "RD", "GS", "IT", "RJ", "RG", "ID", "MD", "BS", "EF", "ES", "NT", "WM", "SM" ] print len(deptCodes)...
Share:

Error Resolving In Linux

Resolve apache error sudo chmod 755 html -R   Add ppa in ubuntu14.04 export http_proxy, https_proxy, ftp_proxy sudo -E add-apt-repository ppa:<repository-name> Path variable export PATH=$PATH:<path_to_bin> ...
Share:

Lex Yacc Parser For HTML Processing

Lex Yacc Parser for html web pages LEX File %{ #include<stdio.h> #include<math.h> #include<string.h> #include "cali.tab.h" int yywrap(void); #include<stdlib.h> %} %% \<fieldset[^\>]*\>\<legend\>(Award|Fellow|Patent|Member.?.?Editorial.?Board|Copyrights|Text) { strcpy(yylval.cval, yytext); return UNWANTED; ...
Share:

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive