Leetcode#80. Remove Duplicates from Sorted Array II
ProblemGiven an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result ...
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#26. Remove Duplicates from Sorted Array
ProblemGiven an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums ini ...
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#1493. Longest Subarray of 1's After Deleting One Element
ProblemGiven a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1‘s in the resulting array. Return 0 if there is no such subarray.
Example 1:
1234Input: nums = [1,1,0,1]Output: 3Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
1234Input: nums = [0,1,1,1,0,1,1,0,1]Output: 5Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest suba ...
Leetcode#643. Maximum Average Subarray I
ProblemYou are given an integer array nums consisting of n elements, and an integer k.
Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.
Example 1:
1234Input: nums = [1,12,-5,-6,50,3], k = 4Output: 12.75000Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
Example 2:
123Input: nums = [5], k = 1Output: 5.00000
Constraints:
n == nums.length
1 < ...
Leetcode#283. Move Zeroes
ProblemGiven an integer array nums, move all 0‘s to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
123Input: nums = [0,1,0,3,12]Output: [1,3,12,0,0]
Example 2:
123Input: nums = [0]Output: [0]
Constraints:
1 <= nums.length <= 10^4
2^31 <= nums[i] <= 2^31 - 1
Follow up:
Could you minimize the total number of operations done?
Solve123456789101112class Solution: def mov ...
Leetcode#435. Non-overlapping Intervals
ProblemGiven an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Example 1:
1234Input: intervals = [[1,2],[2,3],[3,4],[1,3]]Output: 1Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
Example 2:
1234Input: intervals = [[1,2],[1,2],[1,2]]Output: 2Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
...