2022-03-24
881. Boats to Save People
Topic: Array, Two Pointers, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an array
Return the minimum number of boats to carry every given person.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
881. Boats to Save People
Topic: Array, Two Pointers, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an array
people where people[i] is the weight of the i^th person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.Return the minimum number of boats to carry every given person.
Example 1:
Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)
Example 2:
Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)
Example 3:
Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)
Constraints:
•
1 <= people.length <= 5 * 10^4•
1 <= people[i] <= limit <= 3 * 10^42022-03-25
1029. Two City Scheduling
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
A company is planning to interview
Return the minimum cost to fly every person to a city such that exactly
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1029. Two City Scheduling
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
A company is planning to interview
2n people. Given the array costs where costs[i] = [aCost_i, bCost_i], the cost of flying the i^th person to city a is aCost_i, and the cost of flying the i^th person to city b is bCost_i.Return the minimum cost to fly every person to a city such that exactly
n people arrive in each city.Example 1:
Input: costs = [[10,20],[30,200],[400,50],[30,20]]
Output: 110
Explanation:
The first person goes to city A for a cost of 10.
The second person goes to city A for a cost of 30.
The third person goes to city B for a cost of 50.
The fourth person goes to city B for a cost of 20.
The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
Example 2:
Input: costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
Output: 1859
Example 3:
Input: costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
Output: 3086
Constraints:
•
2 * n == costs.length•
2 <= costs.length <= 100•
costs.length is even.•
1 <= aCost_i, bCost_i <= 10002022-03-26
704. Binary Search
Topic: Array, Binary Search
Difficulty: Easy
Problem:
Given an array of integers
You must write an algorithm with
Example 1:
Example 2:
Constraints:
•
•
• All the integers in
•
704. Binary Search
Topic: Array, Binary Search
Difficulty: Easy
Problem:
Given an array of integers
nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.You must write an algorithm with
O(log n) runtime complexity.Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Constraints:
•
1 <= nums.length <= 10^4•
-10^4 < nums[i], target < 10^4• All the integers in
nums are unique.•
nums is sorted in ascending order.2022-03-27
1337. The K Weakest Rows in a Matrix
Topic: Array, Binary Search, Sorting, Heap (Priority Queue), Matrix
Difficulty: Easy
Problem:
You are given an
A row
• The number of soldiers in row
• Both rows have the same number of soldiers and
Return the indices of the
Example 1:
Example 2:
Constraints:
•
•
•
•
•
1337. The K Weakest Rows in a Matrix
Topic: Array, Binary Search, Sorting, Heap (Priority Queue), Matrix
Difficulty: Easy
Problem:
You are given an
m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.A row
i is weaker than a row j if one of the following is true:• The number of soldiers in row
i is less than the number of soldiers in row j.• Both rows have the same number of soldiers and
i < j.Return the indices of the
k weakest rows in the matrix ordered from weakest to strongest.Example 1:
Input: mat =
[[1,1,0,0,0],
[1,1,1,1,0],
[1,0,0,0,0],
[1,1,0,0,0],
[1,1,1,1,1]],
k = 3
Output: [2,0,3]
Explanation:
The number of soldiers in each row is:
- Row 0: 2
- Row 1: 4
- Row 2: 1
- Row 3: 2
- Row 4: 5
The rows ordered from weakest to strongest are [2,0,3,1,4].
Example 2:
Input: mat =
[[1,0,0,0],
[1,1,1,1],
[1,0,0,0],
[1,0,0,0]],
k = 2
Output: [0,2]
Explanation:
The number of soldiers in each row is:
- Row 0: 1
- Row 1: 4
- Row 2: 1
- Row 3: 1
The rows ordered from weakest to strongest are [0,2,3,1].
Constraints:
•
m == mat.length•
n == mat[i].length•
2 <= n, m <= 100•
1 <= k <= m•
matrix[i][j] is either 0 or 1.2022-03-28
81. Search in Rotated Sorted Array II
Topic: Array, Binary Search
Difficulty: Medium
Problem:
There is an integer array
Before being passed to your function,
Given the array
You must decrease the overall operation steps as much as possible.
Example 1:
Example 2:
Constraints:
•
•
•
•
Follow up: This problem is similar to Search in Rotated Sorted Array, but
81. Search in Rotated Sorted Array II
Topic: Array, Binary Search
Difficulty: Medium
Problem:
There is an integer array
nums sorted in non-decreasing order (not necessarily with distinct values).Before being passed to your function,
nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4].Given the array
nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums.You must decrease the overall operation steps as much as possible.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Constraints:
•
1 <= nums.length <= 5000•
-10^4 <= nums[i] <= 10^4•
nums is guaranteed to be rotated at some pivot.•
-10^4 <= target <= 10^4Follow up: This problem is similar to Search in Rotated Sorted Array, but
nums may contain duplicates. Would this affect the runtime complexity? How and why?2022-03-29
287. Find the Duplicate Number
Topic: Array, Two Pointers, Binary Search, Bit Manipulation
Difficulty: Medium
Problem:
Given an array of integers
There is only one repeated number in
You must solve the problem without modifying the array
Example 1:
Example 2:
Constraints:
•
•
•
• All the integers in
Follow up:
• How can we prove that at least one duplicate number must exist in
• Can you solve the problem in linear runtime complexity?
287. Find the Duplicate Number
Topic: Array, Two Pointers, Binary Search, Bit Manipulation
Difficulty: Medium
Problem:
Given an array of integers
nums containing n + 1 integers where each integer is in the range [1, n] inclusive.There is only one repeated number in
nums, return this repeated number.You must solve the problem without modifying the array
nums and uses only constant extra space.Example 1:
Input: nums = [1,3,4,2,2]
Output: 2
Example 2:
Input: nums = [3,1,3,4,2]
Output: 3
Constraints:
•
1 <= n <= 10^5•
nums.length == n + 1•
1 <= nums[i] <= n• All the integers in
nums appear only once except for precisely one integer which appears two or more times.Follow up:
• How can we prove that at least one duplicate number must exist in
nums?• Can you solve the problem in linear runtime complexity?
2022-03-30
74. Search a 2D Matrix
Topic: Array, Binary Search, Matrix
Difficulty: Medium
Problem:
Write an efficient algorithm that searches for a value
• Integers in each row are sorted from left to right.
• The first integer of each row is greater than the last integer of the previous row.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg
Constraints:
•
•
•
•
74. Search a 2D Matrix
Topic: Array, Binary Search, Matrix
Difficulty: Medium
Problem:
Write an efficient algorithm that searches for a value
target in an m x n integer matrix matrix. This matrix has the following properties:• Integers in each row are sorted from left to right.
• The first integer of each row is greater than the last integer of the previous row.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat.jpg
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false
Constraints:
•
m == matrix.length•
n == matrix[i].length•
1 <= m, n <= 100•
-10^4 <= matrix[i][j], target <= 10^42022-03-31
410. Split Array Largest Sum
Topic: Array, Binary Search, Dynamic Programming, Greedy
Difficulty: Hard
Problem:
Given an array
Write an algorithm to minimize the largest sum among these
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
410. Split Array Largest Sum
Topic: Array, Binary Search, Dynamic Programming, Greedy
Difficulty: Hard
Problem:
Given an array
nums which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays.Write an algorithm to minimize the largest sum among these
m subarrays.Example 1:
Input: nums = [7,2,5,10,8], m = 2
Output: 18
Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.
Example 2:
Input: nums = [1,2,3,4,5], m = 2
Output: 9
Example 3:
Input: nums = [1,4,4], m = 3
Output: 4
Constraints:
•
1 <= nums.length <= 1000•
0 <= nums[i] <= 10^6•
1 <= m <= min(50, nums.length)2022-04-01
344. Reverse String
Topic: Two Pointers, String, Recursion
Difficulty: Easy
Problem:
Write a function that reverses a string. The input string is given as an array of characters
You must do this by modifying the input array in-place with
Example 1:
Example 2:
Constraints:
•
•
344. Reverse String
Topic: Two Pointers, String, Recursion
Difficulty: Easy
Problem:
Write a function that reverses a string. The input string is given as an array of characters
s.You must do this by modifying the input array in-place with
O(1) extra memory.Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
•
1 <= s.length <= 10^5•
s[i] is a printable ascii character.2022-04-02
680. Valid Palindrome II
Topic: Two Pointers, String, Greedy
Difficulty: Easy
Problem:
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
680. Valid Palindrome II
Topic: Two Pointers, String, Greedy
Difficulty: Easy
Problem:
Given a string
s, return true if the s can be palindrome after deleting at most one character from it.Example 1:
Input: s = "aba"
Output: true
Example 2:
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
Example 3:
Input: s = "abc"
Output: false
Constraints:
•
1 <= s.length <= 10^5•
s consists of lowercase English letters.2022-04-03
31. Next Permutation
Topic: Array, Two Pointers
Difficulty: Medium
Problem:
A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
• For example, for
The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
• For example, the next permutation of
• Similarly, the next permutation of
• While the next permutation of
Given an array of integers
The replacement must be in place and use only constant extra memory.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
31. Next Permutation
Topic: Array, Two Pointers
Difficulty: Medium
Problem:
A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
• For example, for
arr = [1,2,3], the following are considered permutations of arr: [1,2,3], [1,3,2], [3,1,2], [2,3,1].The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
• For example, the next permutation of
arr = [1,2,3] is [1,3,2].• Similarly, the next permutation of
arr = [2,3,1] is [3,1,2].• While the next permutation of
arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.Given an array of integers
nums, find the next permutation of nums.The replacement must be in place and use only constant extra memory.
Example 1:
Input: nums = [1,2,3]
Output: [1,3,2]
Example 2:
Input: nums = [3,2,1]
Output: [1,2,3]
Example 3:
Input: nums = [1,1,5]
Output: [1,5,1]
Constraints:
•
1 <= nums.length <= 100•
0 <= nums[i] <= 1002022-04-04
1721. Swapping Nodes in a Linked List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
You are given the
Return the head of the linked list after swapping the values of the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/21/linked1.jpg
Example 2:
Constraints:
• The number of nodes in the list is
•
•
1721. Swapping Nodes in a Linked List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
You are given the
head of a linked list, and an integer k.Return the head of the linked list after swapping the values of the
k^th node from the beginning and the k^th node from the end (the list is 1-indexed).Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/21/linked1.jpg
Input: head = [1,2,3,4,5], k = 2
Output: [1,4,3,2,5]
Example 2:
Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
Output: [7,9,6,6,8,7,3,0,9,5]
Constraints:
• The number of nodes in the list is
n.•
1 <= k <= n <= 10^5•
0 <= Node.val <= 1002022-04-05
11. Container With Most Water
Topic: Array, Two Pointers, Greedy
Difficulty: Medium
Problem:
You are given an integer array
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg
Example 2:
Constraints:
•
•
•
11. Container With Most Water
Topic: Array, Two Pointers, Greedy
Difficulty: Medium
Problem:
You are given an integer array
height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i]).Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example 2:
Input: height = [1,1]
Output: 1
Constraints:
•
n == height.length•
2 <= n <= 10^5•
0 <= height[i] <= 10^42022-04-06
923. 3Sum With Multiplicity
Topic: Array, Hash Table, Two Pointers, Sorting, Counting
Difficulty: Medium
Problem:
Given an integer array
As the answer can be very large, return it modulo
Example 1:
Example 2:
Constraints:
•
•
•
923. 3Sum With Multiplicity
Topic: Array, Hash Table, Two Pointers, Sorting, Counting
Difficulty: Medium
Problem:
Given an integer array
arr, and an integer target, return the number of tuples i, j, k such that i < j < k and arr[i] + arr[j] + arr[k] == target.As the answer can be very large, return it modulo
10^9 + 7.Example 1:
Input: arr = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Explanation:
Enumerating by the values (arr[i], arr[j], arr[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.
Example 2:
Input: arr = [1,1,2,2,2,2], target = 5
Output: 12
Explanation:
arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.
Constraints:
•
3 <= arr.length <= 3000•
0 <= arr[i] <= 100•
0 <= target <= 3002022-04-07
1046. Last Stone Weight
Topic: Array, Heap (Priority Queue)
Difficulty: Easy
Problem:
You are given an array of integers
We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights
• If
• If
At the end of the game, there is at most one stone left.
Return the smallest possible weight of the left stone. If there are no stones left, return
Example 1:
Example 2:
Constraints:
•
•
1046. Last Stone Weight
Topic: Array, Heap (Priority Queue)
Difficulty: Easy
Problem:
You are given an array of integers
stones where stones[i] is the weight of the i^th stone.We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights
x and y with x <= y. The result of this smash is:• If
x == y, both stones are destroyed, and• If
x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.At the end of the game, there is at most one stone left.
Return the smallest possible weight of the left stone. If there are no stones left, return
0.Example 1:
Input: stones = [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
Example 2:
Input: stones = [1]
Output: 1
Constraints:
•
1 <= stones.length <= 30•
1 <= stones[i] <= 10002022-04-08
703. Kth Largest Element in a Stream
Topic: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream
Difficulty: Easy
Problem:
Design a class to find the
Implement
•
•
Example 1:
Constraints:
•
•
•
•
• At most
• It is guaranteed that there will be at least
703. Kth Largest Element in a Stream
Topic: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream
Difficulty: Easy
Problem:
Design a class to find the
k^th largest element in a stream. Note that it is the k^th largest element in the sorted order, not the k^th distinct element.Implement
KthLargest class:•
KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.•
int add(int val) Appends the integer val to the stream and returns the element representing the k^th largest element in the stream.Example 1:
Input
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output
[null, 4, 5, 5, 8, 8]
Explanation
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
Constraints:
•
1 <= k <= 10^4•
0 <= nums.length <= 10^4•
-10^4 <= nums[i] <= 10^4•
-10^4 <= val <= 10^4• At most
10^4 calls will be made to add.• It is guaranteed that there will be at least
k elements in the array when you search for the k^th element.2022-04-09
347. Top K Frequent Elements
Topic: Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect
Difficulty: Medium
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
• It is guaranteed that the answer is unique.
Follow up: Your algorithm's time complexity must be better than
347. Top K Frequent Elements
Topic: Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect
Difficulty: Medium
Problem:
Given an integer array
nums and an integer k, return the k most frequent elements. You may return the answer in any order.Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
Constraints:
•
1 <= nums.length <= 10^5•
k is in the range [1, the number of unique elements in the array].• It is guaranteed that the answer is unique.
Follow up: Your algorithm's time complexity must be better than
O(n log n), where n is the array's size.2022-04-10
682. Baseball Game
Topic: Array, Stack, Simulation
Difficulty: Easy
Problem:
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
At the beginning of the game, you start with an empty record. You are given a list of strings
1. An integer
2.
3.
4.
Return the sum of all the scores on the record.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• For operation
• For operations
682. Baseball Game
Topic: Array, Stack, Simulation
Difficulty: Easy
Problem:
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
At the beginning of the game, you start with an empty record. You are given a list of strings
ops, where ops[i] is the i^th operation you must apply to the record and is one of the following:1. An integer
x - Record a new score of x.2.
"+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.3.
"D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.4.
"C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.Return the sum of all the scores on the record.
Example 1:
Input: ops = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.
Example 2:
Input: ops = ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
"5" - Add 5 to the record, record is now [5].
"-2" - Add -2 to the record, record is now [5, -2].
"4" - Add 4 to the record, record is now [5, -2, 4].
"C" - Invalidate and remove the previous score, record is now [5, -2].
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
Example 3:
Input: ops = ["1"]
Output: 1
Constraints:
•
1 <= ops.length <= 1000•
ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 10^4, 3 * 10^4].• For operation
"+", there will always be at least two previous scores on the record.• For operations
"C" and "D", there will always be at least one previous score on the record.2022-04-11
1260. Shift 2D Grid
Topic: Array, Matrix, Simulation
Difficulty: Easy
Problem:
Given a 2D
In one shift operation:
• Element at
• Element at
• Element at
Return the 2D grid after applying shift operation
Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/05/e1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2019/11/05/e2.png
Example 3:
Constraints:
•
•
•
•
•
•
1260. Shift 2D Grid
Topic: Array, Matrix, Simulation
Difficulty: Easy
Problem:
Given a 2D
grid of size m x n and an integer k. You need to shift the grid k times.In one shift operation:
• Element at
grid[i][j] moves to grid[i][j + 1].• Element at
grid[i][n - 1] moves to grid[i + 1][0].• Element at
grid[m - 1][n - 1] moves to grid[0][0].Return the 2D grid after applying shift operation
k times.Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/05/e1.png
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[9,1,2],[3,4,5],[6,7,8]]
Example 2:
Image: https://assets.leetcode.com/uploads/2019/11/05/e2.png
Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
Example 3:
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output: [[1,2,3],[4,5,6],[7,8,9]]
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m <= 50•
1 <= n <= 50•
-1000 <= grid[i][j] <= 1000•
0 <= k <= 1002022-04-12
289. Game of Life
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
The board is made up of an
1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by over-population.
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg
Constraints:
•
•
•
•
Follow up:
• Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
• In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
289. Game of Life
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
The board is made up of an
m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by over-population.
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the
m x n grid board, return the next state.Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg
Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]
Constraints:
•
m == board.length•
n == board[i].length•
1 <= m, n <= 25•
board[i][j] is 0 or 1.Follow up:
• Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
• In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?