Skip to content

Commit

Permalink
leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenH47 committed Oct 12, 2023
1 parent afa6753 commit 8a51193
Show file tree
Hide file tree
Showing 117 changed files with 4,917 additions and 42 deletions.
3 changes: 2 additions & 1 deletion docs/.obsidian/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"landscape": false,
"margin": "2",
"downscalePercent": 100
}
},
"tabSize": 4
}
2 changes: 1 addition & 1 deletion docs/.obsidian/appearance.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"accentColor": "",
"baseFontSize": 16,
"baseFontSize": 17,
"baseFontSizeAction": false
}
81 changes: 42 additions & 39 deletions docs/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"type": "split",
"children": [
{
"id": "5272dcd559b03175",
"id": "2937b015b90dc01d",
"type": "tabs",
"children": [
{
"id": "155f0b39e20d2ad8",
"id": "5d93f12e17b29565",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "redis/redis_initial.md",
"file": "data_structure/array.md",
"mode": "source",
"source": false
}
Expand Down Expand Up @@ -77,7 +77,8 @@
}
],
"direction": "horizontal",
"width": 200
"width": 200,
"collapsed": true
},
"right": {
"id": "bb6e2c06669023ce",
Expand All @@ -104,7 +105,7 @@
"state": {
"type": "backlink",
"state": {
"file": "redis/redis_initial.md",
"file": "data_structure/array.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -121,7 +122,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "redis/redis_initial.md",
"file": "data_structure/array.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -133,7 +134,7 @@
"state": {
"type": "outline",
"state": {
"file": "redis/redis_initial.md"
"file": "data_structure/array.md"
}
}
}
Expand All @@ -158,46 +159,48 @@
"obsidian-excalidraw-plugin:Create new drawing": false
}
},
"active": "155f0b39e20d2ad8",
"active": "5d93f12e17b29565",
"lastOpenFiles": [
"data_structure/linked-list.md",
"data_structure/array.md",
"img/linked-list-20231012-1.png",
"img/linked-list-20231012.png",
"LeetCode/4sum.md",
"data_structure/HashSet.md",
"LeetCode/reverse-linked-list.md",
"data_structure",
"LeetCode/maximum-average-subarray-i.md",
"LeetCode/container-with-most-water.md",
"LeetCode/kth-largest-element-in-an-array.md",
"LeetCode/best-time-to-buy-and-sell-stock-with-transaction-fee.md",
"LeetCode/is-subsequence.md",
"LeetCode/search-suggestions-system.md",
"LeetCode/string-compression.md",
"LeetCode/removing-stars-from-a-string.md",
"LeetCode/path-sum-ii.md",
"LeetCode/factorial-trailing-zeroes.md",
"LeetCode/zigzag-conversion.md",
"LeetCode/koko-eating-bananas.md",
"LeetCode/counting-bits.md",
"LeetCode/single-number.md",
"LeetCode/reorder-routes-to-make-all-paths-lead-to-the-city-zero.md",
"LeetCode/determine-if-two-strings-are-close.md",
"LeetCode/count-good-nodes-in-binary-tree.md",
"LeetCode/reverse-words-in-a-string.md",
"LeetCode/FizzBuzz.md",
"LeetCode/kids-with-the-greatest-number-of-candies.md",
"LeetCode/greatest-common-divisor-of-strings.md",
"img/kubernetes-20231001-2.png",
"img/kubernetes-20231001-1.png",
"img/kubernetes-20231001.png",
"kubernetes",
"img/redis_initial-20230927-2.png",
"img/redis_initial-20230927-1.png",
"img/redis_initial-20230927.png",
"LeetCode/combinations.md",
"LeetCode/add-two-numbers.md",
"LeetCode/two-sum.md",
"LeetCode/LeetCode_note_menu.md",
"redis/redis_initial.md",
"course_notes/course_note_menu.md",
"redis",
"course_notes/fit9132.md",
"course_notes/Rintro.md",
"course_notes/fit5225_note.md",
"course_notes/fit5120.md",
"course_notes/fit5122.md",
"img/fit5122-20230828-1.png",
"img/fit5122-20230828.png",
"img/fit5120-20230821.png",
"course_notes/fit5137.md",
"img/fit5120-20230820.png",
"img/fit5122-20230816.png",
"chat/20230809_AWSProduct.md",
"img/fit5120-20230806-1.png",
"img/fit5120-20230806.png",
"img/Pasted image 20230806211429.png",
"about/about_me.md",
"about/about_copyright.md",
"taylor/outfit.md",
"about/about_site_building.md",
"taylor",
"about/about_index.md",
"career/career_23summer.md",
"career/career_note_menu.md",
"chat/230521_serverless,microservices,RESTFUL.md",
"chat/230522_SaaS+PaaS+IaaS.md",
"chat/chat_index.md",
"course_notes/fit5225.md",
"index.md",
"googleb19ee7ecc0ffed43.html",
"Untitled.canvas",
"img/fit5171-20230404-28.png.~tmp",
Expand Down
27 changes: 27 additions & 0 deletions docs/LeetCode/4sum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[LeetCode - The World's Leading Online Programming Learning Platform](https://leetcode.com/problems/4sum/)
iven an array `nums` of `n` integers, return _an array of all the **unique** quadruplets_ `[nums[a], nums[b], nums[c], nums[d]]` such that:
给定一个 `n` 整数数组,返回一个包含所有唯一四胞胎 `[nums[a], nums[b], nums[c], nums[d]]` 的数组 `nums` ,使得:

- `0 <= a, b, c, d < n`
- `a``b``c`, and `d` are **distinct**.
`a` 、 `b` `c` 和 `d` 是不同的。
- `nums[a] + nums[b] + nums[c] + nums[d] == target`

You may return the answer in **any order**.
您可以按任何顺序返回答案。

**Example 1: 示例 1:**

**Input:** nums = [1,0,-1,0,-2,2], target = 0
**Output:** [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

**Example 2: 示例 2:**

**Input:** nums = [2,2,2,2,2], target = 8
**Output:** [[2,2,2,2]]

**Constraints: 约束:**

- `1 <= nums.length <= 200`
- `-109 <= nums[i] <= 109`
- `-109 <= target <= 109`
28 changes: 28 additions & 0 deletions docs/LeetCode/FizzBuzz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Given a number n, for each integer
iin the range from Ito n inclusive,
print one value per line as follows:
• If iis a multiple of both 3and 5,
print FizzBuzz.
• If iis a multiple of 3(but not 5),
print Fizz.
• If iis a multiple of 5(but not 3),
print BUZZ.
• If iis nota multiple of 30r 5, print
the value of i.

Function Description
Complete the function fizzBuzz in
he editor below.
izzBuzz has the following
parameter(s):
intn: upper limit of values to test
inclusive)
Returns: NONE
Prints:
The function must print the
ppropriate response for each value
iin the set {1, 2, ... n} in ascending
order, each on a separate line.
Constraints
• 007<2 x 105

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
[LeetCode - The World's Leading Online Programming Learning Platform](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/?envType=study-plan-v2&envId=leetcode-75)

Companies 公司

You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day, and an integer `fee` representing a transaction fee.
您将获得一个数组 `prices` ,其中 `prices[i]` 是给定股票 `ith` 当天的价格,以及一个 `fee` 表示交易费用的整数。

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.
找到您可以实现的最大利润。您可以完成任意数量的交易,但您需要为每笔交易支付交易费用。

**Note: 注意:**

- You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
您不得同时进行多笔交易(即,您必须在再次买入之前卖出股票)。
- The transaction fee is only charged once for each stock purchase and sale.
每次股票买卖只收取一次交易费。

**Example 1: 示例 1:**

**Input:** prices = [1,3,2,8,4,9], fee = 2
**Output:** 8
**Explanation:** The maximum profit can be achieved by:
- Buying at prices[0] = 1
- Selling at prices[3] = 8
- Buying at prices[4] = 4
- Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

**Example 2: 示例 2:**

**Input:** prices = [1,3,7,5,10,3], fee = 3
**Output:** 6

**Constraints: 约束:**

- `1 <= prices.length <= 5 * 104`
- `1 <= prices[i] < 5 * 104`
- `0 <= fee < 5 * 104`

```java
package org.leetcode;

public class MaxProfit {
public int maxProfit(int[] prices, int fee) {
int n = prices.length;
int buy = -prices[0];
// Max money we can have if we have bought a stock on the ith day
int sell = 0;
// Max money we can have if we have sold a stock on the ith day

for (int i = 1; i < n; i++) {
int prevBuy = buy;
int prevSell = sell;

// Calculate the maximum money we can have if we buy a stock on the current day
buy = Math.max(prevSell - prices[i], prevBuy);
// Calculate the maximum money we can have if we sell a stock on the current day
sell = Math.max(prevBuy + prices[i] - fee, prevSell);
}

return sell;

}
public static void main(String[] args) {
MaxProfit maxProfit = new MaxProfit();

int[] prices1 = {1, 3, 2, 8, 4, 9};
int fee1 = 2;
int result1 = maxProfit.maxProfit(prices1, fee1);
int expect1 = 8;
if (result1 == expect1) {
System.out.println("Pass Max Profit 1: " + result1);
} else {
System.out.println("Fail expected: " + expect1 + " but got: " + result1);
}

int[] prices2 = {1, 3, 7, 5, 10, 3};
int fee2 = 3;
int result2 = maxProfit.maxProfit(prices2, fee2);
int expect2 = 6;
if (result2 == expect2) {
System.out.println("Pass Max Profit 2: " + result2);
} else {
System.out.println("Fail expected: " + expect2 + " but got: " + result2);
}

int[] prices3 = {4, 6, 2, 8, 3, 10, 14};
int fee3 = 3;
int result3 = maxProfit.maxProfit(prices3, fee3);
int expect3 = 11;
if (result3 == expect3) {
System.out.println("Pass Max Profit 3: " + result3);
} else {
System.out.println("Fail expected: " + expect3 + " but got: " + result3);
}
}

}
```
34 changes: 34 additions & 0 deletions docs/LeetCode/confirmation-rate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[LeetCode - The World's Leading Online Programming Learning Platform](https://leetcode.com/problems/confirmation-rate/description/)
Table: `Confirmations`

+----------------+----------+
| Column Name | Type |
+----------------+----------+
| user_id | int |
| time_stamp | datetime |
| action | ENUM |
+----------------+----------+
(user_id, time_stamp) is the primary key (combination of columns with unique values) for this table.
user_id is a foreign key (reference column) to the Signups table.
action is an ENUM (category) of the type ('confirmed', 'timeout')
Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed ('confirmed') or expired without confirming ('timeout').

The **confirmation rate** of a user is the number of `'confirmed'` messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is `0`. Round the confirmation rate to **two decimal** places.

Write a solution to find the **confirmation rate** of each user.

Return the result table in **any order**.

The result format is in the following example.
```sql
# Write your MySQL query statement below
SELECT
S.user_id,
ROUND(IFNULL(SUM(CASE WHEN c.action = 'confirmed' THEN 1 ELSE 0 END) / COUNT(c.action), 0), 2) AS confirmation_rate

FROM Signups S
LEFT JOIN Confirmations C
ON S.user_id = C.user_id

GROUP BY user_id
```
Loading

0 comments on commit 8a51193

Please sign in to comment.