[LeetCode] Max Difference You Can Get From Changing an Integer

1432. Max Difference You Can Get From Changing an Integer

You are given an integer num. You will apply the following steps exactly two times:

  • Pick a digit x (0 <= x <= 9).
  • Pick another digit y (0 <= y <= 9). The digit y can be equal to x.
  • Replace all the occurrences of x in the decimal representation of num by y.
  • The new integer cannot have any leading zeros, also the new integer cannot be 0.

Let a and b be the results of applying the operations to num the first and second times, respectively.

Return the max difference between a and b.

Read more
[LeetCode] Build an Array With Stack Operations

1441. Build an Array With Stack Operations

You are given an integer array target and an integer n.

You have an empty stack with the two following operations:

  • “Push”: pushes an integer to the top of the stack.
  • “Pop”: removes the integer on the top of the stack.

You also have a stream of the integers in the range [1, n].

Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:

  • If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
  • If the stack is not empty, pop the integer at the top of the stack.
  • If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.

Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.

Read more
[LeetCode] Choose Edges to Maximize Score in a Tree

2378. Choose Edges to Maximize Score in a Tree

You are given a weighted tree consisting of n nodes numbered from 0 to n - 1.

The tree is rooted at node 0 and represented with a 2D array edges of size n where edges[i] = [pari, weighti] indicates that node pari is the parent of node i, and the edge between them has a weight equal to weighti. Since the root does not have a parent, you have edges[0] = [-1, -1].

Choose some edges from the tree such that no two chosen edges are adjacent and the sum of the weights of the chosen edges is maximized.

Return the maximum sum of the chosen edges.

Note:

  • You are allowed to not choose any edges in the tree, the sum of weights in this case will be 0.
  • Two edges Edge1 and Edge2 in the tree are adjacent if they have a common node.
  • In other words, they are adjacent if Edge1 connects nodes a and b and Edge2 connects nodes b and c.
Read more
[AtCoder] A - Multiple ArrayRead more
[AtCoder] D - Walk and TeleportRead more
[AtCoder] C - Factors of FactorialRead more
[AtCoder] D - Card EaterRead more
[AtCoder] C - X: Yet Another Die GameRead more
[Codeforces] Round #663 (Div. 2) C. Cyclic PermutationsRead more
[Codeforces] Round #666 (Div. 2) B. Power SequenceRead more