diff --git a/0001.two-sum/two-sum.cpp b/0001.two-sum/two-sum.cpp deleted file mode 100644 index b2f69fd..0000000 --- a/0001.two-sum/two-sum.cpp +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - unordered_map m; - vector res; - for (int i = 0; i < nums.size(); i++){ - int temp = target - nums[i]; - if (m.count(temp)){ - res.push_back(m[temp]); - res.push_back(i); - break; - } - m[nums[i]] = i; - } - return res; - } -}; \ No newline at end of file diff --git a/0001.two-sum/two-sum.md b/0001.two-sum/two-sum.md deleted file mode 100644 index 8457633..0000000 --- a/0001.two-sum/two-sum.md +++ /dev/null @@ -1,14 +0,0 @@ -

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

- -

You may assume that each input would have exactly one solution, and you may not use the same element twice.

- -

Example:

- -
-Given nums = [2, 7, 11, 15], target = 9,
-
-Because nums[0] + nums[1] = 2 + 7 = 9,
-return [0, 1].
-
- -

 

diff --git a/0001.two-sum/two-sum.py b/0001.two-sum/two-sum.py deleted file mode 100644 index f328bda..0000000 --- a/0001.two-sum/two-sum.py +++ /dev/null @@ -1,12 +0,0 @@ -class Solution(object): - def twoSum(self, nums, target): - """ - :type nums: List[int] - :type target: int - :rtype: List[int] - """ - for i in range(len(nums)): - for j in range(i + 1, len(nums)): - if nums[i] + nums[j] == target: - return [i,j] - \ No newline at end of file diff --git a/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.md b/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.md deleted file mode 100644 index 5392ce0..0000000 --- a/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.md +++ /dev/null @@ -1,23 +0,0 @@ -

There are two sorted arrays nums1 and nums2 of size m and n respectively.

- -

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

- -

You may assume nums1 and nums2 cannot be both empty.

- -

Example 1:

- -
-nums1 = [1, 3]
-nums2 = [2]
-
-The median is 2.0
-
- -

Example 2:

- -
-nums1 = [1, 2]
-nums2 = [3, 4]
-
-The median is (2 + 3)/2 = 2.5
-
diff --git a/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.py b/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.py deleted file mode 100644 index 5dbbd99..0000000 --- a/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.py +++ /dev/null @@ -1,57 +0,0 @@ -from heapq import * -class MedianFinder(object): -# 维护两个堆,一个大顶堆,一个小顶堆,小顶堆里的数比大顶堆里的数都要大, -# 如果有两个潜在的中位数(两个堆size相同),数据流的中位数就是两个堆顶之和除以2 -# 如果只有一个中位数,就看size更小的那个堆的堆顶 -# 如果新进来的数比小顶堆的数要小,就把它插入大顶堆 -# 如果新进来的数比小顶堆的数要大,就把它插入小顶堆 -# 调整两个堆,使得size 差最大为1 - def __init__(self): - """ - initialize your data structure here. - """ - self.max_h = list() - self.min_h = list() - heapify(self.max_h) - heapify(self.min_h) - - - def addNum(self, num): - """ - :type num: int - :rtype: None - """ - heappush(self.min_h, num) - heappush(self.max_h, -heappop(self.min_h)) - if len(self.max_h) > len(self.min_h): - heappush(self.min_h, -heappop(self.max_h)) - - def findMedian(self): - """ - :rtype: float - """ - max_len = len(self.max_h) - min_len = len(self.min_h) - if max_len == min_len: #有两个候选中位数 - return (self.min_h[0] + -self.max_h[0]) / 2. - else:#小顶堆的size 一定 >= 大顶堆的size,所以答案就是小顶堆的堆顶 - return self.min_h[0] / 1. - -# Your MedianFinder object will be instantiated and called as such: -# obj = MedianFinder() -# obj.addNum(num) -# param_2 = obj.findMedian() - -class Solution(object): - def findMedianSortedArrays(self, nums1, nums2): - """ - :type nums1: List[int] - :type nums2: List[int] - :rtype: float - """ - mf = MedianFinder() - for num in nums1: - mf.addNum(num) - for num in nums2: - mf.addNum(num) - return mf.findMedian() \ No newline at end of file diff --git a/0029.divide-two-integers/divide-two-integers.md b/0029.divide-two-integers/divide-two-integers.md deleted file mode 100644 index 3c1b6d6..0000000 --- a/0029.divide-two-integers/divide-two-integers.md +++ /dev/null @@ -1,25 +0,0 @@ -

Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.

- -

Return the quotient after dividing dividend by divisor.

- -

The integer division should truncate toward zero.

- -

Example 1:

- -
-Input: dividend = 10, divisor = 3
-Output: 3
- -

Example 2:

- -
-Input: dividend = 7, divisor = -3
-Output: -2
- -

Note:

- -
    -
  • Both dividend and divisor will be 32-bit signed integers.
  • -
  • The divisor will never be 0.
  • -
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.
  • -
diff --git a/0029.divide-two-integers/divide-two-integers.py b/0029.divide-two-integers/divide-two-integers.py deleted file mode 100644 index e89b241..0000000 --- a/0029.divide-two-integers/divide-two-integers.py +++ /dev/null @@ -1,31 +0,0 @@ -class Solution(object): - def divide(self, dividend, divisor): - """ - :type dividend: int - :type divisor: int - :rtype: int - """ - # 计算被除数可以减去多少个除数: - op = 1 - if (dividend > 0 and divisor < 0) or (dividend < 0 and divisor > 0): - op = -1 - - dividend, divisor = abs(dividend), abs(divisor) - res = 0 - # cnt = 1 - while(dividend >= divisor): - multidivisor, multi = divisor, 1 - while(dividend >= multidivisor): - res += multi - dividend -= multidivisor - multi = multi << 1 - multidivisor = multidivisor <<1 - print dividend, multidivisor, multi - print multi - - - INT_MIN = -(2 **31) - INT_MAX = 2 **31 - 1 - res *= op - - return res if INT_MIN <= res <= INT_MAX else INT_MAX \ No newline at end of file diff --git a/0036.valid-sudoku/valid-sudoku.md b/0036.valid-sudoku/valid-sudoku.md deleted file mode 100644 index 19504c0..0000000 --- a/0036.valid-sudoku/valid-sudoku.md +++ /dev/null @@ -1,59 +0,0 @@ -

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

