See the Pen Intersecting Clickstream data by Dhiviya Dhanasekar (@dhiviyadhanasekar) on CodePen.
Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts
Monday, April 12, 2021
Monday, March 29, 2021
Two Sum
See the Pen Two-Sum by Dhiviya Dhanasekar (@dhiviyadhanasekar) on CodePen.
Monday, February 4, 2019
Median of an unsorted array
See the Pen Median from arrays by Dhiviya Dhanasekar (@dhiviyadhanasekar) on CodePen.
Monday, April 3, 2017
Jaccard Similarity
# similarity: count(intersection) / count(union)
# arr1 = [3,7,9,12,4...]
# arr2 = [5,9,10,1,2...]
#Case 1
# not sorted
# no duplicates
function computeSimilarity(arr1, arr2){
var map = {};
var i;
for(i=0; i < arr1.length; i++){
map[arr1[i]] = true;
}
var intersection = 0;
for(i=0; i < arr2.length; i++){
var val = arr2[i];
if(map[val] != null){
intersection++;
}
}
var union = arr1.length + arr2.length - intersection;
return (intersection/union);
}
# Case 2: sorted arr inputs
# O(n)
# arr1 = [3,7,9,12...]
# arr2 = [5,9,10,...]
function computeSortedSim(arr1, arr2){
var intersection = 0;
var i, j;
for(i =0, j=0; i < arr1.length && j < arr2.length; ){
if(arr1[i] < arr2[j]) i++;
else if(arr1[i] > arr2[j]) j++;
else { i++; j++; intersection++; }
}
var union = arr1.length + arr2.length - intersection;
return (intersection/union);
}
Labels:
Algo,
Arrays,
Code,
Interview,
Javascript
Tuesday, August 2, 2016
Find Duplicate Element
Given: a list of integers where, the integers in the list are in the range 1..n and the list has a length of n+1. Find the duplicate element. The complexity of should be at most O(n).
Link to Code on JSfiddle: https://jsfiddle.net/dhiviyad/zwsan7zt/1/
Link to Code on JSfiddle: https://jsfiddle.net/dhiviyad/zwsan7zt/1/
Wednesday, September 16, 2015
Max Profit
Problem
You are given an array containing prices for a share during a day. The index of the array is the time (in minutes) at which the price of the share had the value at that index. Write a function to return the maximum profit you can get. You can buy the share at any point in time and sell it at any point in time.Solution
int[] price = {-1,10,7,6,2};
public int getMaxProfit() {
int min = price[0], maxProfit =0;
int profit = 0;
for(int i=1; i< price.length; i++) {
if(price[i] < min) min = price[i];
else {
profit = price[i] - min;
if(profit > maxProfit) maxProfit = profit;
}
}
return maxProfit;
}
Subscribe to:
Posts (Atom)