Press "Enter" to skip to content

Posts tagged as “hard”

花花酱 LeetCode 839. Similar String Groups

Problem

Two stringsĀ XĀ andĀ YĀ are similar if we can swap two letters (in different positions) ofĀ X, so thatĀ it equalsĀ Y.

For example,Ā "tars"Ā andĀ "rats"Ā are similar (swapping at positionsĀ 0Ā andĀ 2), andĀ "rats"Ā andĀ "arts"Ā are similar, butĀ "star"Ā is not similar toĀ "tars",Ā "rats", orĀ "arts".

Together, these form two connected groups by similarity:Ā {"tars", "rats", "arts"}Ā andĀ {"star"}.Ā  Notice thatĀ "tars"Ā andĀ "arts"Ā are in the same group even though they are not similar.Ā  Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.

We are given a listĀ AĀ of strings.Ā  Every string inĀ AĀ is an anagram of every other string inĀ A.Ā  How many groups are there?

Example 1:

Input: ["tars","rats","arts","star"]
Output: 2

Note:

  1. A.length <= 2000
  2. A[i].length <= 1000
  3. A.length * A[i].length <= 20000
  4. All words inĀ AĀ consist of lowercase letters only.
  5. All words inĀ AĀ have the same length and are anagrams of each other.
  6. The judging time limit has been increased for this question.

Solution: Brute Force + Union Find

Time Complexity: O(n^2 * L)

Space Complexity: O(n)

C++

 

花花酱 LeetCode 410. Split Array Largest Sum

Problem

Given an array which consists of non-negative integers and an integerĀ m, you can split the array intoĀ mĀ non-empty continuous subarrays. Write an algorithm to minimize the largest sum among theseĀ mĀ subarrays.

Note:
IfĀ nĀ is the length of array, assume the following constraints are satisfied:

  • 1 ā‰¤Ā nĀ ā‰¤ 1000
  • 1 ā‰¤Ā mĀ ā‰¤ min(50,Ā n)

Examples:

Input:
nums = [7,2,5,10,8]
m = 2

Output:
18

Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.

Ā 

Solution: DP

Time complexity: O(n^2*m)

Space complexity: O(n*m)

C++ / Recursion + Memorization

C++ / DP

Solution: Binary Search

Time complexity: O(log(sum(nums))*n)

Space complexity: O(1)

 

花花酱 LeetCode 847. Shortest Path Visiting All Nodes

Problem

题ē›®å¤§ę„ļ¼šę±‚锶ē‚¹č¦†ē›–ēš„ęœ€ēŸ­č·Æå¾„ć€‚

https://leetcode.com/problems/shortest-path-visiting-all-nodes/description/

An undirected, connected graph of N nodes (labeledĀ 0, 1, 2, ..., N-1) is given asĀ graph.

graph.length = N, andĀ j != iĀ is in the listĀ graph[i]Ā exactly once, if and only if nodesĀ iĀ andĀ jĀ are connected.

Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.

Example 1:

Input: [[1,2,3],[0],[0],[0]]
Output: 4
Explanation: One possible path is [1,0,2,0,3]

Example 2:

Input: [[1],[0,2,4],[1,3,4],[2],[1,2]]
Output: 4
Explanation: One possible path is [0,1,4,2,3]

Solution: BFS

Time complexity: O(n*2^n)

Space complexity: O(n*2^n)

C++

C++ / vector

Related Problems

花花酱 LeetCode 810. Chalkboard XOR Game

Problem

We are given non-negative integers nums[i] which are written on a chalkboard.Ā  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.Ā  If erasing a number causesĀ the bitwise XOR of all the elements of the chalkboard to becomeĀ 0, then that player loses.Ā  (Also, we’ll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.)

Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins.

Return True if and only if Alice wins the game, assuming both players play optimally.

Example:
Input: nums = [1, 1, 2]
Output: false
Explanation: 
Alice has two choices: erase 1 or erase 2. 
If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. 
If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.

Notes:

  • 1 <= N <= 1000.
  • 0 <= nums[i] <= 2^16.

Solution: Math

Time complexity: O(n)

Space complexity: O(1)

 

 

花花酱 LeetCode 827. Making A Large Island

Problem

In a 2D grid ofĀ 0s andĀ 1s, we change at most oneĀ 0Ā to aĀ 1.

After, what is the size of the largest island?Ā (An island is a 4-directionally connected group ofĀ 1s).

Example 1:

Input: [[1, 0], [0, 1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.

Example 2:

Input: [[1, 1], [1, 0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 1.

Example 3:

Input: [[1, 1], [1, 1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 1.

Notes:

  • 1 <= grid.length = grid[0].length <= 50.
  • 0 <= grid[i][j] <= 1.

Solution

Step 1: give each connected component a unique id and count its ara.

Step 2: for each 0 zero, check its 4 neighbours, sum areas up by unique ids.

Time complexity: O(n*m)

Space complexity: O(n*m)

C++