-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
117 changed files
with
4,917 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,5 +16,6 @@ | |
"landscape": false, | ||
"margin": "2", | ||
"downscalePercent": 100 | ||
} | ||
}, | ||
"tabSize": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"accentColor": "", | ||
"baseFontSize": 16, | ||
"baseFontSize": 17, | ||
"baseFontSizeAction": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
99 changes: 99 additions & 0 deletions
99
docs/LeetCode/best-time-to-buy-and-sell-stock-with-transaction-fee.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
Oops, something went wrong.