Skip to content

Commit 62f60b4

Browse files
committed
Added 3 solutions
1 parent ad9e9cd commit 62f60b4

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

Hard/LRU Cache.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class LRUCache {
2+
3+
Map<Integer, Node> map;
4+
Node head, tail;
5+
int capacity;
6+
int count;
7+
8+
public LRUCache(int capacity) {
9+
map = new HashMap<>();
10+
this.capacity = capacity;
11+
12+
head = new Node();
13+
head.previous = null;
14+
15+
tail = new Node();
16+
tail.next = null;
17+
18+
head.next = tail;
19+
tail.previous = head;
20+
21+
this.count = 0;
22+
}
23+
24+
public int get(int key) {
25+
Node node = map.get(key);
26+
27+
if (node == null) {
28+
return -1;
29+
}
30+
31+
moveToHead(node);
32+
33+
return node.val;
34+
}
35+
36+
private void moveToHead(Node node) {
37+
removeNode(node);
38+
addNode(node);
39+
}
40+
41+
private void addNode(Node node) {
42+
node.previous = head;
43+
node.next = head.next;
44+
45+
head.next.previous = node;
46+
head.next = node;
47+
}
48+
49+
private Node popTail() {
50+
Node res = tail.previous;
51+
removeNode(res);
52+
53+
return res;
54+
}
55+
56+
private void removeNode(Node node) {
57+
Node pre = node.previous;
58+
Node post = node.next;
59+
60+
pre.next = post;
61+
post.previous = pre;
62+
}
63+
64+
public void put(int key, int value) {
65+
Node node = map.get(key);
66+
67+
if (node == null) {
68+
Node newNode = new Node();
69+
newNode.key = key;
70+
newNode.val = value;
71+
72+
map.put(key, newNode);
73+
addNode(newNode);
74+
75+
count++;
76+
77+
if (count > capacity) {
78+
Node tail = popTail();
79+
map.remove(tail.key);
80+
count--;
81+
}
82+
}
83+
else {
84+
node.val = value;
85+
moveToHead(node);
86+
}
87+
}
88+
89+
private class Node {
90+
Node previous;
91+
Node next;
92+
int key;
93+
int val;
94+
}
95+
}
96+

Medium/Coin Change.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int coinChange(int[] coins, int amount) {
3+
int max = amount + 1;
4+
int[] cache = new int[amount + 1];
5+
Arrays.fill(cache, max);
6+
cache[0] = 0;
7+
8+
for (int i=1; i<=amount; i++) {
9+
for (int j=0; j<coins.length; j++) {
10+
if (coins[j] <= i) {
11+
cache[i] = Math.min(cache[i], cache[i - coins[j]] + 1);
12+
}
13+
}
14+
}
15+
16+
return cache[amount] > amount ? -1 : cache[amount];
17+
}
18+
}

Medium/Container With Most Water.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
int start = 0;
4+
int end = height.length - 1;
5+
int mul = end;
6+
int res = Integer.MIN_VALUE;
7+
8+
while (end - start >= 1) {
9+
int temp = mul * Math.min(height[start], height[end]);
10+
res = Math.max(res, temp);
11+
12+
if (height[start] < height[end]) {
13+
start++;
14+
}
15+
else {
16+
end--;
17+
}
18+
19+
mul--;
20+
}
21+
22+
return res;
23+
}
24+
}

0 commit comments

Comments
 (0)