Skip to content

Commit 75e4720

Browse files
Merge pull request algorithm001#661 from yxs354/master
submit week 04 assignment
2 parents be568b1 + 6931dd4 commit 75e4720

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Week_04/id_44/LeetCode_169_044.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class LeetCode_169_044 {
2+
public int majorityElement(int[] nums) {
3+
return divc(nums, 0, nums.length - 1);
4+
}
5+
private int divc(int[] nums, int l, int r){
6+
if(l == r)
7+
return nums[l];
8+
int mid = l + (r - l)/2;
9+
int ls = divc(nums, l, mid);
10+
int rs = divc(nums, mid+1, r);
11+
if(ls == rs)
12+
return ls;
13+
int c1 = 0, c2 = 0;
14+
for(int i = l; i <= r; i++){
15+
if(nums[i] == ls) c1++;
16+
if(nums[i] == rs) c2++;
17+
}
18+
return c1 > c2 ? ls : rs;
19+
}
20+
}

Week_04/id_44/LeetCode_746_044.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class LeetCode_746_044 {
2+
public int minCostClimbingStairs(int[] cost) {
3+
int step[] = new int[1024];
4+
step[0] = 0;
5+
step[1] = 0;
6+
if (cost.length == 0) {
7+
return 0;
8+
}
9+
if (cost.length == 1) {
10+
return cost[0];
11+
}
12+
if (cost.length == 2) {
13+
return Math.min(cost[0], cost[1]);
14+
}
15+
for (int i = 2; i <= cost.length; i++) {
16+
step[i] = Math.min(step[i - 1] + cost[i - 1], step[i - 2] + cost[i - 2]);
17+
}
18+
return step[cost.length];
19+
}
20+
}

0 commit comments

Comments
 (0)