Skip to content

Commit

Permalink
Add java LRU Cache, Min stack, Happy number, Palindrone linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
miladra committed Jun 23, 2022
1 parent b817e34 commit cbfb739
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
91 changes: 91 additions & 0 deletions java/146-LRU-Cache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
class LRUCache {

private Map<Integer,Node> cache;
private int capacity;

private Node left;
private Node right;

public LRUCache(int capacity) {
this.capacity = capacity;
cache = new HashMap<>();

//left = LRU , right = most recent
this.left = new Node(0,0);
this.right = new Node(0,0);
this.left.next = this.right;
this.right.prev = this.left;

}

public int get(int key) {

if(cache.containsKey(key)){
remove(cache.get(key));
insert(cache.get(key));
return cache.get(key).val;

} else{
return -1;
}

}

public void put(int key, int value) {

if(cache.containsKey(key)){
remove(cache.get(key));
}
cache.put(key , new Node(key , value));
insert(cache.get(key));

if(cache.size() > capacity){
// remove from the list and delte the LRU from the hashmap
Node lru = this.left.next;
remove(lru);
cache.remove(lru.key);

}


}

// remove node from list
public void remove(Node node) {

Node prev = node.prev;
Node next = node.next;

prev.next = next;
next.prev = prev;
}

// insert node at right
public void insert(Node node) {

Node prev = this.right.prev;
Node next = this.right;


prev.next = node;
next.prev = node;

node.next =next;
node.prev =prev;



}
private class Node{
private int key;
private int val;

Node next;
Node prev;

public Node(int key , int val){
this.key = key;
this.val = val;
}
}
}
44 changes: 44 additions & 0 deletions java/155-Min-Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class MinStack {

private Stack<Integer> stack;
private Stack<Integer> minStack;

public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();

}

public void push(int val) {
stack.push(val);

// The min stack may be empty, so we need to check it
val = Math.min(val, minStack.isEmpty() ? val : minStack.peek());
minStack.push(val);

}

public void pop() {
stack.pop();
minStack.pop();

}

public int top() {
return stack.peek();
}

public int getMin() {
return minStack.peek();

}
}

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
41 changes: 41 additions & 0 deletions java/202-Happy-Number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public boolean isHappy(int n) {

if (n == 1 || n == -1) {
return true;
}

Set<Integer> visit = new HashSet<Integer>();

// compute square until getting duplicate value
while (!visit.contains(n)) {
visit.add(n);
// using helper function to compute the sum of squares
n = sumOfSquare(n);

if (n == 1)
return true;

}

return false;

}

public int sumOfSquare(int n) {

int output = 0;

while (n != 0) {
int digit = n % 10;
digit = digit * digit;
output += digit;
n = n / 10;

}

return output;

}

}
43 changes: 43 additions & 0 deletions java/234-Palindrome-Linked-List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
public boolean isPalindrome(ListNode head) {

ListNode fast = head;
ListNode slow = head;

while (fast != null && fast.next != null) {

// fast pointer shifted twice
fast = fast.next;
fast = fast.next;

// slow pointer shifted only once
slow = slow.next;

}

// reverse second half
ListNode prev = null;
while (slow != null) {
ListNode tmp = slow.next;
slow.next = prev;
prev = slow;
slow = tmp;
}

ListNode left = head;
ListNode right = prev;

while (right != null) {
if (left.val != right.val) {
return false;
}

left = left.next;
right = right.next;

}

return true;

}
}

0 comments on commit cbfb739

Please sign in to comment.