LeetCode算法专题一:数组(3)

“昨晚一晚没有睡,还不是因为做什么有意义的事情!希望最近调整过来!”

121.Best Time to buy and sell stock(06.03)

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.

public int maxProfit(int[] prices) {
        int min = Integer.MAX_VALUE, max = 0;
        for (int price: prices) {
            min = Math.min(min, price);
            max = Math.max(price - min, max);
        }
        return max;
    }

“为什么我是三天之后再来刷题…你干了什么”

122. Best Time to Buy and Sell Stock II (0606)

上一道题的改进,我完全没有办法!

Say you have an array prices for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0

额,大佬的原话是:Is this question a joke?A simple code like this. The designer of this question must thought of something too complicated.

public class Solution {
public int maxProfit(int[] prices) {
    int total = 0;
    for (int i=0; i< prices.length-1; i++) {
        if (prices[i+1]>prices[i]) total += prices[i+1]-prices[i];
    }
    
    return total;
}

嗯我觉得主要的思想是:最大的利润是分为各个时间段的小利润加起来的。所以我们算出每两天的利润,是正的就加到总利润即可。

169.Majority Element:求一个数组中出现次数超过一半的元素。

我的想法:

  1. 分治,把这个数组均分为两个数组,最多元素要么都是这两个数组的最多元素,要么一个数组全部是它…不断分下去,好像代码行不通…
  2. 转为映射关系,原来数组的元素变为index,但是无法确定元素最大值有多大也就无法确定映射数组有多长…

别人的做法:
(啊啊啊啊!太强了!一题多解)

1.Sorting

public int majorityElement(int[] nums){
	java.util.Arrays.sort(nums);
	return nums[nums.length/2];
	}
	

2.Hashtable 这是我想要的解法


public int majorityElement(int[] nums){
	Map<Integer,Integer> myMap = new HashMap<Integer,Integer>();
	int ret = 0;
	for(int num:nums){
		if(!myMap.containsKey(num))	myMap.put(num,1);
		else
			myMap.put(num,myMap.get(num)+1);
		if(myMap.get(num)>nums.length/2){
			ret = num;
			break;
			}
			}
			return ret;
		}
		

3.Moore voting algorithm

public int majorityElement3(int[] nums) {
    int count=0, ret = 0;
    for (int num: nums) {
        if (count==0)
            ret = num;
        if (num!=ret)
            count--;
        else
            count++;
    }
    return ret;
}

Bit manipulation (位操作)这个还不太明白

public int majorityElement(int[] nums) {
    int[] bit = new int[32];
    for (int num: nums)
        for (int i=0; i<32; i++) 
            if ((num>>(31-i) & 1) == 1)
                bit[i]++;
    int ret=0;
    for (int i=0; i<32; i++) {
        bit[i]=bit[i]>nums.length/2?1:0;
        ret += bit[i]*(1<<(31-i));
    }
    return ret;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值