Hypercube's LeetCode Weekly Contest 49

  • Rank: 731 / 2362
  • Score: 9 / 27
  • Finish Time: 00:26:39

Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence.

Example 1:

Input: [1, 3, 5, 4, 7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1, 3, 5], its length is 3.
Even though [1, 3, 5, 7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.

Example 2:

Input: [2, 2, 2, 2, 2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.

Note: Length of the array will not exceed 10,000.

Accepted in 00:06:40 with 1 wrong submissions
class Solution:
    def findLengthOfLCIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0
        m = 0
        length = 0
        last = nums[0] - 1
        for i in nums:
            if i > last:
                length += 1
                last = i
            else:
                m = max(m, length)
                length = 1
                last = i
        return max(m, length)

Implement Magic Dictionary

Implement a magic directory with buildDict, and search methods.

For the method buildDict, you’ll be given a list of non-repetitive words to build a dictionary.

For the method search, you’ll be given a word, and judge whether if you modify exactly one character into another character in this word, the modified word is in the dictionary you just built.

Example 1:

Input: buildDict(["hello", "leetcode"]), Output: Null
Input: search("hello"), Output: False
Input: search("hhllo"), Output: True
Input: search("hell"), Output: False
Input: search("leetcoded"), Output: False

Note:

  1. You may assume that all the inputs are consist of lowercase letters a-z.
  2. For contest purpose, the test data is rather small by now. You could think about highly efficient algorithm after the contest.
  3. Please remember to RESET your class variables declared in class MagicDictionary, as static/class variables are persisted across multiple test cases. Please see here for more details.
Accepted in 00:16:39 with 1 wrong submissions
class MagicDictionary:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.s = None

    def buildDict(self, dict):
        """
        Build a dictionary through a list of words
        :type dict: List[str]
        :rtype: void
        """
        self.s = {}
        for word in dict:
            for i in range(len(word)):
                self.s.setdefault(word[:i] + '_' + word[i+1:], set()).add(word)

    def search(self, word):
        """
        Returns if there is any word in the trie that equals to the given word after modifying exactly one character
        :type word: str
        :rtype: bool
        """
        for i in range(len(word)):
            n = word[:i] + '_' + word[i+1:]
            if n in self.s:
                if len(self.s[n]) > 1 or word not in self.s[n]:
                    return True
        return False


# Your MagicDictionary object will be instantiated and called as such:
# obj = MagicDictionary()
# obj.buildDict(dict)
# param_2 = obj.search(word)

Cut Off Trees for Golf Event

You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map:

  1. 0 represents the obstacle can’t be reached.
  2. 1 represents the ground can be walked through.
  3. The place with number bigger than 1 represents a tree can be walked through, and this positive number represents the tree’s height.

You are asked to cut off all the trees in this forest in the order of tree’s height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).

You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees. If you can’t cut off all the trees, output -1 in that situation.

You are guaranteed that no two trees have the same height and there is at least one tree needs to be cut off.

Example 1:

Input:
[
 [1, 2, 3],
 [0, 0, 4],
 [7, 6, 5]
]
Output: 6

Example 2:

Input:
[
 [1, 2, 3],
 [0, 0, 0],
 [7, 6, 5]
]
Output: -1

Example 3:

Input:
[
 [2, 3, 4],
 [0, 0, 5],
 [8, 7, 6]
]
Output: 6
Explanation: You started from the point (0, 0) and you can cut off the tree in (0, 0) directly without walking.

Hint: size of the given matrix will not exceed 50x50.

Unsolved

Number of Longest Increasing Subsequence

Given an unsorted array of integers, find the number of longest increasing subsequence.

Example 1:

Input: [1, 3, 5, 4, 7]
Output: 2
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].

Example 2:

Input: [2, 2, 2, 2, 2]
Output: 5
Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.

Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.

Unsolved