Skip to content

Commit 75491c4

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parent c03b738 commit 75491c4

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

leetcode/323. Number of Connected Components in an Undirected Graph.md

+32
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,35 @@ You can assume that no duplicate edges will appear in edges. Since all edges are
5858
}
5959
}
6060
```
61+
62+
### Amazon session
63+
* Method 1: union find
64+
```Java
65+
class Solution {
66+
private int[] uf;
67+
public int countComponents(int n, int[][] edges) {
68+
this.uf = new int[n];
69+
for(int i = 0; i < n; i++)
70+
uf[i] = i;
71+
for(int[] edge : edges){
72+
union(edge[0], edge[1]);
73+
}
74+
int count = 0;
75+
for(int i = 0; i < n; i++)
76+
if(uf[i] == i)
77+
count++;
78+
return count;
79+
}
80+
private int find(int i){
81+
if(uf[i] != i){
82+
uf[i] = find(uf[i]);
83+
}
84+
return uf[i];
85+
}
86+
private void union(int i, int j){
87+
int x = find(i);
88+
int y = find(j);
89+
uf[x] = y;
90+
}
91+
}
92+
```
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
## 983. Minimum Cost For Tickets
2+
3+
### Question
4+
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
5+
6+
Train tickets are sold in 3 different ways:
7+
* a 1-day pass is sold for costs[0] dollars;
8+
* a 7-day pass is sold for costs[1] dollars;
9+
* a 30-day pass is sold for costs[2] dollars.
10+
11+
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
12+
13+
Return the minimum number of dollars you need to travel every day in the given list of days.
14+
15+
```
16+
Example 1:
17+
18+
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
19+
Output: 11
20+
Explanation:
21+
For example, here is one way to buy passes that lets you travel your travel plan:
22+
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
23+
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
24+
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
25+
In total you spent $11 and covered all the days of your travel.
26+
27+
Example 2:
28+
29+
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
30+
Output: 17
31+
Explanation:
32+
For example, here is one way to buy passes that lets you travel your travel plan:
33+
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
34+
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
35+
In total you spent $17 and covered all the days of your travel.
36+
```
37+
38+
39+
Note:
40+
1. 1 <= days.length <= 365
41+
2. 1 <= days[i] <= 365
42+
3. days is in strictly increasing order.
43+
4. costs.length == 3
44+
5. 1 <= costs[i] <= 1000
45+
46+
### Solutions
47+
* Method 1: dp
48+
1. transfer function:
49+
2. for every single day, the we check if it deserves to by a long term ticket.
50+
```Java
51+
class Solution {
52+
public int mincostTickets(int[] days, int[] costs) {
53+
int last = days[days.length - 1];
54+
int[] dp = new int[last + 1];
55+
int index = 0;
56+
for(int i = 1; i <= last; i++){
57+
if(i != days[index]){
58+
dp[i] = dp[i - 1];
59+
continue;
60+
}
61+
dp[i] = dp[i - 1] + costs[0]; // buy 1 day pass.
62+
dp[i] = Math.min(dp[i], (i - 7 >= 0 ? dp[i - 7]: 0) + costs[1]); // buy 7 day pass.
63+
dp[i] = Math.min(dp[i], (i - 30 >= 0 ? dp[i - 30]: 0) + costs[2]); // buy 30 day pass.
64+
index++;
65+
}
66+
return dp[last];
67+
}
68+
}
69+
```

0 commit comments

Comments
 (0)