Press "Enter" to skip to content

Posts published in “Easy”

花花酱 LeetCode 121. Best Time to Buy and Sell Stock

题目大意: 给你一只股票每天的价格,如果只能做一次交易(一次买进一次卖出)问你最多能赚多少钱。

Problem:

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 (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Example 1:

Example 2:

 

Idea:

DP

Solution 1:

C++

C++ / reduce to maximum subarray

Related Problems:

花花酱 LeetCode 748. Shortest Completing Word

题目大意: 给你一个由字母和数字组成车牌。另外给你一些单词,让你找一个最短的单词能够覆盖住车牌中的字母(不考虑大小写)。如果有多个解,输出第一个解。

Problem:

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate

Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word.

It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.

The license plate might have the same letter occurring multiple times. For example, given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does.

Example 1:

Example 2:

Note:

  1. licensePlate will be a string with length in range [1, 7].
  2. licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
  3. words will have a length in the range [10, 1000].
  4. Every words[i] will consist of lowercase letters, and have length in range [1, 15].

Idea:

Hashtable

Solution:

C++

Time complexity: 时间复杂度 O(N*26), N is number of words.

Space complexity: 空间复杂度 O(26) = O(1)

 

花花酱 LeetCode 530. Minimum Absolute Difference in BST

Link

Problem:

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

Example:

Note: There are at least two nodes in this BST.


Idea:

Sorting via inorder traversal gives us sorted values, compare current one with previous one to reduce space complexity from O(n) to O(h).

Solution:

C++ O(n) space

C++ O(h) space

Java

Python

Related Problems:

  • [解题报告] LeetCode 98. Validate Binary Search Tree

花花酱 LeetCode 198. House Robber

Problem:

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Idea:

DP

Time complexity: O(n)

Space complexity: O(n) -> O(1)

Solution:

C++ / Recursion + Memorization

Time complexity: O(n)

Space complexity: O(n)

 

C++ / DP

Time complexity: O(n)

Space complexity: O(n)

C++ / O(1) Space

 

花花酱 LeetCode 734. Sentence Similarity

Problem:

Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar.

For example, “great acting skills” and “fine drama talent” are similar, if the similar word pairs are pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]].

Note that the similarity relation is not transitive. For example, if “great” and “fine” are similar, and “fine” and “good” are similar, “great” and “good” are not necessarily similar.

However, similarity is symmetric. For example, “great” and “fine” being similar is the same as “fine” and “great” being similar.

Also, a word is always similar with itself. For example, the sentences words1 = ["great"], words2 = ["great"], pairs = [] are similar, even though there are no specified similar word pairs.

Finally, sentences can only be similar if they have the same number of words. So a sentence like words1 = ["great"] can never be similar to words2 = ["doubleplus","good"].

Note:

  • The length of words1 and words2 will not exceed 1000.
  • The length of pairs will not exceed 2000.
  • The length of each pairs[i] will be 2.
  • The length of each words[i] and pairs[i][j] will be in the range [1, 20].


题目大意:

给你两个句子(由单词数组表示)和一些近义词对,问你这两个句子是否相似,即每组相对应的单词都要相似。

注意相似性不能传递,比如给只你”great”和”fine”相似、”fine”和”good”相似,这种情况下”great”和”good”不相似。

Idea:

Use hashtable to store mapping from word to its similar words.

Solution: HashTable

Time complexity: O(|pairs| + |words1|)

Space complexity: O(|pairs|)

C++

Java

Python

Related Problems: