Skip to main content

Posts

Showing posts from June, 2017

LeetCode Algorithm Questions by Python

4. Median of Two Sorted Arrays There are two sorted arrays  nums1  and  nums2  of size m and n respectively. Find the median of the two sorted arrays. 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 The idea is to pick up the numbers in nums2 that are smaller than a number in nums1, until we get the median. Pay attention: 1. consider three cases: odd, even, and null; 2. max of nums1 may smaller than min of nums2; 3. use float() while doing the division. My code in Python is as follows: class Solution(object):     def findMedianSortedArrays(self, nums1, nums2):         """         :type nums1: List[int]         :type nums2: List[int]         :rtype: float         """         m = len(nums1)         n = len(nums2)         mn = m + n         i = 0         acc = 0         med2 = 0         if m == 0 or n == 0:             nums1.extend(nums2)  

Display the Records Which Have N or More Consecutive Rows with Amount More Than K

Example: Human Traffic of Stadium X city built a new stadium, each day many people visit it and the stats are saved as these columns:  id ,  date ,  people Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive). For example, the table  stadium : +------+------------+-----------+ | id | date | people | +------+------------+-----------+ | 1 | 2017-01-01 | 10 | | 2 | 2017-01-02 | 109 | | 3 | 2017-01-03 | 150 | | 4 | 2017-01-04 | 99 | | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 2017-01-08 | 188 | +------+------------+-----------+ For the sample data above, the output is: +------+------------+-----------+ | id | date | people | +------+------------+-----------+ | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 201