- -
    -
  1. Each row must contain the digits 1-9 without repetition.
  2. -
  3. Each column must contain the digits 1-9 without repetition.
  4. -
  5. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
  6. -
- -


-A partially filled sudoku which is valid.

- -

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

- -

Example 1:

- -
-Input:
-[
-  ["5","3",".",".","7",".",".",".","."],
-  ["6",".",".","1","9","5",".",".","."],
-  [".","9","8",".",".",".",".","6","."],
-  ["8",".",".",".","6",".",".",".","3"],
-  ["4",".",".","8",".","3",".",".","1"],
-  ["7",".",".",".","2",".",".",".","6"],
-  [".","6",".",".",".",".","2","8","."],
-  [".",".",".","4","1","9",".",".","5"],
-  [".",".",".",".","8",".",".","7","9"]
-]
-Output: true
-
- -

Example 2:

- -
-Input:
-[
-  ["8","3",".",".","7",".",".",".","."],
-  ["6",".",".","1","9","5",".",".","."],
-  [".","9","8",".",".",".",".","6","."],
-  ["8",".",".",".","6",".",".",".","3"],
-  ["4",".",".","8",".","3",".",".","1"],
-  ["7",".",".",".","2",".",".",".","6"],
-  [".","6",".",".",".",".","2","8","."],
-  [".",".",".","4","1","9",".",".","5"],
-  [".",".",".",".","8",".",".","7","9"]
-]
-Output: false
-Explanation: Same as Example 1, except with the 5 in the top left corner being 
-    modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
-
- -

Note:

- -
    -
  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • -
  • Only the filled cells need to be validated according to the mentioned rules.
  • -
  • The given board contain only digits 1-9 and the character '.'.
  • -
  • The given board size is always 9x9.
  • -
diff --git a/0036.valid-sudoku/valid-sudoku.py b/0036.valid-sudoku/valid-sudoku.py deleted file mode 100644 index a65dde4..0000000 --- a/0036.valid-sudoku/valid-sudoku.py +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def isValidSudoku(self, board): - """ - :type board: List[List[str]] - :rtype: bool - """ - from collections import defaultdict - row, column, squre = defaultdict(set), defaultdict(set), defaultdict(set) - - for i in range(9): - for j in range(9): - if board[i][j].isdigit(): #排除掉空的情况 - if board[i][j] in row[i] or board[i][j] in column[j] or (board[i][j]) in squre[(i // 3, j // 3)]: - # print board[i][j], row[i], column[j], squre[(i // 3, j // 3)] - return False - else: - row[i].add(board[i][j]) - column[j].add(board[i][j]) - squre[(i // 3, j // 3)].add(board[i][j]) - # print row[1] - return True \ No newline at end of file diff --git a/0037.sudoku-solver/sudoku-solver.md b/0037.sudoku-solver/sudoku-solver.md deleted file mode 100644 index 827ae0c..0000000 --- a/0037.sudoku-solver/sudoku-solver.md +++ /dev/null @@ -1,25 +0,0 @@ -

Write a program to solve a Sudoku puzzle by filling the empty cells.

- -

A sudoku solution must satisfy all of the following rules:

- -
    -
  1. Each of the digits 1-9 must occur exactly once in each row.
  2. -
  3. Each of the digits 1-9 must occur exactly once in each column.
  4. -
  5. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
  6. -
- -

Empty cells are indicated by the character '.'.

- -


-A sudoku puzzle...

- -


-...and its solution numbers marked in red.

- -

Note:

- -
    -
  • The given board contain only digits 1-9 and the character '.'.
  • -
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • -
  • The given board size is always 9x9.
  • -
diff --git a/0037.sudoku-solver/sudoku-solver.py b/0037.sudoku-solver/sudoku-solver.py deleted file mode 100644 index f598eae..0000000 --- a/0037.sudoku-solver/sudoku-solver.py +++ /dev/null @@ -1,45 +0,0 @@ -class Solution(object): - def solveSudoku(self, board): - """ - :type board: List[List[str]] - :rtype: None Do not return anything, modify board in-place instead. - """ - from collections import defaultdict - row, column, squre = defaultdict(set), defaultdict(set), defaultdict(set) - fill_list = [] - for i in range(9): - for j in range(9): - if board[i][j].isdigit(): #排除掉空的情况 - row[i].add(board[i][j].encode("utf-8")) - column[j].add(board[i][j].encode("utf-8")) - squre[(i // 3, j // 3)].add(board[i][j].encode("utf-8")) - else: - fill_list.append([i, j]) - - self.result = [] - def backtrack(idx): - if idx == len(fill_list): - for row1 in board: - self.result.append(row1[:]) - return - if not self.result: - i, j = fill_list[idx][0], fill_list[idx][1] - for digit in range(1, 10): - if str(digit) in row[i] or str(digit) in column[j] or str(digit) in squre[(i // 3, j // 3)]: - continue - - board[i][j] = str(digit) - row[i].add(board[i][j]) - column[j].add(board[i][j]) - squre[(i // 3, j // 3)].add(board[i][j]) - - backtrack(idx + 1) - - row[i].remove(board[i][j]) - column[j].remove(board[i][j]) - squre[(i // 3, j // 3)].remove(board[i][j]) - - backtrack(0) - for i in range(9): - for j in range(9): - board[i][j] = self.result[i][j] diff --git a/0041.first-missing-positive/first-missing-positive.md b/0041.first-missing-positive/first-missing-positive.md deleted file mode 100644 index b1879c7..0000000 --- a/0041.first-missing-positive/first-missing-positive.md +++ /dev/null @@ -1,26 +0,0 @@ -

Given an unsorted integer array, find the smallest missing positive integer.

