Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Week_04/id_44/LeetCode_169_044.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class LeetCode_169_044 {
public int majorityElement(int[] nums) {
return divc(nums, 0, nums.length - 1);
}
private int divc(int[] nums, int l, int r){
if(l == r)
return nums[l];
int mid = l + (r - l)/2;
int ls = divc(nums, l, mid);
int rs = divc(nums, mid+1, r);
if(ls == rs)
return ls;
int c1 = 0, c2 = 0;
for(int i = l; i <= r; i++){
if(nums[i] == ls) c1++;
if(nums[i] == rs) c2++;
}
return c1 > c2 ? ls : rs;
}
}
20 changes: 20 additions & 0 deletions Week_04/id_44/LeetCode_746_044.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class LeetCode_746_044 {
public int minCostClimbingStairs(int[] cost) {
int step[] = new int[1024];
step[0] = 0;
step[1] = 0;
if (cost.length == 0) {
return 0;
}
if (cost.length == 1) {
return cost[0];
}
if (cost.length == 2) {
return Math.min(cost[0], cost[1]);
}
for (int i = 2; i <= cost.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

贪心还是动态规划呢?

step[i] = Math.min(step[i - 1] + cost[i - 1], step[i - 2] + cost[i - 2]);
}
return step[cost.length];
}
}