2018年6月1日 星期五

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

轉自LeetCode

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.
You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)
Return the maximum profit you can make.
Example 1:
Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
  • Buying at prices[0] = 1
  • Selling at prices[3] = 8
  • Buying at prices[4] = 4
  • Selling at prices[5] = 9
  • The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
    Note:


  • 0 < prices.length <= 50000.
  • 0 < prices[i] < 50000.
  • 0 <= fee < 50000.


  • <Solution>

    [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown

    轉自LeetCode

    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:
    Input: [1,2,3,0,2]
    Output: 3 
    Explanation: transactions = [buy, sell, cooldown, buy, sell]
    <Solution>

    [LeetCode] 628. Maximum Product of Three Numbers

    轉自LeetCode

    Given an integer array, find three numbers whose product is maximum and output the maximum product.
    Example 1:
    Input: [1,2,3]
    Output: 6
    
    Example 2:
    Input: [1,2,3,4]
    Output: 24
    
    Note:
    1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
    2. Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.
    <Solution>

    [LeetCode] 617. Merge Two Binary Trees

    轉自LeetCode

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
    You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.
    Example 1:
    Input: 
     Tree 1                     Tree 2                  
              1                         2                             
             / \                       / \                            
            3   2                     1   3                        
           /                           \   \                      
          5                             4   7                  
    Output: 
    Merged tree:
          3
         / \
        4   5
       / \   \ 
      5   4   7
    
    Note: The merging process must start from the root nodes of both trees.
    <Solution>

    [LeetCode] 652. Find Duplicate Subtrees

    轉自LeetCode

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them.
    Two trees are duplicate if they have the same structure with same node values.
    Example 1: 
            1
           / \
          2   3
         /   / \
        4   2   4
           /
          4
    
    The following are two duplicate subtrees:
          2
         /
        4
    
    and
        4
    
    Therefore, you need to return above trees' root in the form of a list.

    <Solution>

    [LeetCode] 606. Construct String from Binary Tree

    轉自 LeetCode

    You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.
    The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.
    Example 1:
    Input: Binary tree: [1,2,3,4]
           1
         /   \
        2     3
       /    
      4     
    
    Output: "1(2(4))(3)"
    
    Explanation: Originallay it needs to be "1(2(4)())(3()())", 
    but you need to omit all the unnecessary empty parenthesis pairs. 
    And it will be "1(2(4))(3)".
    
    Example 2:
    Input: Binary tree: [1,2,3,null,4]
           1
         /   \
        2     3
         \  
          4 
    
    Output: "1(2()(4))(3)"
    
    Explanation: Almost the same as the first example, 
    except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
    <Solution>

    2018年5月22日 星期二

    [LeetCode] 649. Dota2 Senate

    轉自LeetCode

    In the world of Dota2, there are two parties: the Radiant and the Dire.
    The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
    1. Ban one senator's right
      A senator can make another senator lose all his rights in this and all the following rounds.
    2. Announce the victory
      If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and make the decision about the change in the game.
    Given a string representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party respectively. Then if there are n senators, the size of the given string will be n.
    The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
    Suppose every senator is smart enough and will play the best strategy for his own party, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be Radiant or Dire.
    Example 1:
    Input: "RD"
    Output: "Radiant"
    Explanation: The first senator comes from Radiant and he can just ban the next senator's right in the round 1. 
    And the second senator can't exercise any rights any more since his right has been banned. 
    And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
    
    Example 2:
    Input: "RDD"
    Output: "Dire"
    Explanation: 
    The first senator comes from Radiant and he can just ban the next senator's right in the round 1. 
    And the second senator can't exercise any rights anymore since his right has been banned. 
    And the third senator comes from Dire and he can ban the first senator's right in the round 1. 
    And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
    
    Note:
    1. The length of the given string will in the range [1, 10,000].
    <Solution>