Skip to content

Commit ad9e9cd

Browse files
committed
Added 5 solutions
1 parent f336742 commit ad9e9cd

5 files changed

+233
-0
lines changed

Easy/Max Stack.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class MaxStack {
2+
3+
/** initialize your data structure here. */
4+
PriorityQueue<Integer> pr;
5+
List<Integer> list;
6+
public MaxStack() {
7+
pr = new PriorityQueue<>((a,b) -> b-a);
8+
list = new ArrayList<>();
9+
}
10+
11+
public void push(int x) {
12+
pr.add(x);
13+
list.add(x);
14+
}
15+
16+
public int pop() {
17+
int res = list.get(list.size()-1);
18+
list.remove(list.size()-1);
19+
pr.remove(res);
20+
return res;
21+
}
22+
23+
public int top() {
24+
return list.get(list.size()-1);
25+
}
26+
27+
public int peekMax() {
28+
return pr.peek();
29+
}
30+
31+
public int popMax() {
32+
int res = pr.poll();
33+
int i = list.size()-1;
34+
35+
while (i >= 0) {
36+
if (list.get(i) == res) {
37+
break;
38+
}
39+
40+
i--;
41+
}
42+
43+
list.remove(i);
44+
45+
return res;
46+
}
47+
}
48+
49+
/**
50+
* Your MaxStack object will be instantiated and called as such:
51+
* MaxStack obj = new MaxStack();
52+
* obj.push(x);
53+
* int param_2 = obj.pop();
54+
* int param_3 = obj.top();
55+
* int param_4 = obj.peekMax();
56+
* int param_5 = obj.popMax();
57+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int longestConsecutive(TreeNode root) {
12+
if (root == null) {
13+
return 0;
14+
}
15+
16+
int[] ans = {1};
17+
helper(root, ans, null, 1);
18+
19+
return ans[0];
20+
}
21+
22+
private void helper(TreeNode root, int[] ans, TreeNode parent, int count) {
23+
if (root == null) {
24+
return;
25+
}
26+
27+
if (parent != null) {
28+
if (root.val == parent.val + 1) {
29+
count++;
30+
ans[0] = Math.max(ans[0], count);
31+
}
32+
else {
33+
count = 1;
34+
}
35+
}
36+
37+
helper(root.left, ans, root, count);
38+
helper(root.right, ans, root, count);
39+
}
40+
}

Medium/Coin Change 2.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public static int change(int amount, int[] coins) {
3+
int[] combinations = new int[amount+1];
4+
combinations[0] = 1;
5+
6+
for (int coin : coins) {
7+
for (int i=coin; i<amount+1; i++) {
8+
combinations[i] += combinations[i-coin];
9+
}
10+
}
11+
12+
return combinations[amount];
13+
}
14+
}

Medium/Design Phone Directory.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class PhoneDirectory {
2+
3+
/** Initialize your data structure here
4+
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
5+
int[] directory;
6+
int pos;
7+
public PhoneDirectory(int maxNumbers) {
8+
directory = new int[maxNumbers];
9+
for (int i=0; i<maxNumbers; i++) {
10+
directory[i] = (i+1)%maxNumbers;
11+
}
12+
pos = 0;
13+
}
14+
15+
/** Provide a number which is not assigned to anyone.
16+
@return - Return an available number. Return -1 if none is available. */
17+
public int get() {
18+
if (directory[pos] == -1) {
19+
return -1;
20+
}
21+
22+
int val = pos;
23+
pos = directory[pos];
24+
directory[val] = -1;
25+
return val;
26+
}
27+
28+
/** Check if a number is available or not. */
29+
public boolean check(int number) {
30+
return directory[number] != -1;
31+
}
32+
33+
/** Recycle or release a number. */
34+
public void release(int number) {
35+
if (directory[number] != -1) {
36+
return;
37+
}
38+
directory[number] = pos;
39+
pos = number;
40+
}
41+
}
42+
43+
/**
44+
* Your PhoneDirectory object will be instantiated and called as such:
45+
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
46+
* int param_1 = obj.get();
47+
* boolean param_2 = obj.check(number);
48+
* obj.release(number);
49+
*/
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Trie {
2+
3+
Node trie;
4+
/** Initialize your data structure here. */
5+
public Trie() {
6+
trie = new Node("");
7+
}
8+
9+
/** Inserts a word into the trie. */
10+
public void insert(String word) {
11+
Node curr = trie;
12+
for(int i=0; i<word.length(); i++) {
13+
if (!curr.childrens.containsKey(word.charAt(i))) {
14+
curr.childrens.put(word.charAt(i), new Node(word.substring(0, i+1)));
15+
}
16+
17+
curr = curr.childrens.get(word.charAt(i));
18+
19+
if (i == word.length() - 1) {
20+
curr.isWord = true;
21+
}
22+
}
23+
}
24+
25+
/** Returns if the word is in the trie. */
26+
public boolean search(String word) {
27+
Node curr = trie;
28+
for (int i=0; i<word.length(); i++) {
29+
if (curr.childrens.containsKey(word.charAt(i))) {
30+
curr = curr.childrens.get(word.charAt(i));
31+
}
32+
else {
33+
return false;
34+
}
35+
}
36+
37+
return curr.isWord;
38+
}
39+
40+
/** Returns if there is any word in the trie that starts with the given prefix. */
41+
public boolean startsWith(String prefix) {
42+
Node curr = trie;
43+
44+
for (int i=0; i<prefix.length(); i++) {
45+
if (curr.childrens.containsKey(prefix.charAt(i))) {
46+
curr = curr.childrens.get(prefix.charAt(i));
47+
}
48+
else {
49+
return false;
50+
}
51+
}
52+
53+
return true;
54+
}
55+
56+
private static final class Node {
57+
String prefix;
58+
Map<Character, Node> childrens;
59+
boolean isWord;
60+
61+
public Node(String prefix) {
62+
this.prefix = prefix;
63+
this.childrens = new HashMap<>();
64+
}
65+
}
66+
}
67+
/**
68+
* Your Trie object will be instantiated and called as such:
69+
* Trie obj = new Trie();
70+
* obj.insert(word);
71+
* boolean param_2 = obj.search(word);
72+
* boolean param_3 = obj.startsWith(prefix);
73+
*/

0 commit comments

Comments
 (0)