Leetcode#855. Exam Room
ProblemThere is an exam room with n seats in a single row labeled from 0 to n - 1.
When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. If no one is in the room, then the student sits at seat number 0.
Design a class that simulates the mentioned exam room.
Implement the ExamRoom class:
ExamRoom(int n) Initializes the object of the exam room with the number of th ...
Leetcode#862. Shortest Subarray with Sum at Least K
ProblemGiven an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1.
A subarray is a contiguous part of an array.
Example 1:
123Input: nums = [1], k = 1Output: 1
Example 2:
123Input: nums = [1,2], k = 4Output: -1
Example 3:
123Input: nums = [2,-1,2], k = 3Output: 3
Constraints:
1 <= nums.length <= 10^5
10^5 <= nums[i] <= 10^5
1 <= k <= 10^9
Solve想破頭 簡單說就是 找到第一組解後 ...
Leetcode#950. Reveal Cards In Increasing Order
ProblemYou are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].
You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.
You will do the following steps repeatedly until all cards are revealed:
Take the top card of the deck, reveal it, and take it out of the deck.
If there are still cards in the deck then put the next top card of the deck at the bottom of t ...
Leetcode#909. Snakes and Ladders
ProblemYou are given an n x n integer matrix board where the cells are labeled from 1 to n^2 in a Boustrophedon style starting from the bottom left of the board (i.e. board[n - 1][0]) and alternating direction each row.
You start on square 1 of the board. In each move, starting from square curr, do the following:
Choose a destination square next with a label in the range [curr + 1, min(curr + 6, n^2)].
This choice simulates the result of a standard 6-sided die roll: i.e., there are always at mo ...
Leetcode#76. Minimum Window Substring
ProblemGiven two strings s and t of lengths m and n respectively, return the minimum window
substring
of
1s
such that every character in
1t
(including duplicates) is included in the window
. If there is no such substring, return
the empty string
1""
.
The testcases will be generated such that the answer is unique.
Example 1:
1234Input: s = "ADOBECODEBANC", t = "ABC"Output: "BANC"Explanation: The minimum window substring "BANC" includes 'A ...
Leetcode#684. Redundant Connection
ProblemIn this problem, a tree is an undirected graph that is connected and has no cycles.
You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.
Return an edge that can be removed so that ...
Leetcode#200. Number of Islands
ProblemGiven an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
12345678Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"] ...
Leetcode#5. Longest Palindromic Substring
ProblemGiven a string s, return the longest
palindromic
substring
in
1s
.
Example 1:
1234Input: s = "babad"Output: "bab"Explanation: "aba" is also a valid answer.
Example 2:
123Input: s = "cbbd"Output: "bb"
Constraints:
1 <= s.length <= 1000
s consist of only digits and English letters.
Solve123456789101112131415161718192021class Solution: def longestPalindrome(self, s: str) -> str: res = "" for i in range ...
Leetcode#124. Binary Tree Maximum Path Sum
ProblemA path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
The path sum of a path is the sum of the node’s values in the path.
Given the root of a binary tree, return the maximum path sum of any non-empty path.
Example 1:
!https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg
1234Input: root = [1,2,3]Output: 6 ...
Leetcode#841. Keys and Rooms
ProblemThere 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 ...