[LeetCode] 2279. Maximum Bags With Full Capacity of Rocks

2279. Maximum Bags With Full Capacity of Rocks Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Greedy、Sorting 一、題目 You have n bags numbered from 0 to n - 1. You are given two 0-indexed integer arrays capacity and rocks. The ith bag can hold a maximum of capacity[i] rocks and currently contains rocks[i] rocks. You are also given an integer additionalRocks, the number of additional rocks you can place in any of the bags. Return the maximum number of bags that could have full capacity after placing the additional rocks in some bags. ...

December 27, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2389. Longest Subsequence With Limited Sum

2389. Longest Subsequence With Limited Sum Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Array、Binary Search、Greedy、Sorting、Prefix Sum 一、題目 You are given an integer array nums of length n, and an integer array queries of length m. Return an array answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such that the sum of its elements is less than or equal to queries[i]. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. ...

December 25, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 790. Domino and Tromino Tiling

790. Domino and Tromino Tiling Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Dynamic Programming 一、題目 You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes. Given an integer n, return the number of ways to tile an 2 x n board. Since the answer may be very large, return it modulo 10^9 + 7. In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile. ...

December 24, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 841. Keys and Rooms

841. Keys and Rooms Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Depth-First Search、Breadth-First Search、Graph 一、題目 There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key. When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms. Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return true if you can visit all the rooms, or false otherwise. ...

December 20, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 25. Reverse Nodes in k-Group

25. Reverse Nodes in k-Group Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: Linked List、Recursion 一、題目 Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. You may not alter the values in the list’s nodes, only nodes themselves may be changed. ...

December 16, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 24. Swap Nodes in Pairs

24. Swap Nodes in Pairs Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Linked List、Recursion 一、題目 Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.) Example 1: Input: head = [1,2,3,4] Output: [2,1,4,3] Example 2: Input: head = [] Output: [] Example 3: Input: head = [1] Output: [1] Constraints: ...

December 14, 2022 · 1 分鐘 · Rain Hu

[LeetCode] 931. Minimum Falling Path Sum

931. Minimum Falling Path Sum Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Dynamic Programming、Matrix 一、題目 Given an n x n array of integers matrix, return the minimum sum of any falling path through matrix. A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1). ...

December 13, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 70. Climbing Stairs

70. Climbing Stairs Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Math、Dynamic Programming、Memoization 一、題目 You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Example 1: Input: n = 2 Output: 2 Explanation: There are two ways to climb to the top. 1 step + 1 step 2 steps Example 2: ...

December 12, 2022 · 1 分鐘 · Rain Hu

[LeetCode] 2501. Longest Square Streak in an Array

2501. Longest Square Streak in an Array Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Hash Table、Binary Search、Dynamic Programming、Sorting \(\color{blue}\textsf{Weekly Contest 323}\) 一、題目 You are given an integer array nums. A subsequence of nums is called a square streak if: The length of the subsequence is at least 2, and after sorting the subsequence, each element (except the first element) is the square of the previous number. Return the length of the longest square streak in nums, or return -1 if there is no square streak. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Example 1: ...

December 11, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2500. Delete Greatest Value in Each Row

2500. Delete Greatest Value in Each Row Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Array、Hash Table、Binary Search、Dynamic Programming、Sorting \(\color{blue}\textsf{Weekly Contest 323}\) 一、題目 You are given an m x n matrix grid consisting of positive integers. Perform the following operation until grid becomes empty: Delete the element with the greatest value from each row. If multiple such elements exist, delete any of them. Add the maximum of deleted elements to the answer. Note that the number of columns decreases by one after each operation. Return the answer after performing the operations described above. Example 1: ...

December 11, 2022 · 2 分鐘 · Rain Hu