Skip to content

Commit 13af7bf

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parent df53f9c commit 13af7bf

6 files changed

+142
-87
lines changed

leetcode/121. Best Time to Buy and Sell Stock.md

+19
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,22 @@ class Solution {
139139
}
140140
}
141141
```
142+
143+
### Fifth Time
144+
* Method 1:
145+
1. Find previous min price(means we buy at that day).
146+
2. Update current max profit.
147+
3. Update min buy time.
148+
```Java
149+
class Solution {
150+
public int maxProfit(int[] prices) {
151+
if(prices == null || prices.length == 0) return 0;
152+
int profit = 0, min = prices[0];
153+
for(int i = 1; i < prices.length; i++){
154+
profit = Math.max(profit, prices[i] - min);
155+
min = Math.min(min, prices[i]);
156+
}
157+
return profit;
158+
}
159+
}
160+
```

leetcode/17. Letter Combinations of a Phone Number.md

+25
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,28 @@ class Solution {
9191
}
9292
}
9393
```
94+
95+
### Fourth Time
96+
* Method 1: dfs
97+
```Java
98+
class Solution {
99+
private static final String[] vals = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
100+
private List<String> result;
101+
public List<String> letterCombinations(String digits) {
102+
this.result = new ArrayList<>();
103+
if(digits.length() == 0) return this.result;
104+
dfs(digits, 0, "");
105+
return result;
106+
}
107+
private void dfs(String digits, int cur, String temp){
108+
if(cur == digits.length()){
109+
result.add(temp);
110+
}else{
111+
int num = digits.charAt(cur) - '0';
112+
for(char c : vals[num].toCharArray()){
113+
dfs(digits, cur + 1, temp + c);
114+
}
115+
}
116+
}
117+
}
118+
```

leetcode/253. Meeting Rooms II.md

+63-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,67 @@ return 2.
99

1010
### Thinking:
1111
* Method 1:
12+
1. Sort the intervals according to the starting time.
13+
2. Use a minHeap(pq) to save the endTime for all intervals according to the order of start time.
14+
* Add end time to the pq.
15+
* if cur start time < pq.peek() => means current start time is before first ending time, which means we must have a new room.
16+
* if cur start time >= pq.peek() => means we can use this room for the meeting, we poll out the the original period and add current period to the pq(Means we update the room with the new meeting).
17+
```Java
18+
class Solution {
19+
public int minMeetingRooms(int[][] intervals) {
20+
if(intervals == null || intervals.length == 0) return 0;
21+
Arrays.sort(intervals, new Comparator<int[]>(){
22+
@Override
23+
public int compare(int[] a, int[] b){
24+
return a[0] - b[0];
25+
}
26+
});
27+
PriorityQueue<Integer> pq = new PriorityQueue<>();
28+
int room = 0;
29+
for(int i = 0; i < intervals.length; i++){
30+
pq.offer(intervals[i][1]);
31+
if(intervals[i][0] < pq.peek()) room++;
32+
else{
33+
pq.poll();
34+
}
35+
}
36+
return room;
37+
}
38+
}
39+
```
1240

13-
```Java
14-
To be add
15-
```
41+
* Method 2: Two pointer + sort
42+
* we sort start time and end time.
43+
* initialize the start and end index as 0.
44+
* if start time < end time, means we have a meeting in active, active++.
45+
* else active--.
46+
* We need to record the max number of the active room.
47+
```Java
48+
class Solution {
49+
public int minMeetingRooms(int[][] intervals) {
50+
int len = intervals.length;
51+
int[] startTime = new int[len];
52+
int[] endTime = new int[len];
53+
int index = 0;
54+
for(int[] interval: intervals){
55+
startTime[index] = interval[0];
56+
endTime[index++] = interval[1];
57+
}
58+
Arrays.sort(startTime);
59+
Arrays.sort(endTime);
60+
int i = 0, j = 0;
61+
int activate = 0, max = 0;
62+
while(i < len && j < len){
63+
if(startTime[i] < endTime[j]){
64+
activate++;
65+
i++;
66+
}else{
67+
activate--;
68+
j++;
69+
}
70+
max = Math.max(max, activate);
71+
}
72+
return max;
73+
}
74+
}
75+
```

leetcode/53. Maximum Subarray.md

+19
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,22 @@ class Solution {
107107
}
108108
}
109109
```
110+
111+
### Fourth Time
112+
* Method 1: dp
113+
```Java
114+
class Solution {
115+
public int maxSubArray(int[] nums) {
116+
if(nums == null || nums.length == 0) return 0;
117+
int len = nums.length;
118+
int res = Integer.MIN_VALUE;
119+
int[] dp = new int[len + 1];
120+
dp[0] = 0;
121+
for(int i = 1; i <= len; i++){
122+
dp[i] = Math.max(nums[i - 1], dp[i - 1] + nums[i - 1]);
123+
res = Math.max(res, dp[i]);
124+
}
125+
return res;
126+
}
127+
}
128+
```

leetcode/70. Climbing Stairs.md

+16
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,20 @@ class Solution {
9898
return dp[n];
9999
}
100100
}
101+
```
102+
103+
### Fourth Time
104+
* Method 1: dp
105+
```Java
106+
class Solution {
107+
public int climbStairs(int n) {
108+
int[] dp = new int[n + 1];
109+
dp[0] = 1;
110+
for(int i = 1; i <= n; i++){
111+
dp[i] = dp[i - 1];
112+
dp[i] += (i - 2 >= 0) ? dp[i - 2]: 0;
113+
}
114+
return dp[n];
115+
}
116+
}
101117
```

leetcode/700. Climbing Stairs.md

-84
This file was deleted.

0 commit comments

Comments
 (0)