Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1_2 Two Sum

public int[] unsortedTwoSum(int[] nums, int target) {


int[] res = new int[2];
Map<Integer, Integer> hm = new HashMap<>();

for(int i = 0; i < nums.length; i++) {


if(hm.containsKey(nums[i])) {
res[0] = hm.get(nums[i]);
res[1] = i;
return res;
}
else {
hm.put(target-nums[i], i);
}
}
return res;
}

public int[] sortedTwoSum(int[] nums, int target) {


int[] res = new int[2];
int left = 0; int right = nums.length-1;

while(left <= right) {


int currSum = nums[left] + nums[right];
if(currSum == target) {
res[0] = left+1;
res[1] = right+1;
return res;
}
else if(currSum < target) {
left++;
}
else {
right--;
}
}
return res;
}
21_22 Intersection of Two Arrays
// Sorting and comparing values 2 ptrs (Time O(nlogn) Space O(n))

// Using HashSet (No handling duplicates - (Time O(n) Space O(n))


public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> hs = new HashSet();
Set<Integer> res = new HashSet();

for(int i: nums1) {
if(!hs.contains(i)) {
hs.add(i);
}
}
for(int i: nums2) {
if(hs.contains(i)) {
res.add(i);
}
}
return res.stream().mapToInt(i->i).toArray();
}

// 22. Using Map – handling duplicates - faster lookup (Time O(n) Space O(n))
public int[] intersect_22(int[] nums1, int[] nums2) {
// populate map with num1 array // traverse another num2 array
// decrement mapValue if num exists
// traverse map - put values in resList

Map<Integer, Integer> hm = new HashMap<>(); // map of num - count


List<Integer> resList = new ArrayList<>();

for(int n: nums1) {
if(hm.containsKey(n)) {
hm.put(n, hm.get(n) + 1);
}
else {
hm.put(n,1);
}
}

for(int n: nums2) {
if(hm.containsKey(n)) {
resList.add(n);
hm.put(n, hm.get(n) - 1);
if(hm.get(n) == 0) {
hm.remove(n);
}
}
}
return resList.stream().mapToInt(i->i).toArray();
}
42 Insert Delete GetRandom O(1)
public class _42_InsertDeleteGetRandom {

Map<Integer, Integer> hm;


List<Integer> alist;
Random rand = new Random();

/** Initialize your data structure here. */


public _42_InsertDeleteGetRandom() {
hm = new HashMap<Integer, Integer>();
alist = new ArrayList<Integer>();
}

/** Inserts a value to the set. Returns true if the set did not already
contain the specified element. */
public boolean insert(int val) {
if(hm.containsKey(val)) {
return false;
}

hm.put(val, alist.size()); // val with list size.. easy to get val


alist.add(alist.size(), val); // add at end of list given value
return true;
}

/** Removes a value from the set. Returns true if the set contained the
specified element. */
public boolean remove(int val) {
if(!hm.containsKey(val)) {
return false;
}

int eleIndex = hm.get(val);


int lastElement = alist.get(alist.size() - 1);

alist.set(eleIndex, lastElement); // Put last elelment at ele's index


hm.put(lastElement, eleIndex ); // update last ele value in hm
// delete last ele..
// so given ele replaces with lastEle and lastEle gets deleted
alist.remove(alist.size()-1);
hm.remove(val);
return true;
}

/** Get a random element from the set. */


public int getRandom() {
return alist.get(rand.nextInt(alist.size()));
}
}

You might also like