Skip to content

Commit a62c4d2

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parent 438c7f1 commit a62c4d2

3 files changed

+98
-1
lines changed

leetcode/116. Populating Next Right Pointers in Each Node.md

+14
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,18 @@ public class Solution {
146146
return root;
147147
}
148148
}
149+
```
150+
151+
* Method 2: Recursion
152+
```Java
153+
class Solution {
154+
public Node connect(Node root) {
155+
if(root == null) return root;
156+
if(root.left != null) root.left.next = root.right;
157+
if(root.right != null) root.right.next = root.next == null ? null: root.next.left;
158+
connect(root.left);
159+
connect(root.right);
160+
return root;
161+
}
162+
}
149163
```

leetcode/25. Reverse Nodes in k-Group.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,40 @@ class Solution {
112112
return head;
113113
}
114114
}
115-
```
115+
```
116+
117+
### Amazon Session
118+
* Method 1: Recursion
119+
```Java
120+
/**
121+
* Definition for singly-linked list.
122+
* public class ListNode {
123+
* int val;
124+
* ListNode next;
125+
* ListNode(int x) { val = x; }
126+
* }
127+
*/
128+
class Solution {
129+
public ListNode reverseKGroup(ListNode node, int k){
130+
if(node == null) return null;
131+
ListNode dummy = new ListNode(0);
132+
dummy.next = node;
133+
ListNode temp = dummy;
134+
for(int i = 0; i < k; i++){
135+
if(temp.next == null) return node;
136+
temp = temp.next;
137+
}
138+
int count = 0;
139+
ListNode pre = null, cur = node, next = null;
140+
while(count++ < k && cur != null){
141+
next = cur.next;
142+
cur.next = pre;
143+
pre = cur;
144+
cur = next;
145+
}
146+
if(cur == null) return pre;
147+
node.next = reverseKGroup(cur, k);
148+
return pre;
149+
}
150+
}
151+
```

leetcode/716. Max Stack.md

+47
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,50 @@ Note:
8181
* int param_5 = obj.popMax();
8282
*/
8383
```
84+
85+
### Amazon session
86+
* Method 1: LinkedList + PriorityQueue
87+
```Java
88+
class MaxStack {
89+
private PriorityQueue<Integer> pq;
90+
private LinkedList<Integer> list;
91+
/** initialize your data structure here. */
92+
public MaxStack() {
93+
this.pq = new PriorityQueue<>((a, b)->{
94+
return b - a;
95+
});
96+
this.list = new LinkedList<>();
97+
}
98+
public void push(int x) {
99+
this.list.addFirst(x); // O(1)
100+
this.pq.offer(x); //O(NlogN)
101+
}
102+
public int pop() {
103+
Integer res = list.pollFirst();
104+
this.pq.remove(res); // O(NlogN)
105+
return res;
106+
}
107+
public int top() {
108+
return list.get(0);
109+
}
110+
public int peekMax() {
111+
return this.pq.peek();
112+
}
113+
114+
public int popMax() {
115+
Integer res = pq.poll(); // O(1)
116+
this.list.remove(res); // O(n)
117+
return res;
118+
}
119+
}
120+
121+
/**
122+
* Your MaxStack object will be instantiated and called as such:
123+
* MaxStack obj = new MaxStack();
124+
* obj.push(x);
125+
* int param_2 = obj.pop();
126+
* int param_3 = obj.top();
127+
* int param_4 = obj.peekMax();
128+
* int param_5 = obj.popMax();
129+
*/
130+
```

0 commit comments

Comments
 (0)