Press "Enter" to skip to content

Posts published in “Medium”

花花酱 LeetCode 740. Delete and Earn

Problem:

Given an array nums of integers, you can perform operations on the array.

In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete everyelement equal to nums[i] - 1 or nums[i] + 1.

You start with 0 points. Return the maximum number of points you can earn by applying such operations.

Example 1:

Example 2:

Note:

  • The length of nums is at most 20000.
  • Each element nums[i] is an integer in the range [1, 10000].


Idea:

Reduce the problem to House Robber Problem

Key observations: If we take nums[i]

  1. We can safely take all of its copies.
  2. We can’t take any of copies of nums[i – 1] and nums[i + 1]

This problem is reduced to 198 House Robber.

Houses[i] has all the copies of num whose value is i.

[3 4 2] -> [0 2 3 4], rob([0 2 3 4]) = 6            

[2, 2, 3, 3, 3, 4] -> [0 2*2 3*3 4], rob([0 2*2 3*3 4]) = 9

Time complexity: O(n+r) reduction + O(r) solving rob = O(n + r)

Space complexity: O(r)

r = max(nums) – min(nums) + 1

Time complexity: O(n + r)

Space complexity: O(r)

Solution:

Related Problem:

花花酱 LeetCode 735. Asteroid Collision

Problem:

We are given an array asteroids of integers representing asteroids in a row.

For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.

Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.

Example 1:

Example 2:

Example 3:

Example 4:

Note:

  • The length of asteroids will be at most 10000.
  • Each asteroid will be a non-zero integer in the range [-1000, 1000]..
Idea:
Simulation

Time complexity: O(n), Space complexity: O(n)

Solution:

C ++

 

花花酱 LeetCode 737. Sentence Similarity II

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, words1 = ["great", "acting", "skills"] and words2 = ["fine", "drama", "talent"] are similar, if the similar word pairs are pairs = [["great", "good"], ["fine", "good"], ["acting","drama"], ["skills","talent"]].

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

Similarity is also 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:

DFS / Union Find

Solution1:

Time complexity: O(|Pairs| * |words1|)

Space complexity: O(|Pairs|)

C++ / DFS

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

Space complexity: O(|Pairs|)

C++ / DFS Optimized

 

Solution2:

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

Space complexity: O(|Pairs|)

C++ / Union Find

 

C++ / Union Find, Optimized

Related Problems:

花花酱 LeetCode 64. Minimum Path Sum

Problem:

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Example 1:

Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.

 

Idea:

DP

Solution 1:

C++ / Recursion with memoization

Time complexity: O(mn)

Space complexity: O(mn)

Solution 2:

C++ / DP

Time complexity: O(mn)

Space complexity: O(1)

Related Problems:

 

花花酱 LeetCode 113. Path Sum II

Problem:

Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.

For example:
Given the below binary tree and sum = 22,

return

Idea:
Recursion
Solution:
C++

Related Problems: