面试笔试杂项积累-leetcode 306-310

306.306-Additive Number-Difficulty: Medium

Additive number is a string whose digits can form additive sequence.

A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.

For example:
"112358" is an additive number because the digits can form an additive sequence: 1, 1, 2, 3, 5, 8.

1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
"199100199" is also an additive number, the additive sequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199

Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.

Given a string containing only digits '0'-'9', write a function to determine if it's an additive number.

Follow up:
How would you handle overflow for very large input integers?

思路

判断是否是additive number

暴力判断


public class Solution {
    public bool IsAdditiveNumber(string num) {
   if (num.Length <= 2) return false;
        for (int i = 1; i <= num.Length / 2; i++)
        {
            for (int j = 1; Math.Max(i, j) <= num.Length - i - j; j++)
            {
                if (isValid(i, j, num)) return true;
            }
        }
        return false;
    }
    bool isValid(int i, int j, string num)
    {
             if ((num[i] == '0' && j > 1)||(num[0] == '0' && i > 1)) 
             return false;
        long l1 = long.Parse(num.Substring(0, i));
        long l2 = long.Parse(num.Substring(i, j));
        String sum = "";
        for (int k = i + j; num.Length != k; k = sum.Length)
        {
            l2 = l1 + l2;
            l1 = l2 - l1;
            sum = l2.ToString();
            num = num.Substring(k);
            if (!num.StartsWith(sum)) return false;
        }
        return true; 
    }
}

309.309-Best Time to Buy and Sell Stock with Cooldown-Difficulty: Medium

Say you have an array 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(ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]

思路

像121和122题一样是买卖股票,这次是带了冷却一天

参考:

https://leetcode.com/discuss/73617/7-line-java-only-consider-sell-and-cooldown

Define:

profit1[i] = max profit on day i if I sell

profit2[i] = max profit on day i if I do nothing

How will those profits on day i+1 relate to profits on day i ?

1. profit1[i+1] means I must sell on day i+1, and there are 2 cases:

a. If I just sold on day i, then I have to buy again on day i and sell on day i+1

b. If I did nothing on day i, then I have to buy today and sell today 

Taking both cases into account, profit1[i+1] = max(profit1[i]+prices[i+1]-prices[i], profit2[i])

2. profit2[i+1] means I do nothing on day i+1, so it will be max(profit1[i], profit2[i])



public class Solution {
    public int MaxProfit(int[] prices) {
          int profit1=0, profit2=0;   
    for(int i=1; i<prices.Length; i++){
        int copy=profit1;
        profit1=Math.Max(profit1+prices[i]-prices[i-1], profit2);
        profit2=Math.Max(copy, profit2);
    }
    return Math.Max(profit1, profit2); 
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值