Skip to content

Commit c564493

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parent dc513d7 commit c564493

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## 556. Next Greater Element III
2+
3+
### Question
4+
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
5+
6+
```
7+
Example 1:
8+
Input: 12
9+
Output: 21
10+
11+
Example 2:
12+
Input: 21
13+
Output: -1
14+
```
15+
16+
### Solution
17+
* Method 1: Math
18+
1. Find the position to swap: traversal from right to left, find the first index whose value is smaller than its right index value. If no such index, return -1, which means current value is the maximum value can be represented by current numbers.
19+
2. Swap the number in that index with the first number bigger than current one, traversal from right to the left.
20+
3. Sort the values at right side of that index in ascending order.
21+
```Java
22+
class Solution {
23+
public int nextGreaterElement(int n) {
24+
String s = n + "";
25+
char[] arr = s.toCharArray();
26+
int len = s.length(), i = len - 2;
27+
for(; i >= 0; i--){
28+
if(arr[i] < arr[i + 1]) break;
29+
}
30+
if(i < 0) return -1;
31+
for(int j = len - 1; j > i; j--){
32+
if(arr[j] > arr[i]){
33+
char temp = arr[i];
34+
arr[i] = arr[j];
35+
arr[j] = temp;
36+
break;
37+
}
38+
}
39+
char[] sub = Arrays.copyOfRange(arr, i + 1, len);
40+
Arrays.sort(sub);
41+
for(int j = i + 1; j < len; j++){
42+
arr[j] = sub[j - i - 1];
43+
}
44+
long res = Long.parseLong(new String(arr));
45+
return res > Integer.MAX_VALUE ? -1: (int)res;
46+
}
47+
}
48+
```

leetcode/636. Exclusive Time of Functions.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,33 @@ Note:
6565
return result;
6666
}
6767
}
68+
```
69+
70+
### Amazon Session
71+
* Method 1: Stack
72+
```Java
73+
class Solution {
74+
public int[] exclusiveTime(int n, List<String> logs) {
75+
int[] res = new int[n];
76+
int currentTime = 0;
77+
Stack<Integer> stack = new Stack<>();
78+
for(String log: logs){
79+
String[] tokens = log.split(":");
80+
int id = Integer.parseInt(tokens[0]);
81+
int time = Integer.parseInt(tokens[2]);
82+
if(tokens[1].equals("start")){
83+
if(!stack.isEmpty()){ //current is begin, we need to update the previous id.
84+
int preId = stack.peek();
85+
res[preId] += time - currentTime - 1;
86+
}
87+
stack.push(id);
88+
}else{
89+
int preId = stack.pop();
90+
res[preId] += time - currentTime + 1;
91+
}
92+
currentTime = time;
93+
}
94+
return res;
95+
}
96+
}
6897
```

leetcode/653. Two Sum IV - Input is a BST.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,25 @@ Output: False
6161
}
6262
}
6363
}
64-
```
64+
```
65+
66+
### Amazon Session
67+
* Method 1: dfs
68+
```Java
69+
class Solution {
70+
private Set<Integer> set;
71+
private int target;
72+
public boolean findTarget(TreeNode root, int k) {
73+
if(root == null) return false;
74+
this.set = new HashSet<>();
75+
this.target = k;
76+
return dfs(root);
77+
}
78+
private boolean dfs(TreeNode node){
79+
if(node == null) return false;
80+
if(set.contains(this.target - node.val)) return true;
81+
set.add(node.val);
82+
return dfs(node.left) || dfs(node.right);
83+
}
84+
}
85+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy