2023年9月29日 星期五

9/28 每日一題(回傳最大元素和最小元素,刪減k範圍後的最小差距)

 You are given an integer array nums and an integer k.

In one operation, you can choose any index i where 0 <= i < nums.length and change nums[i] to nums[i] + x where x is an integer from the range [-k, k]. You can apply this operation at most once for each index i.

The score of nums is the difference between the maximum and minimum elements in nums.

Return the minimum score of nums after applying the mentioned operation at most once for each index in it.

 

Example 1:

Input: nums = [1], k = 0
Output: 0
Explanation: The score is max(nums) - min(nums) = 1 - 1 = 0.

Example 2:

Input: nums = [0,10], k = 2
Output: 6
Explanation: Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.

Example 3:

Input: nums = [1,3,6], k = 3
Output: 0
Explanation: Change nums to be [4, 4, 4]. The score is max(nums) - min(nums) = 4 - 4 = 0.

 

Constraints:

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 104
  • 0 <= k <= 104


class Solution:
    def smallestRangeI(self, nums: List[int], k: int) -> int:
        if len(nums)==1:
            return 0
        else:
            max_ele=max(nums)
            min_ele=min(nums)
            min_diff=max_ele-min_ele-2*k
# min_diff=min_ele+k=max_ele-k
            return max(0,min_diff)

------------------------參考
class Solution:
    def smallestRangeI(self, nums: List[int], k: int) -> int:
        # make the max smaller, make min larger
        return max((max(nums) - k) - (min(nums) + k), 0)


--------------------------

標籤:

2023年9月28日 星期四

9/27 每日一題(pandas 回傳第二高薪水)

Table: Employee

+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| salary      | int  |
+-------------+------+
id is the primary key (column with unique values) for this table.
Each row of this table contains information about the salary of an employee.

 

Write a solution to find the second highest salary from the Employee table. If there is no second highest salary, return null (return None in Pandas).

The result format is in the following example.

 

Example 1:

Input: 
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
Output: 
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

Example 2:

Input: 
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
+----+--------+
Output: 
+---------------------+
| SecondHighestSalary |
+---------------------+
| null                | 

+---------------------+ 

import pandas as pd


def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame:
    res=set()
    for i,row in employee.iterrows():
        salary=row['salary']
        res.add(salary)
    res=list(res)
    res.sort()

    if len(res)<2:
        return pd.DataFrame({'SecondHighestSalary':[None]})
    else:
        return pd.DataFrame({'SecondHighestSalary':[res[-2]]})

---------------------
import pandas as pd

def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame:
    salaries = employee['salary'].drop_duplicates().sort_values(ascending = False)
    N = 2
    if len(salaries) < N:
        return pd.DataFrame({'SecondHighestSalary': [None]})
    else:
        ans = salaries.iloc[N-1]
        return pd.DataFrame({'SecondHighestSalary': [ans]})

標籤: