Labels
Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts
Wednesday, October 24, 2018
Wednesday, June 14, 2017
Add Strings
Problem Statement
Given two non-negative integersnum1
and num2
represented as string, return the sum of num1
and num2
.
Note:
- The length of both
num1
andnum2
is < 5100. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does not contain any leading zero. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
Link to GitHub :Code
Sunday, June 11, 2017
Minimum Moves to Equal Array Elements
Problem Statement
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
Link to GitHub : Code
Wednesday, June 7, 2017
Max consecutive ones
Problem Statement
Given a binary array, find the maximum number of consecutive 1s in this array.Link to GitHub :Code
Reshape Matrix array manipulations
Problem statement
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.
You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
Link to GitHub : CodeMissing Number
Problem Statement
Given an array containing n distinct numbers taken from0, 1, 2, ..., n
, find the one that is missing from the array.Link to GitHub code
Tuesday, June 6, 2017
Array partition
Problem Statement
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.Link to Github :Code
Find All Numbers Disappeared in an Array
Problem Statement
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
K diff pairs in an Array
Problem Statement
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.Link to GitHub : Code
Monday, June 5, 2017
Remove element
Problem Statement
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Link to GitHub : Code
Link to GitHub : Code
Remove duplicates from the sorted array
Problem Statement
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums =
Given input array nums =
[1,1,2]
,
Your function should return length =
2
, with the first two elements of nums being 1
and 2
respectively. It doesn't matter what you leave beyond the new length.Contains Duplicate II
Problem Statement
Basically we need to validate if we have near by duplicate
Link to Github :code
Plus one
Problem Statement
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
Link to GitHub :Code
Link to GitHub :Code
Example |
input = {1, 4, 9, 9}
output ={1, 5, 0, 0 } | |
Sunday, June 4, 2017
Best time to buy and sell a stock II
Problem Statement
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Solution using python
Space Complexity 0(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 | stocks=[7,1,5,2,4] def buy_sell_once(s): maxprofit=0 #first loop for i in range(len(s)-1): for j in range(i+1,len(s)): if s[j] - s[i] > maxprofit: maxprofit=s[j] - s[i] return maxprofit m=buy_sell_once(stocks) print(m) |
Space Complexity 0(1)
1 2 3 4 5 6 7 8 9 10 11 12 | stocks =[7,1,5,2,4] def buy_sell_once_ef(s): max_profit=0 min_price=stocks[0] for price in range(len(stocks)-1): min_price = min(min_price,price) profit =(price-min_price) max_profit=max(profit,max_profit) return max_profit m=buy_sell_once(stocks) print(m) |
Solution using Java
Link to GitHib : Code
Solution
- We need a variable to store the maxProfit
- We need to iterate through the array and Get the difference between selling price and buying price maxprofit+=prices[i+1]-prices[i]
- We need to make sure that selling price is greater than buying price. This check can be done using if condition if(prices[i+1] >prices[i])
public static int get(int [] prices) { int maxprofit=0; //loop through the array set . We need to keep on adding the profit //We need to buy and sell before buying others for(int i=0;i<prices.length-1;i++) { //We need to make sure that selling is greater than profit if(prices[i+1] >prices[i]) { maxprofit+=prices[i+1]-prices[i]; } } return maxprofit; }
ContainDuplicates
Problem statement
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.Link to GitHub :Code
Subscribe to:
Posts (Atom)
Labels
- Algorithms (52)
- Apache Kafka (7)
- Apache Spark (21)
- Architecture (8)
- Arrays (23)
- Big Data (98)
- Cloud services (6)
- Cognitive technologies (12)
- Data Analytics (3)
- Data Science (6)
- Design (1)
- Hadoop (26)
- Hive (11)
- Java (2)
- JavaScript (65)
- JavaScript Run-time (12)
- Machine learning (11)
- Maths (6)
- MySQL (1)
- Networking (3)
- No SQL (2)
- Node (20)
- Python (28)
- SQL (40)
- Security (4)
- Spark Grpahx (1)
- Spark MLlib (1)
- Spark Sql (3)
- Spark Streaming (4)
- Sqoop (2)
- Strings (13)
- devOps (1)
- mongoDb (2)
- ssis (3)