# LeetCode第七天

## 数组 Medium

#### 40.（162）Find Peak Element

##### JAVA
//斜率思想，二分法
class Solution {
public int findPeakElement(int[] nums) {
int l=0,r=nums.length-1;
while(l<r){
int mid = (r+l)/2;
if(nums[mid]>nums[mid+1])
r = mid;
else
l = mid+1;
}
return l;
}
}


#### 41.（731）My Calendar II

##### JAVA
public class MyCalendarTwo {
List<int[]> calendar;
List<int[]> overlaps;//已经重叠过一次的区间

MyCalendarTwo() {
calendar = new ArrayList();
overlaps = new ArrayList();
}

public boolean book(int start, int end) {
for (int[] iv: overlaps) {
if (iv[0] < end && start < iv[1]) return false;
}
for (int[] iv: calendar) {
if (iv[0] < end && start < iv[1])
}
return true;
}
}


#### 42.（153）Find Minimum in Rotated Sorted Array

##### JAVA
class Solution {
public int findMin(int[] nums) {
int l = 0;
int r = nums.length-1;
while(l<r){
int mid = (l+r)/2;
if(nums[mid]<nums[r])
r = mid;
else
l = mid+1;
}
return nums[r];
}
}

class Solution {
public int findMin(int[] nums) {
return find(nums,0,nums.length-1);
}

public int find(int[] nums, int l, int r) {
if(nums[l] <= nums[r]) {
return nums[l];
}
int mid = (l + r) / 2;
return Math.min(find(nums,l,mid),find(nums,mid+1,r));
}
}


#### 43.（152）Maximum Product Subarray

##### JAVA
class Solution {
public int maxProduct(int[] nums) {
if(nums.length == 0){
return 0;
}
int maxPre = nums[0];
int minPre = nums[0];
int max = nums[0];
for(int i =1;i<nums.length;i++){
int maxHere = Math.max(Math.max(maxPre*nums[i],minPre*nums[i]),nums[i]);
int minHere = Math.min(Math.min(maxPre*nums[i],minPre*nums[i]),nums[i]);
max = Math.max(max,maxHere);
maxPre = maxHere;
minPre = minHere;
}
return max;
}
}


#### 44.（611）Valid Triangle Number

##### JAVA
class Solution {
public int triangleNumber(int[] nums) {
int count = 0;
Arrays.sort(nums);
for(int i =0;i<nums.length-2;i++){
if(nums[i]==0)
continue;
int k = i+2;
for(int j = i+1;j<nums.length-1;j++){
while(k<nums.length&&nums[i]+nums[j]>nums[k])
k++;
count += k-j-1;
}
}
return count;
}
}


##### JAVA
//计算休眠时间，再加上任务时间等于总时间
class Solution {
public int leastInterval(char[] tasks, int n) {
int[] map = new int[26];
map[c-'A']++;
Arrays.sort(map);
int idle = (map[25] -1)*n;
for(int i=24;i>=0&&map[i]>0;i--){
idle -= Math.min(map[i],map[25]-1);
}
}
}


09-01 25

02-28 26

12-20 366

12-03 475

11-08 88

01-19 42

06-26 66

04-29 170

09-08 86

03-03 159

11-20 58

06-11 467

11-19 105

02-18 2246

04-17 311

01-26 596

04-10 65

05-13 86

04-19 1256

04-14 58万+

03-13 14万+

03-01 13万+

03-04 13万+

03-08 4万+

03-08 7万+

04-25 6万+

03-10 12万+

03-12 11万+

03-13 11万+

03-14 1万+

03-18 9076

03-19 3万+

03-19 8万+

03-19 3万+

03-22 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-27 1万+

03-29 21万+

03-29 10万+

03-30 16万+

05-28 5425

05-28 1万+

04-02 3086

04-02 4万+

04-06 7万+

04-09 8万+

04-09 2万+

05-17 7772

04-11 3万+

04-15 6万+

04-18 4万+

04-20 4万+

04-24 3万+

04-29 6666

04-30 9648

04-30 9129

05-01 4783

05-03 9114

05-16 5万+

05-06 1万+

05-08 4万+

05-10 2984

05-14 5926

05-14 1668

05-16 3263

05-16 1万+

05-20 658

05-18 7909

05-27 829

05-18 3158

05-19 1万+

05-21 6186

05-21 6187

05-27 4524

05-25 4463

05-23 6157

05-28 722

#### 因为无聊，我做出了一个能下载电影、动漫、电视剧的小程序，从此告别过去只能看部分视频简介的历史

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客