2023年7月31日 星期一

7/31 每日一題 (MYSQL 找出薪水高於其直屬上司的員工)

 Table: Employee

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
| salary      | int     |
| managerId   | int     |
+-------------+---------+
id is the primary key column for this table.
Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager.

 

Write an SQL query to find the employees who earn more than their managers.

Return the result table in any order.

The query result format is in the following example.

 

Example 1:

Input: 
Employee table:
+----+-------+--------+-----------+
| id | name  | salary | managerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | Null      |
| 4  | Max   | 90000  | Null      |
+----+-------+--------+-----------+
Output: 
+----------+
| Employee |
+----------+
| Joe      |
+----------+
Explanation: Joe is the only employee who earns more than his manager.


# Write your MySQL query statement below
SELECT E1.name AS Employee
FROM Employee E1
INNER JOIN Employee E2
ON E1.managerId = E2.id
WHERE E1.salary > E2.salary;

#SELECT E1.name AS Employee 這部分是表查詢的結果列,使用SELECT指定要返回的欄位

#從Employee表 簡稱E1 選擇欄位name,用AS把欄位取別名為Employee ,

#自連接了 同樣的Employee表改稱E2

#其中連接的基準用 Employee表的 E1.managerId = E2.id

#找到E1表的經理ID然後查對應到的E2表的員工ID, 這樣連接起來 就是員工與經理的配對

#WHERE E1.salary > E2.salary; 找出工資高於其經理的員工


合併資料庫
------------------------------------下面合併表格 看 員工與對應的經理資料庫
| id | name | salary | managerId | id | name | salary | managerId |
| -- | ----- | ------ | --------- | -- | ---- | ------ | --------- |
| 1 | Joe | 70000 | 3 | 3 | Sam | 60000 | null |
| 2 | Henry | 80000 | 4 | 4 | Max | 90000 | null |

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


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

Output
| Employee | | -------- | | Joe |



---------------------參考解答


# Write your MySQL query statement below
select b.name as Employee from Employee as a JOIN employee as b 
ON a.id = b.managerId AND a.salary<b.salary











































































標籤:

7/31 每日一題(Py,返回2n個數組兩兩配對的最小值總和)

 Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum.

 

Example 1:

Input: nums = [1,4,3,2]
Output: 4
Explanation: All possible pairings (ignoring the ordering of elements) are:
1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4
So the maximum possible sum is 4.

Example 2:

Input: nums = [6,2,6,5,1,2]
Output: 9
Explanation: The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.

 

Constraints:

  • 1 <= n <= 104
  • nums.length == 2 * n
  • -104 <= nums[i] <= 104




class Solution:
    def arrayPairSum(self, nums: List[int]) -> int:
       
        #為了考慮最小值的總和最大化應該考慮相鄰的字兩兩一組減少損失
        nums.sort() #升序
        c=0
        res=0
        for i in nums:
            if c%2==0:
                res+=i
            c+=1
        return res




























標籤:

2023年7月29日 星期六

7/30 每日一題(每個子句順序不變, 子句內容反轉)

 Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

 

Example 1:

Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

Example 2:

Input: s = "God Ding"
Output: "doG gniD"

 

Constraints:

  • 1 <= s.length <= 5 * 104
  • s contains printable ASCII characters.
  • s does not contain any leading or trailing spaces.
  • There is at least one word in s.
  • All the words in s are separated by a single space.

class Solution:
    def reverseWords(self, s: str) -> str:
        n=len(s)
       
        if n==1:
            return s #只有自身直接返回
        res=s.split()#預設值以空格當作分割
        ans=[] #初始化ans
        for i in range(len(res)):
            ans.append(res[i][-1::-1]) #取每一個子句的反轉+到ans

        return ' '.join(ans) #以空格為間隔加入ans

       
       

------------------參考解答
def reverseWords(s: str) -> str:
    words = s.split()  # Split the string into words by spaces
    reversed_words = [word[::-1] for word in words]  # Reverse each word
    return ' '.join(reversed_words)  # Join the reversed words with spaces and return the result









標籤: