七月在线学习笔记第一课

第一题 LeetCode121 Best Time to Buy and Sell Stock

题目描述

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

代码

暴力枚举

int max(int a,int b){
    if(a>b)
        return a;
    else
        return b;
}
int maxProfit(int* prices, int pricesSize){
    int ans=0;
    for(int i=0;i<pricesSize;i++){
        for(int j=i;j<pricesSize;j++){
            ans=max(prices[j]-prices[i],ans);
        }
    }
    return ans;
}
复杂度分析 : O(N2)

优化

int max(int a,int b){
    if(a>b)
        return a;
    else
        return b;
}
int maxProfit(int* prices, int pricesSize){
    int ans=0;
    if(pricesSize==0){
        return ans;
    }
    int min=prices[0];
    for(int i=1;i<pricesSize;i++){
        if(prices[i]<min){
            min=prices[i];
        }
        else{
            ans=max(prices[i]-min,ans);
        }
    }
    return ans;
}
复杂度分析 : O(N)

第二题 LeetCode53 Maximun Subarray

题目描述

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

代码

暴力枚举

int max(int a,int b){
    if(a>b) return a;
    else return b;
}
int maxSubArray(int *nums,int numsSize){
    int ans=nums[0],sum=0;
    for(int i=0;i<numsSize;i++){
        sum=0;
        for(int j=i;j<numsSize;j++){
            sum+=nums[j];
            ans=max(ans,sum);
        }
    }
    return ans;
}

优化算法

int max(int a,int b){
    if(a>b) return a;
    else return b;
}
int maxSubArray(int* nums, int numsSize){
    int ans=nums[0],sum=0;
    for(int i=0;i<numsSize;i++){
        sum+=nums[i];
        ans=max(ans,sum);
        //如果当前的子序列之和小于0,则需要舍弃这段子序列,因为返回最大的序列和(序列全为0依然需要返回最大的那个负值,
        //所以舍弃之前需要保留)
        sum=max(0,sum);
    }
    return ans;
}
int maxSubarray(int *a,int n){
	int sum=a[0],ans=a[0];
 	for(int i = 1; i < n; ++ i) {
 	//如果当前sum取到a[i],则表示重新开始计数
 	 	sum= max(a[i], sum+ a[i]);
     	ans= max(sum, ans);
	}
	return ans;	
}

第三题 LeetCode152 Maximum Product Subarray

题目描述

Click Here
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.

Example 1:

Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:

Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

代码

暴力枚举

int maxProduct(int *a,int n){
    int sum=1,ans=a[0];
    for(int i=0;i<n;i++){
        sum=1;
        for(int j=i;j<n;j++){
            sum*=a[j];
            ans=max(ans,sum);
        }
    }
    return ans;
}

优化算法

\quad 考虑一种特殊情况,前面得到一个较小的负数积,和后面一个较大负数相乘,就有可能得到最大值,所以我们不仅需要维护一个局部最大值,还需要维护一个局部最小值。

\quad 当遍历一个新数据时【若该新数据为负数时,当前局部最小的值乘上新数据很有可能成为最新的局部最大值】,更新当前的局部最小和局部最大。

\quad 全局最优值在之前的全局最优和当前的局部最大值中取最大。

int maxProduct(int *a,int n){
    int min_sum=a[0],max_sum=a[0],ans=a[0];
    for(int i=1;i<n;i++){
        int p=min_sum*a[i];
        int q=max_sum*a[i];
        
        max_sum=max(max(p,q),a[i]);
        min_sum=min(min(p,q),a[i]);
        ans=max(max_sum,ans);
    }
    return ans;
}

“最大的子数组和”与“最大的子数组积”本质区别是:“最大的子数组积”中需要维护一个局部最小值,因为该局部最小值遇到一个负数时,就可能成为局部最大值;而“最大的子数组和”中一个局部最小值无论遇到什么新数据,都不可能成为局部最大值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值