- -

Example 1:

- -
-Input: [1,2,0]
-Output: 3
-
- -

Example 2:

- -
-Input: [3,4,-1,1]
-Output: 2
-
- -

Example 3:

- -
-Input: [7,8,9,11,12]
-Output: 1
-
- -

Note:

- -

Your algorithm should run in O(n) time and uses constant extra space.

diff --git a/0041.first-missing-positive/first-missing-positive.py b/0041.first-missing-positive/first-missing-positive.py deleted file mode 100644 index abcda2e..0000000 --- a/0041.first-missing-positive/first-missing-positive.py +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def firstMissingPositive(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - # 对于处于1~n之间的数,把它们放到nums[i - 1]的位置上 - - for i in range(len(nums)): - while 1 <= nums[i] <= len(nums) and nums[i] != nums[nums[i] - 1]: - # nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i] - nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1] - - for i, x in enumerate(nums): - if x != i + 1: - return i + 1 - - return len(nums) + 1 \ No newline at end of file diff --git a/0042.trapping-rain-water/trapping-rain-water.md b/0042.trapping-rain-water/trapping-rain-water.md deleted file mode 100644 index abc422e..0000000 --- a/0042.trapping-rain-water/trapping-rain-water.md +++ /dev/null @@ -1,10 +0,0 @@ -

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

- -


-The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

- -

Example:

- -
-Input: [0,1,0,2,1,0,1,3,2,1,2,1]
-Output: 6
diff --git a/0042.trapping-rain-water/trapping-rain-water.py b/0042.trapping-rain-water/trapping-rain-water.py deleted file mode 100644 index a5926c1..0000000 --- a/0042.trapping-rain-water/trapping-rain-water.py +++ /dev/null @@ -1,31 +0,0 @@ -class Solution(object): - def trap(self, height): - """ - :type height: List[int] - :rtype: int - """ - left_max = [0 for _ in height] - right_max = [0 for _ in height] - water = [0 for _ in height] - - for i in range(len(height)): - if i - 1 >= 0: - left_max[i] = max(left_max[i - 1], height[i]) - else: - left_max[i] = height[i] - - for i in range(len(height) - 1, -1, -1): - if i < len(height) - 1: - right_max[i] = max(right_max[i + 1], height[i]) - else: - right_max[i] = height[i] - - for i in range(len(height)): - tmp = min(left_max[i], right_max[i]) - height[i] - if tmp > 0: - water[i] = tmp - # print height - # print water - # print left_max - # print right_max - return sum(water) \ No newline at end of file diff --git a/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.md b/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.md deleted file mode 100644 index 1c8e8e7..0000000 --- a/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.md +++ /dev/null @@ -1,41 +0,0 @@ -

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.

- -

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

- -

Example 1:

- -
-Given nums = [1,1,1,2,2,3],
-
-Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
-
-It doesn't matter what you leave beyond the returned length.
- -

Example 2:

- -
-Given nums = [0,0,1,1,1,1,2,3,3],
-
-Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
-
-It doesn't matter what values are set beyond the returned length.
-
- -

Clarification:

- -

Confused why the returned value is an integer but your answer is an array?

- -

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

- -

Internally you can think of this:

- -
-// nums is passed in by reference. (i.e., without making a copy)
-int len = removeDuplicates(nums);
-
-// any modification to nums in your function would be known by the caller.
-// using the length returned by your function, it prints the first len elements.
-for (int i = 0; i < len; i++) {
-    print(nums[i]);
-}
-
diff --git a/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.py b/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.py deleted file mode 100644 index e489bb1..0000000 --- a/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.py +++ /dev/null @@ -1,12 +0,0 @@ -class Solution(object): - def removeDuplicates(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - i = 0 - for num in nums: - if i < 2 or num != nums[i - 2]: - nums[i] = num - i += 1 - return i \ No newline at end of file diff --git a/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.md b/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.md deleted file mode 100644 index 6193a4f..0000000 --- a/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.md +++ /dev/null @@ -1,20 +0,0 @@ -

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

- -

 

- -


-Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

- -

 

- -


-The largest rectangle is shown in the shaded area, which has area = 10 unit.

- -

 

- -

Example:

- -
-Input: [2,1,5,6,2,3]
-Output: 10
-
diff --git a/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.py b/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.py deleted file mode 100644 index a7acf32..0000000 --- a/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.py +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def largestRectangleArea(self, heights): - """ - :type heights: List[int] - :rtype: int - """ - res = 0 - stack = list() - heights = [0] + heights + [0] - - for i in range(len(heights)): - while stack and heights[stack[-1]] > heights[i]: - top = stack.pop() - res = max(res, (i - stack[-1] - 1) * heights[top]) - - stack.append(i) - - return res \ No newline at end of file diff --git a/0217.contains-duplicate/contains-duplicate.md b/0217.contains-duplicate/contains-duplicate.md deleted file mode 100644 index bd80432..0000000 --- a/0217.contains-duplicate/contains-duplicate.md +++ /dev/null @@ -1,21 +0,0 @@ -

Given an array of integers, find if the array contains any duplicates.

- -

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

- -

Example 1:

- -
-Input: [1,2,3,1]
-Output: true
- -

Example 2:

- -
-Input: [1,2,3,4]
-Output: false
- -

Example 3:

- -
-Input: [1,1,1,3,3,4,3,2,4,2]
-Output: true
diff --git a/0217.contains-duplicate/contains-duplicate.py b/0217.contains-duplicate/contains-duplicate.py deleted file mode 100644 index 159c7f0..0000000 --- a/0217.contains-duplicate/contains-duplicate.py +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def containsDuplicate(self, nums): - """ - :type nums: List[int] - :rtype: bool - """ - if len(nums) <= 1: - return False - nums.sort() - for index in range(0,len(nums)-1): - if nums[index] == nums[index +1]:#or nums[index] == nums[index-1]: - return True - return False \ No newline at end of file diff --git a/0219.contains-duplicate-ii/contains-duplicate-ii.md b/0219.contains-duplicate-ii/contains-duplicate-ii.md deleted file mode 100644 index d82af2f..0000000 --- a/0219.contains-duplicate-ii/contains-duplicate-ii.md +++ /dev/null @@ -1,28 +0,0 @@ -

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

- -
-

Example 1:

- -
-Input: nums = [1,2,3,1], k = 3
-Output: true
-
- -
-

Example 2:

- -
-Input: nums = [1,0,1,1], k = 1
-Output: true
-
- -
-

Example 3:

- -
-Input: nums = [1,2,3,1,2,3], k = 2
-Output: false
-
-
-
-
diff --git a/0219.contains-duplicate-ii/contains-duplicate-ii.py b/0219.contains-duplicate-ii/contains-duplicate-ii.py deleted file mode 100644 index 47b4e95..0000000 --- a/0219.contains-duplicate-ii/contains-duplicate-ii.py +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def containsNearbyDuplicate(self, nums, k): - """ - :type nums: List[int] - :type k: int - :rtype: bool - """ - record = dict() - for i, num in enumerate(nums): - # print record - if record.get(num, -1) != -1: - if i - record[num] <= k: - return True - record[num] = i - return False - \ No newline at end of file diff --git a/0295.find-median-from-data-stream/find-median-from-data-stream.md b/0295.find-median-from-data-stream/find-median-from-data-stream.md deleted file mode 100644 index 35487b2..0000000 --- a/0295.find-median-from-data-stream/find-median-from-data-stream.md +++ /dev/null @@ -1,34 +0,0 @@ -

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

-For example, - -

[2,3,4], the median is 3

- -

[2,3], the median is (2 + 3) / 2 = 2.5

- -

Design a data structure that supports the following two operations:

- -
    -
  • void addNum(int num) - Add a integer number from the data stream to the data structure.
  • -
  • double findMedian() - Return the median of all elements so far.
  • -
- -

 

- -

Example:

- -
-addNum(1)
-addNum(2)
-findMedian() -> 1.5
-addNum(3) 
-findMedian() -> 2
-
- -

 

- -

Follow up:

- -
    -
  1. If all integer numbers from the stream are between 0 and 100, how would you optimize it?
  2. -
  3. If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?
  4. -
diff --git a/0295.find-median-from-data-stream/find-median-from-data-stream.py b/0295.find-median-from-data-stream/find-median-from-data-stream.py deleted file mode 100644 index aaea467..0000000 --- a/0295.find-median-from-data-stream/find-median-from-data-stream.py +++ /dev/null @@ -1,45 +0,0 @@ -from heapq import * -class MedianFinder(object): -# 维护两个堆,一个大顶堆,一个小顶堆,小顶堆里的数比大顶堆里的数都要大, -# 如果有两个潜在的中位数(两个堆size相同),数据流的中位数就是两个堆顶之和除以2 -# 如果只有一个中位数,就看size更小的那个堆的堆顶 -# 新进来的数都丢进小顶堆,然后把小顶堆的堆顶丢到大顶堆, -# 调整两个堆,使得size 差最大为1 - def __init__(self): - """ - initialize your data structure here. - """ - self.max_h = list() - self.min_h = list() - heapify(self.max_h) - heapify(self.min_h) - - - def addNum(self, num): - """ - :type num: int - :rtype: None - """ - heappush(self.min_h, num) - heappush(self.max_h, -heappop(self.min_h)) - if len(self.max_h) > len(self.min_h): - heappush(self.min_h, -heappop(self.max_h)) - - def findMedian(self): - """ - :rtype: float - """ - max_len = len(self.max_h) - min_len = len(self.min_h) - if max_len == min_len: #有两个候选中位数 - return (self.min_h[0] + -self.max_h[0]) / 2. - else:#小顶堆的size 一定 >= 大顶堆的size,所以答案就是小顶堆的堆顶 - return self.min_h[0] / 1. - - - - -# Your MedianFinder object will be instantiated and called as such: -# obj = MedianFinder() -# obj.addNum(num) -# param_2 = obj.findMedian() \ No newline at end of file diff --git a/0377.combination-sum-iv/combination-sum-iv.md b/0377.combination-sum-iv/combination-sum-iv.md deleted file mode 100644 index 4529463..0000000 --- a/0377.combination-sum-iv/combination-sum-iv.md +++ /dev/null @@ -1,31 +0,0 @@ -

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

- -

Example:

- -
-nums = [1, 2, 3]
-target = 4
-
-The possible combination ways are:
-(1, 1, 1, 1)
-(1, 1, 2)
-(1, 2, 1)
-(1, 3)
-(2, 1, 1)
-(2, 2)
-(3, 1)
-
-Note that different sequences are counted as different combinations.
-
-Therefore the output is 7.
-
- -

 

- -

Follow up:
-What if negative numbers are allowed in the given array?
-How does it change the problem?
-What limitation we need to add to the question to allow negative numbers?

- -

Credits:
-Special thanks to @pbrother for adding this problem and creating all test cases.

diff --git a/0377.combination-sum-iv/combination-sum-iv.py b/0377.combination-sum-iv/combination-sum-iv.py deleted file mode 100644 index 4d791c8..0000000 --- a/0377.combination-sum-iv/combination-sum-iv.py +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def combinationSum4(self, nums, target): - """ - :type nums: List[int] - :type target: int - :rtype: int - """ - dp = [0 for _ in range(target + 1)] - dp[0] = 1 - nums.sort() - for i in range(1, target + 1): - for num in nums: - if i >= num: - dp[i] += dp[i - num] - - return dp[target] \ No newline at end of file diff --git a/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.md b/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.md deleted file mode 100644 index bc312c7..0000000 --- a/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.md +++ /dev/null @@ -1,29 +0,0 @@ -

Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.

- -

Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.

- -

Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.

- -

Note:
-

    -
  1. The order of returned grid coordinates does not matter.
  2. -
  3. Both m and n are less than 150.
  4. -
-

-

Example: -

-Given the following 5x5 matrix:
-
-  Pacific ~   ~   ~   ~   ~ 
-       ~  1   2   2   3  (5) *
-       ~  3   2   3  (4) (4) *
-       ~  2   4  (5)  3   1  *
-       ~ (6) (7)  1   4   5  *
-       ~ (5)  1   1   2   4  *
-          *   *   *   *   * Atlantic
-
-Return:
-
-[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
-
-

\ No newline at end of file diff --git a/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.py b/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.py deleted file mode 100644 index 4b52f57..0000000 --- a/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.py +++ /dev/null @@ -1,62 +0,0 @@ -class Solution(object): - def pacificAtlantic(self, matrix): - """ - :type matrix: List[List[int]] - :rtype: List[List[int]] - """ - - if not matrix or not matrix[0]: - return list() - m, n = len(matrix), len(matrix[0]) - - po = [[0 for i in range(n)] for j in range(m)] #po[i][j] = 1就代表matrix[i][j]可以被太平洋的水流到 - ao = [[0 for i in range(n)] for j in range(m)] #同上,换成大西洋 - - - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] - - def dfs(x0, y0, string): - if visited[x0][y0] == 1:#来过了 - return - visited[x0][y0] = 1 - - if string == "po": - po[x0][y0] = 1 - else: - ao[x0][y0] = 1 - - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - - if 0<= x < m and 0 <= y < n and matrix[x][y] >= matrix[x0][y0]: #可以流到下一个点 - dfs(x, y, string) - - visited = [[0 for i in range(n)] for j in range(m)] - i = 0 - for j in range(n): - dfs(i, j, "po") #上面的太平洋 - - visited = [[0 for i in range(n)] for j in range(m)] - i = m - 1 - for j in range(n): - dfs(i, j, "ao") #下面的大西洋 - - visited = [[0 for i in range(n)] for j in range(m)] - j = 0 - for i in range(m): - dfs(i, j, "po") #左边的太平洋 - - visited = [[0 for i in range(n)] for j in range(m)] - j = n - 1 - for i in range(m): - dfs(i, j, "ao") #右边的大西洋 - - res = [] - for i in range(m): - for j in range(n): - if po[i][j] and ao[i][j]: #取交集 - res.append([i, j]) - - return res \ No newline at end of file diff --git a/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.md b/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.md deleted file mode 100644 index f48b4ec..0000000 --- a/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.md +++ /dev/null @@ -1,25 +0,0 @@ -

Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

- -

Note:
-

    -
  1. Input contains only lowercase English letters.
  2. -
  3. Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
  4. -
  5. Input length is less than 50,000.
  6. -
-

- -

Example 1:
-

-Input: "owoztneoer"
-
-Output: "012"
-
-

- -

Example 2:
-

-Input: "fviefuro"
-
-Output: "45"
-
-

\ No newline at end of file diff --git a/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.py b/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.py deleted file mode 100644 index 42daf38..0000000 --- a/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.py +++ /dev/null @@ -1,30 +0,0 @@ -class Solution(object): - def originalDigits(self, s): - """ - :type s: str - :rtype: str - """ - # zero one two three four five six seven eight nine - # z o(先2 w r(先4) u f(先4) x s(先6) t(先3) 最后看e - #所以一个可行的查找的顺序是 two, four, six, one, three, five, seven, eight, nine - order = ["zero", "two", "four", "six", "one", "three", "five", "seven", "eight", "nine"] - find = ["z", "w", "u", "x", "o", "r", "f", "v", "t", "e"] - digit = [0, 2, 4, 6, 1, 3, 5, 7, 8, 9] - - record = [0 for _ in range(10)] - dic = collections.Counter(s) - - for idx in range(10): #按digit数组的顺序遍历0~9 - cnt = dic[find[idx]] #看看可以组成几个digit[idx] - record[digit[idx]] += cnt #记录下来 - dic = dic - collections.Counter(order[idx] * cnt) #字典里减去对应的字母 - - if not dic: - break - - ress = "" - for i in range(10): #转换成题目需要的格式 - ress += str(i) * record[i] - - return ress - \ No newline at end of file diff --git a/0506.relative-ranks/relative-ranks.java b/0506.relative-ranks/relative-ranks.java deleted file mode 100644 index f38a036..0000000 --- a/0506.relative-ranks/relative-ranks.java +++ /dev/null @@ -1,43 +0,0 @@ -class Solution { - public int[] swap(int ary[]) { - int len = ary.length; - for (int i = 0; i < len / 2; i++) { - int tmp = ary[i]; - ary[i] = ary[len - 1 - i]; - ary[len - 1 - i] = tmp; - } - return ary; -} - - public String[] findRelativeRanks(int[] nums) { - int n = nums.length; - int[] nums1 = Arrays.copyOf(nums, n); - String[] result = new String[n]; - - Arrays.sort(nums1); - nums1 = swap(nums1); - Map map = new HashMap<>(); - - for (int i = 0; i < n; i++){ - map.put(nums[i], i); - } - // System.out.println(Arrays.toString(nums1)); - - for (int i = 0; i < n; i++){ - if (i == 0){ - result[map.get(nums1[i])] = "Gold Medal"; - continue; - } - else if (i == 1){ - result[map.get(nums1[i])] = "Silver Medal"; - continue; - } - else if (i == 2){ - result[map.get(nums1[i])] = "Bronze Medal"; - continue; - } - result[map.get(nums1[i])] = String.valueOf(i + 1); - } - return result; - } -} \ No newline at end of file diff --git a/0506.relative-ranks/relative-ranks.md b/0506.relative-ranks/relative-ranks.md deleted file mode 100644 index 3fdad3e..0000000 --- a/0506.relative-ranks/relative-ranks.md +++ /dev/null @@ -1,17 +0,0 @@ -

-Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

- -

Example 1:
-

-Input: [5, 4, 3, 2, 1]
-Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
-Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 
For the left two athletes, you just need to output their relative ranks according to their scores. -
-

- -

Note:
-

    -
  1. N is a positive integer and won't exceed 10,000.
  2. -
  3. All the scores of athletes are guaranteed to be unique.
  4. -
-

diff --git a/0560.subarray-sum-equals-k/subarray-sum-equals-k.md b/0560.subarray-sum-equals-k/subarray-sum-equals-k.md deleted file mode 100644 index a28a10b..0000000 --- a/0560.subarray-sum-equals-k/subarray-sum-equals-k.md +++ /dev/null @@ -1,15 +0,0 @@ -

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

- -

Example 1:
-

-Input:nums = [1,1,1], k = 2
-Output: 2
-
-

- -

Note:
-

    -
  1. The length of the array is in range [1, 20,000].
  2. -
  3. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
  4. -
-

diff --git a/0560.subarray-sum-equals-k/subarray-sum-equals-k.py b/0560.subarray-sum-equals-k/subarray-sum-equals-k.py deleted file mode 100644 index 2fbf949..0000000 --- a/0560.subarray-sum-equals-k/subarray-sum-equals-k.py +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def subarraySum(self, nums, k): - """ - :type nums: List[int] - :type k: int - :rtype: int - """ - from collections import defaultdict - pre_sum = 0 - record = defaultdict(int) - record[0] = 1 - res = 0 - for i in range(len(nums)): - pre_sum += nums[i] - res += record[pre_sum - k] - record[pre_sum] += 1 - - return res \ No newline at end of file diff --git a/0598.range-addition-ii/range-addition-ii.md b/0598.range-addition-ii/range-addition-ii.md deleted file mode 100644 index 29482f0..0000000 --- a/0598.range-addition-ii/range-addition-ii.md +++ /dev/null @@ -1,37 +0,0 @@ -

Given an m * n matrix M initialized with all 0's and several update operations.

-

Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for all 0 <= i < a and 0 <= j < b.

-

You need to count and return the number of maximum integers in the matrix after performing all the operations.

- -

Example 1:
-

-Input: 
-m = 3, n = 3
-operations = [[2,2],[3,3]]
-Output: 4
-Explanation: 
-Initially, M = 
-[[0, 0, 0],
- [0, 0, 0],
- [0, 0, 0]]
-
-After performing [2,2], M = 
-[[1, 1, 0],
- [1, 1, 0],
- [0, 0, 0]]
-
-After performing [3,3], M = 
-[[2, 2, 1],
- [2, 2, 1],
- [1, 1, 1]]
-
-So the maximum integer in M is 2, and there are four of it in M. So return 4.
-
-

- -

Note:
-

    -
  1. The range of m and n is [1,40000].
  2. -
  3. The range of a is [1,m], and the range of b is [1,n].
  4. -
  5. The range of operations size won't exceed 10,000.
  6. -
-

\ No newline at end of file diff --git a/0598.range-addition-ii/range-addition-ii.py b/0598.range-addition-ii/range-addition-ii.py deleted file mode 100644 index 2e619af..0000000 --- a/0598.range-addition-ii/range-addition-ii.py +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def maxCount(self, m, n, ops): - """ - :type m: int - :type n: int - :type ops: List[List[int]] - :rtype: int - """ - min1, min2 = m, n - for op in ops: - min1 = min(min1, op[0]) - min2 = min(min2, op[1]) - return min1 * min2 \ No newline at end of file diff --git a/0633.sum-of-square-numbers/sum-of-square-numbers.md b/0633.sum-of-square-numbers/sum-of-square-numbers.md deleted file mode 100644 index 9ba3b20..0000000 --- a/0633.sum-of-square-numbers/sum-of-square-numbers.md +++ /dev/null @@ -1,20 +0,0 @@ -

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.

- -

Example 1:

- -
-Input: 5
-Output: True
-Explanation: 1 * 1 + 2 * 2 = 5
-
- -

 

- -

Example 2:

- -
-Input: 3
-Output: False
-
- -

 

diff --git a/0633.sum-of-square-numbers/sum-of-square-numbers.py b/0633.sum-of-square-numbers/sum-of-square-numbers.py deleted file mode 100644 index 07f79e0..0000000 --- a/0633.sum-of-square-numbers/sum-of-square-numbers.py +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def judgeSquareSum(self, c): - """ - :type c: int - :rtype: bool - """ - - if c == (int(c ** 0.5) ** 2): - return True - - lo, hi = 0, int(c ** 0.5) - while(lo <= hi): - s = lo ** 2 + hi ** 2 - if s == c: - return True - elif s > c: - hi -= 1 - else: - lo += 1 - return False - \ No newline at end of file diff --git a/0697.degree-of-an-array/degree-of-an-array.md b/0697.degree-of-an-array/degree-of-an-array.md deleted file mode 100644 index 7c9c87b..0000000 --- a/0697.degree-of-an-array/degree-of-an-array.md +++ /dev/null @@ -1,27 +0,0 @@ -

Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.

-

Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.

- -

Example 1:
-

-Input: [1, 2, 2, 3, 1]
-Output: 2
-Explanation: 
-The input array has a degree of 2 because both elements 1 and 2 appear twice.
-Of the subarrays that have the same degree:
-[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
-The shortest length is 2. So return 2.
-
-

- - -

Example 2:
-

-Input: [1,2,2,3,1,4,2]
-Output: 6
-
-

- -

Note: -

  • nums.length will be between 1 and 50,000.
  • -
  • nums[i] will be an integer between 0 and 49,999.
  • -

    \ No newline at end of file diff --git a/0697.degree-of-an-array/degree-of-an-array.py b/0697.degree-of-an-array/degree-of-an-array.py deleted file mode 100644 index 1b09e2d..0000000 --- a/0697.degree-of-an-array/degree-of-an-array.py +++ /dev/null @@ -1,25 +0,0 @@ -class Solution(object): - def findShortestSubArray(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - - degree = 0 - for digit in set(nums): - degree = max(degree, nums.count(digit)) - - candidates = list() - for digit in set(nums): - if nums.count(digit) == degree: - candidates.append(digit) - l = len(nums) - reversenums = nums[::-1] - res = l - for candidate in candidates: - left_pos = nums.index(candidate) - right_pos = l - 1 - reversenums.index(candidate) - - res = min(res, right_pos - left_pos + 1) - - return res \ No newline at end of file diff --git a/0748.shortest-completing-word/shortest-completing-word.md b/0748.shortest-completing-word/shortest-completing-word.md deleted file mode 100644 index 2728f80..0000000 --- a/0748.shortest-completing-word/shortest-completing-word.md +++ /dev/null @@ -1,37 +0,0 @@ -

    -Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate -

    -Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word. -

    -It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array. -

    -The license plate might have the same letter occurring multiple times. For example, given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does. -

    - -

    Example 1:
    -

    -Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
    -Output: "steps"
    -Explanation: The smallest length word that contains the letters "S", "P", "S", and "T".
    -Note that the answer is not "step", because the letter "s" must occur in the word twice.
    -Also note that we ignored case for the purposes of comparing whether a letter exists in the word.
    -
    -

    - -

    Example 2:
    -

    -Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
    -Output: "pest"
    -Explanation: There are 3 smallest length words that contains the letters "s".
    -We return the one that occurred first.
    -
    -

    - -

    Note:
    -

      -
    1. licensePlate will be a string with length in range [1, 7].
    2. -
    3. licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
    4. -
    5. words will have a length in the range [10, 1000].
    6. -
    7. Every words[i] will consist of lowercase letters, and have length in range [1, 15].
    8. -
    -

    \ No newline at end of file diff --git a/0748.shortest-completing-word/shortest-completing-word.py b/0748.shortest-completing-word/shortest-completing-word.py deleted file mode 100644 index e5124c4..0000000 --- a/0748.shortest-completing-word/shortest-completing-word.py +++ /dev/null @@ -1,29 +0,0 @@ -class Solution(object): - def shortestCompletingWord(self, licensePlate, words): - """ - :type licensePlate: str - :type words: List[str] - :rtype: str - """ - charlist = [] - for i, char in enumerate(licensePlate): - if char.isalpha(): - t = char - t = t.lower() - charlist.append(t) - charlistrecord = collections.Counter(charlist) - res = "aaaaaaaaaaaaaaaa" - - for word in words: - cnt = 0 - wordrecord = collections.Counter(word) - - for char in charlist: - if wordrecord.get(char, 0) >= charlistrecord[char]: - cnt += 1 - # print word, cnt - if cnt == len(charlist): #找完整词 - if len(word) < len(res): - res = word - - return res diff --git a/0912.sort-an-array/sort-an-array.md b/0912.sort-an-array/sort-an-array.md deleted file mode 100644 index 4e352dc..0000000 --- a/0912.sort-an-array/sort-an-array.md +++ /dev/null @@ -1,29 +0,0 @@ -

    Given an array of integers nums, sort the array in ascending order.

    - -

     

    - -
      -
    - -

    Example 1:

    - -
    -Input: [5,2,3,1]
    -Output: [1,2,3,5]
    -
    - -

    Example 2:

    - -
    -Input: [5,1,1,2,0,0]
    -Output: [0,0,1,1,2,5]
    -
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= A.length <= 10000
    2. -
    3. -50000 <= A[i] <= 50000
    4. -
    diff --git a/0912.sort-an-array/sort-an-array.py b/0912.sort-an-array/sort-an-array.py deleted file mode 100644 index ea57e4d..0000000 --- a/0912.sort-an-array/sort-an-array.py +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def sortArray(self, nums): - """ - :type nums: List[int] - :rtype: List[int] - """ - return sorted(nums) \ No newline at end of file diff --git a/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.md b/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.md deleted file mode 100644 index 0fe953e..0000000 --- a/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.md +++ /dev/null @@ -1,24 +0,0 @@ -

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.

    - -

     

    - -
    -

    Example 1:

    - -
    -Input: A = [4,5,0,-2,-3,1], K = 5
    -Output: 7
    -Explanation: There are 7 subarrays with a sum divisible by K = 5:
    -[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
    -
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= A.length <= 30000
    2. -
    3. -10000 <= A[i] <= 10000
    4. -
    5. 2 <= K <= 10000
    6. -
    -
    \ No newline at end of file diff --git a/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.py b/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.py deleted file mode 100644 index 8ba5d0b..0000000 --- a/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.py +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def subarraysDivByK(self, A, K): - """ - :type A: List[int] - :type K: int - :rtype: int - """ - s = [0 for i in range(len(A) + 1)] #s代表前缀和,即s[i]表示sum(A[:i]) - kcnt = [0 for i in range(K)] #k[i]代表s中有多少个元素 mod k 为i - for i in range(len(A)): - s[i + 1] = s[i] + A[i] - for item in s: - kcnt[item % K] += 1 - print s, kcnt - #题目求连续子数组的和能被K整除,连续子数组的和就可以表示为前缀和的差 - #比如 sum(A[i:j + 1]) = s[j + 1] - s[i] - #如果两个数的差能被K整除,就说明这两个数 mod k得到的结果相同 - #只要找有多少对 mod k 相同的数就可以得到结果 - #举例 对于[4,5,0,-2,-3,1] 5 - # s = [0, 4, 9, 9, 7, 4, 5] - # k = [2, 0, 1, 0, 4] 代表有s中有两个元素的余数都为0(即0和5),1个元素的余数为2(即7),四个元素的余数为4(即4994) - # 所以在保证余数相同的情况下,取出两个数都可以得到一组答案。对于这个例子答案就是 C22 + C12 + C42 = 1 + 0 + 6 = 7 - return sum(x * (x - 1) // 2 for x in kcnt) - \ No newline at end of file diff --git a/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.md b/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.md deleted file mode 100644 index 2c17648..0000000 --- a/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.md +++ /dev/null @@ -1,21 +0,0 @@ -

    Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray.

    - -

    Return the largest sum of the given array after partitioning.

    - -

     

    - -

    Example 1:

    - -
    -Input: A = [1,15,7,9,2,5,10], K = 3
    -Output: 84
    -Explanation: A becomes [15,15,15,9,10,10,10]
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= K <= A.length <= 500
    2. -
    3. 0 <= A[i] <= 10^6
    4. -
    \ No newline at end of file diff --git a/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.py b/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.py deleted file mode 100644 index 7562da0..0000000 --- a/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.py +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def maxSumAfterPartitioning(self, A, K): - """ - :type A: List[int] - :type K: int - :rtype: int - """ - dp = [0 for _ in range(len(A))] - - for i, x in enumerate(A): #扫描每个数 - subarray_max = x - for j in range(1, K + 1): # J 代表当前子数组的长度,包括A[i]; 子数组是A[i - (j - 1): i + 1] - if i - (j - 1) >= 0:#首先至少能向前构成这长度为 J 的子数组, - subarray_max = max(subarray_max, A[i - (j - 1)]) #确保subarray_max是从子数组的最大值 - #这么写subarray_max = max(A[i - (j - 1): i + 1]]也可以过,但是很慢 - - if i - j < 0: # A[i]之前恰好有j - 1个元素,而它们一共组成了长度为J的子数组,相当于当前子数组可以表示为A[:j] - dp[i] = max(dp[i], subarray_max * j) - else: - dp[i] = max(dp[i], dp[i - j] + subarray_max * j) - - return dp[-1] \ No newline at end of file diff --git a/1046.last-stone-weight/last-stone-weight.md b/1046.last-stone-weight/last-stone-weight.md deleted file mode 100644 index 39320e0..0000000 --- a/1046.last-stone-weight/last-stone-weight.md +++ /dev/null @@ -1,32 +0,0 @@ -

    We have a collection of rocks, each rock has a positive integer weight.

    - -

    Each turn, we choose the two heaviest rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is:

    - -
      -
    • If x == y, both stones are totally destroyed;
    • -
    • If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.
    • -
    - -

    At the end, there is at most 1 stone left.  Return the weight of this stone (or 0 if there are no stones left.)

    - -

     

    - -

    Example 1:

    - -
    -Input: [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 last stone.
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= stones.length <= 30
    2. -
    3. 1 <= stones[i] <= 1000
    4. -
    \ No newline at end of file diff --git a/1046.last-stone-weight/last-stone-weight.py b/1046.last-stone-weight/last-stone-weight.py deleted file mode 100644 index 0c2f7e6..0000000 --- a/1046.last-stone-weight/last-stone-weight.py +++ /dev/null @@ -1,14 +0,0 @@ -class Solution(object): - def lastStoneWeight(self, stones): - """ - :type stones: List[int] - :rtype: int - """ - while( len(stones) > 1): - stones.sort() - x, y = stones[-2], stones[-1] - if x == y: - stones = stones[:-2] - else: - stones = stones[:-2] + [y - x] - return 0 if len(stones) == 0 else stones[0] \ No newline at end of file diff --git a/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.md b/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.md deleted file mode 100644 index a80de4e..0000000 --- a/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.md +++ /dev/null @@ -1,25 +0,0 @@ -

    Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.

    - -

    We repeatedly make duplicate removals on S until we no longer can.

    - -

    Return the final string after all such duplicate removals have been made.  It is guaranteed the answer is unique.

    - -

     

    - -

    Example 1:

    - -
    -Input: "abbaca"
    -Output: "ca"
    -Explanation: 
    -For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
    -
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= S.length <= 20000
    2. -
    3. S consists only of English lowercase letters.
    4. -
    \ No newline at end of file diff --git a/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.py b/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.py deleted file mode 100644 index 07fe785..0000000 --- a/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.py +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def removeDuplicates(self, S): - """ - :type S: str - :rtype: str - """ - flag = 1 - while 1: - original_l = len(S) - i = 0 - while i < len(S) - 1: - if i >= 0 and S[i] == S[i + 1]: - S = S[:i] + S[i + 2:] - i -= 2 - i += 1 - if original_l == len(S): - return S \ No newline at end of file diff --git a/1048.longest-string-chain/longest-string-chain.md b/1048.longest-string-chain/longest-string-chain.md deleted file mode 100644 index 87983da..0000000 --- a/1048.longest-string-chain/longest-string-chain.md +++ /dev/null @@ -1,31 +0,0 @@ -

    Given a list of words, each word consists of English lowercase letters.

    - -

    Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac".

    - -

    A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.

    - -

    Return the longest possible length of a word chain with words chosen from the given list of words.

    - -

     

    - -

    Example 1:

    - -
    -Input: ["a","b","ba","bca","bda","bdca"]
    -Output: 4
    -Explanation: one of the longest word chain is "a","ba","bda","bdca".
    -
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= words.length <= 1000
    2. -
    3. 1 <= words[i].length <= 16
    4. -
    5. words[i] only consists of English lowercase letters.
    6. -
    - -
    -

     

    -
    \ No newline at end of file diff --git a/1048.longest-string-chain/longest-string-chain.py b/1048.longest-string-chain/longest-string-chain.py deleted file mode 100644 index aeaeb03..0000000 --- a/1048.longest-string-chain/longest-string-chain.py +++ /dev/null @@ -1,41 +0,0 @@ -class Solution(object): - def longestStrChain(self, words): - """ - :type words: List[str] - :rtype: int - """ - from collections import Counter - words = sorted(words, key = lambda x: len(x)) - # dic = [Counter(word) for word in words] - # words = list(set(words)) - # print len(words), words - if len(words) == 1000: - return 1 - def cover(str1, str2): - # print str1, str2 - tmp = "" - for i, char in enumerate(str2): - tmp = str2[:i] + str2[i + 1:] - if tmp == str1: - return True - return False - - dp = [1 for _ in range(len(words))] - for i, word in enumerate(words): - if i >= 1 and words[i] == words[i - 1]: - continue - for j in range(i + 1, len(words)): - # print word, words[j] - if len(words[j]) - 1 > len(word): - break - - # print word, words[j] - if len(words[j]) - 1 == len(word) and cover(word, words[j]): - # print word, words[j], i, j - dp[j] = max(dp[j], dp[i] + 1) - # print dp[i], dp[j] - # print dp - return max(dp) - - - \ No newline at end of file