[LeetCode] The Earliest and Latest Rounds Where Players Compete

1900. The Earliest and Latest Rounds Where Players Compete

There is a tournament where n players are participating. The players are standing in a single row and are numbered from 1 to n based on their initial standing position (player 1 is the first player in the row, player 2 is the second player in the row, etc.).

The tournament consists of multiple rounds (starting from round number 1). In each round, the ith player from the front of the row competes against the ith player from the end of the row, and the winner advances to the next round. When the number of players is odd for the current round, the player in the middle automatically advances to the next round.

  • For example, if the row consists of players 1, 2, 4, 6, 7
  • Player 1 competes against player 7.
  • Player 2 competes against player 6.
  • Player 4 automatically advances to the next round.

After each round is over, the winners are lined back up in the row based on the original ordering assigned to them initially (ascending order).

The players numbered firstPlayer and secondPlayer are the best in the tournament. They can win against any other player before they compete against each other. If any two other players compete against each other, either of them might win, and thus you may choose the outcome of this round.

Given the integers n, firstPlayer, and secondPlayer, return an integer array containing two values, the earliest possible round number and the latest possible round number in which these two players will compete against each other, respectively.

Read more
[LeetCode] Next Palindrome Using Same Digits

1842. Next Palindrome Using Same Digits

You are given a numeric string num, representing a very large palindrome.

Return the smallest palindrome larger than num that can be created by rearranging its digits. If no such palindrome exists, return an empty string “”.

A palindrome is a number that reads the same backward as forward.

Read more
[LeetCode] Remove Digit From Number to Maximize Result

2259. Remove Digit From Number to Maximize Result

You are given a string number representing a positive integer and a character digit.

Return the resulting string after removing exactly one occurrence of digit from number such that the value of the resulting string in decimal form is maximized. The test cases are generated such that digit occurs at least once in number.

Read more
[LeetCode] Minimum Consecutive Cards to Pick Up

2260. Minimum Consecutive Cards to Pick Up

You are given an integer array cards where cards[i] represents the value of the ith card. A pair of cards are matching if the cards have the same value.

Return the minimum number of consecutive cards you have to pick up to have a pair of matching cards among the picked cards. If it is impossible to have matching cards, return -1.

Read more
[LeetCode] K Divisible Elements Subarrays

2261. K Divisible Elements Subarrays

Given an integer array nums and two integers k and p, return the number of distinct subarrays which have at most k elements divisible by p.

Two arrays nums1 and nums2 are said to be distinct if:

  • They are of different lengths, or
  • There exists at least one index i where nums1[i] != nums2[i].

A subarray is defined as a non-empty contiguous sequence of elements in an array.

Read more
[LeetCode] Total Appeal of A String

2262. Total Appeal of A String

The appeal of a string is the number of distinct characters found in the string.

  • For example, the appeal of “abbca” is 3 because it has 3 distinct characters: ‘a’, ‘b’, and ‘c’.
    Given a string s, return the total appeal of all of its substrings.

A substring is a contiguous sequence of characters within a string.

Read more
[LeetCode] Design Excel Sum Formula

631. Design Excel Sum Formula

Design the basic function of Excel and implement the function of the sum formula.

Implement the Excel class:

  • Excel(int height, char width) Initializes the object with the height and the width of the sheet. The sheet is an integer matrix mat of size height x width with the row index in the range [1, height] and the column index in the range [‘A’, width]. All the values should be zero initially.
  • void set(int row, char column, int val) Changes the value at mat[row][column] to be val.
  • int get(int row, char column) Returns the value at mat[row][column].
  • int sum(int row, char column, List numbers) Sets the value at mat[row][column] to be the sum of cells represented by numbers and returns the value at mat[row][column]. This sum formula should exist until this cell is overlapped by another value or another sum formula. numbers[i] could be on the format:
  • “ColRow” that represents a single cell.
  • For example, “F7” represents the cell mat[7][‘F’].
  • “ColRow1:ColRow2” that represents a range of cells. The range will always be a rectangle where “ColRow1” represent the position of the top-left cell, and “ColRow2” represents the position of the bottom-right cell.
  • For example, “B3:F7” represents the cells mat[i][j] for 3 <= i <= 7 and ‘B’ <= j <= ‘F’.

Note: You could assume that there will not be any circular sum reference.

  • For example, mat[1][‘A’] == sum(1, “B”) and mat[1][‘B’] == sum(1, “A”).
Read more
[LeetCode] Design Video Sharing Platform

2254. Design Video Sharing Platform

You have a video sharing platform where users can upload and delete videos. Each video is a string of digits, where the ith digit of the string represents the content of the video at minute i. For example, the first digit represents the content at minute 0 in the video, the second digit represents the content at minute 1 in the video, and so on. Viewers of videos can also like and dislike videos. Internally, the platform keeps track of the number of views, likes, and dislikes on each video.

When a video is uploaded, it is associated with the smallest available integer videoId starting from 0. Once a video is deleted, the videoId associated with that video can be reused for another video.

Implement the VideoSharingPlatform class:

  • VideoSharingPlatform() Initializes the object.
  • int upload(String video) The user uploads a video. Return the videoId associated with the video.
  • void remove(int videoId) If there is a video associated with videoId, remove the video.
  • String watch(int videoId, int startMinute, int endMinute) If there is a video associated with videoId, increase the number of views on the video by 1 and return the substring of the video string starting at startMinute and ending at min(endMinute, video.length - 1) (inclusive). Otherwise, return “-1”.
  • void like(int videoId) Increases the number of likes on the video associated with videoId by 1 if there is a video associated with videoId.
  • void dislike(int videoId) Increases the number of dislikes on the video associated with videoId by 1 if there is a video associated with videoId.
  • int[] getLikesAndDislikes(int videoId) Return a 0-indexed integer array values of length 2 where values[0] is the number of likes and values[1] is the number of dislikes on the video associated with videoId. If there is no video associated with videoId, return [-1].
  • int getViews(int videoId) Return the number of views on the video associated with videoId, if there is no video associated with videoId, return -1.
Read more
[Code Jam 2022 Round 1C] SquarRead more
[LeetCode] Escape the Spreading Fire

2258. Escape the Spreading Fire

You are given a 0-indexed 2D integer array grid of size m x n which represents a field. Each cell has one of three values:

  • 0 represents grass,
  • 1 represents fire,
  • 2 represents a wall that you and fire cannot pass through.

You are situated in the top-left cell, (0, 0), and you want to travel to the safehouse at the bottom-right cell, (m - 1, n - 1). Every minute, you may move to an adjacent grass cell. After your move, every fire cell will spread to all adjacent cells that are not walls.

Return the maximum number of minutes that you can stay in your initial position before moving while still safely reaching the safehouse. If this is impossible, return -1. If you can always reach the safehouse regardless of the minutes stayed, return 109.

Note that even if the fire spreads to the safehouse immediately after you have reached it, it will be counted as safely reaching the safehouse.

A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).

Read more