Long Luo's Life Notes

每一天都是奇迹

By Long Luo

This article is the solution 2 Approaches: DFS and BFS with Detailed Explanation of Problem 199. Binary Tree Right Side View.

Here shows 2 Approaches to slove this problem: DFS and BFS.

DFS

We traverse the tree in the order of \(\textit{root node} \to \textit{right subtree} \to \textit{left subtree}\) to ensure that each level traverse the rightmost node first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public List<Integer> rightSideView_dfs(TreeNode root) {
List<Integer> ans = new ArrayList<>();
if (root == null) {
return ans;
}

dfs(root, 1, ans);
return ans;
}

public void dfs(TreeNode root, int depth, List<Integer> numList) {
if (root == null) {
return;
}

if (numList.size() < depth) {
numList.add(root.val);
}

dfs(root.right, depth + 1, numList);
dfs(root.left, depth + 1, numList);
}

Analysis

  • Time Complexity: \(O(n)\)
  • Space Complexity: \(O(n)\)

BFS

We can use BFS to traverse the levels of the tree and record the last element of each level.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public List<Integer> rightSideView_bfs(TreeNode root) {
List<Integer> ans = new ArrayList<>();
if (root == null) {
return ans;
}

Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = size - 1; i >= 0; i--) {
TreeNode node = queue.poll();
if (i == size - 1) {
ans.add(node.val);
}

if (node.right != null) {
queue.offer(node.right);
}

if (node.left != null) {
queue.offer(node.left);
}
}
}

return ans;
}

Analysis

  • Time Complexity: \(O(n)\).
  • Space Complexity: \(O(n)\).

All suggestions are welcome. If you have any query or suggestion please comment below. Please upvote👍 if you like💗 it. Thank you:-)

Explore More Leetcode Solutions. 😉😃💗

By Long Luo

This article is the solution 3 Approaches: DFS, BFS, DP of Problem 322. Coin Change.

Here shows 3 Approaches to slove this problem: DFS, BFS and Dynamic Programming.

DFS

TLE!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
int minCount = Integer.MAX_VALUE;

public int coinChange(int[] coins, int amount) {
if (amount <= 0) {
return 0;
}

minCount = Integer.MAX_VALUE;
dfs(coins, amount, 0);
return minCount == Integer.MAX_VALUE ? -1 : minCount;
}

private int dfs(int[] coins, int remain, int count) {
if (remain < 0) {
return -1;
}

if (remain == 0) {
minCount = Math.min(minCount, count);
return count;
}

for (int x : coins) {
dfs(coins, remain - x, count + 1);
}

return -1;
}
}

Sorting the array first, then use DFS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
int minAns = Integer.MAX_VALUE;

public int coinChange(int[] coins, int amount) {
Arrays.sort(coins);
minAns = Integer.MAX_VALUE;
coinChange(coins, amount, coins.length - 1, 0);
return minAns == Integer.MAX_VALUE ? -1 : minAns;
}

private void coinChange(int[] coins, int amount, int index, int cnt) {
if (amount == 0) {
minAns = Math.min(minAns, cnt);
return;
}

if (index < 0) {
return;
}

for (int k = amount / coins[index]; k >= 0 && k + cnt < minAns; k--) {
coinChange(coins, amount - k * coins[index], index - 1, cnt + k);
}
}
}

Memory DFS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Solution {
public int coinChange(int[] coins, int amount) {
if (amount <= 0) {
return 0;
}

return coinChange(coins, amount, new int[amount]);
}

private int coinChange(int[] coins, int rem, int[] count) {
if (rem < 0) {
return -1;
}

if (rem == 0) {
return 0;
}

if (count[rem - 1] != 0) {
return count[rem - 1];
}

int min = Integer.MAX_VALUE;
for (int coin : coins) {
int res = coinChange(coins, rem - coin, count);
if (res >= 0 && res < min) {
min = 1 + res;
}
}
count[rem - 1] = (min == Integer.MAX_VALUE) ? -1 : min;
return count[rem - 1];
}
}

Analysis

  • Time Complexity: \(O(amount \times n)\)
  • Space Complexity: \(O(amount)\)
阅读全文 »

By Long Luo

This article is the solution Dynamic Programming Space O(n) Solutions: Top-Down and Bottom-Up Approaches of Problem 120. Triangle .

Intuition

This problem is a classic and typical dynamic programming problem. We can break the large problem into sub-problems.

We can use both the Top-Down and Bottom-Up approach to solve this problem.

Top-Down Approach

  1. State definition:

\(dp[i][j]\) represents the minimum path sum of row \(i\) and column \(j\).

  1. State Analysis:

\(dp[0][0]=c[0][0]\)

  1. The State Transfer Equation:

\[ dp[i][j] = \begin{cases} dp[i-1][0] + c[i][0] & j=0 \\ dp[i-1][i-1] + c[i][i] & j==i \\ min(dp[i-1][j-1], dp[i-1][j]) + c[i][j] & 0 < j < i \\ \end{cases} \]

so we can easily write such code:

阅读全文 »

By Long Luo

This article is the solution How to Maintain the Enqueued Tasks? | Sorting the array then Priority Queue | minHeap of Problem 1834. Single-Threaded CPU .

Intuition

To simulate the CPU operations, there comes \(3\) questions:

  1. Which task should int the enqueued tasks list?

  2. How to maintain the enqueued tasks?

  3. Which task of the enqueued tasks should the CPU choose?

Let’s answer the \(3\) questions:

  1. We assign the tasks to the CPU by \(\textit{enqueueTime}\), so we sort the array first by \(\textit{enqueueTime}\). However, we will lose the \(\textit{index}\) of the task.

We can parse the task by creating a new class \(\texttt{Job}\), whose members are \(\textit{id}\), \(\textit{enqueueTime}\), \(\textit{processTime}\).

  1. We put all the tasks assigned to the CPU into a Priority Queue and poll out the task whose \(\textit{processTime}\) is the least each time.

  2. We can maintain a \(\textit{curTime}\) variable, which represents the current time with initial value is \(0\).

If the CPU has no tasks to execute, we set the \(\textit{curTime}\) to \(\textit{enqueueTime}\) of the next task in the array that has not yet been assigned to the CPU.

After this, we put all \(\textit{enqueueTime} \le \textit{curTime}\) tasks into the Priority Queue.

阅读全文 »

By Long Luo

This article is the solution 4 Approaches: Brute Force, HashMap, Binary Search, Two Pointers of Problem 167. Two Sum II - Input Array Is Sorted .

Here shows 4 Approaches to slove this problem: Brute Force, HashMap, Binary Search, Two Pointers.

Brute Force

It’s easy to use Brute Force to find the answer, however, the time complexity is \(O(n^2)\), so the BF solution will Time Limit Exceeded!

1
2
3
4
5
6
7
8
9
10
11
12
public static int[] twoSum_bf(int[] numbers, int target) {
int len = numbers.length;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (numbers[i] + numbers[j] == target) {
return new int[]{i + 1, j + 1};
}
}
}

return new int[0];
}

Analysis

  • Time Complexity: \(O(n^2)\).
  • Space Complexity: \(O(1)\).

HashMap

We can use a extra \(\texttt{HashMap}\) to record the element we traversalled.

阅读全文 »
0%