Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
  [0,1,1,1]
]
<strong>输出:</strong>15
<strong>解释:</strong>
<strong>解释:</strong>
边长为 1 的正方形有 <strong>10</strong> 个。
边长为 2 的正方形有 <strong>4</strong> 个。
边长为 3 的正方形有 <strong>1</strong> 个。
Expand All @@ -42,15 +42,15 @@ tags:

<p><strong>示例 2:</strong></p>

<pre><strong>输入:</strong>matrix =
<pre><strong>输入:</strong>matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
<strong>输出:</strong>7
<strong>解释:</strong>
边长为 1 的正方形有 <strong>6</strong> 个。
边长为 1 的正方形有 <strong>6</strong> 个。
边长为 2 的正方形有 <strong>1</strong> 个。
正方形的总数 = 6 + 1 = <strong>7</strong>.
</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
&nbsp; [0,1,1,1]
]
<strong>Output:</strong> 15
<strong>Explanation:</strong>
<strong>Explanation:</strong>
There are <strong>10</strong> squares of side 1.
There are <strong>4</strong> squares of side 2.
There is <strong>1</strong> square of side 3.
Expand All @@ -43,16 +43,16 @@ Total number of squares = 10 + 4 + 1 = <strong>15</strong>.
<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> matrix =
<strong>Input:</strong> matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
<strong>Output:</strong> 7
<strong>Explanation:</strong>
There are <b>6</b> squares of side 1.
There is <strong>1</strong> square of side 2.
<strong>Explanation:</strong>
There are <b>6</b> squares of side 1.
There is <strong>1</strong> square of side 2.
Total number of squares = 6 + 1 = <b>7</b>.
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ tags:
<pre>
<strong>Input:</strong> mat = [[1,0,1],[1,1,0],[1,1,0]]
<strong>Output:</strong> 13
<strong>Explanation:</strong>
<strong>Explanation:</strong>
There are 6 rectangles of side 1x1.
There are 2 rectangles of side 1x2.
There are 3 rectangles of side 2x1.
There is 1 rectangle of side 2x2.
There is 1 rectangle of side 2x2.
There is 1 rectangle of side 3x1.
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
</pre>
Expand All @@ -44,14 +44,14 @@ Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
<pre>
<strong>Input:</strong> mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
<strong>Output:</strong> 24
<strong>Explanation:</strong>
<strong>Explanation:</strong>
There are 8 rectangles of side 1x1.
There are 5 rectangles of side 1x2.
There are 2 rectangles of side 1x3.
There are 2 rectangles of side 1x3.
There are 4 rectangles of side 2x1.
There are 2 rectangles of side 2x2.
There are 2 rectangles of side 3x1.
There is 1 rectangle of side 3x2.
There are 2 rectangles of side 2x2.
There are 2 rectangles of side 3x1.
There is 1 rectangle of side 3x2.
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3648.Minimum%20Sensors%20to%20Cover%20Grid/README.md
rating: 1395
source: 第 163 场双周赛 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3648.Minimum%20Sensors%20to%20Cover%20Grid/README_EN.md
rating: 1395
source: Biweekly Contest 163 Q1
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3649.Number of Perfect Pairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3649.Number%20of%20Perfect%20Pairs/README.md
rating: 1715
source: 第 163 场双周赛 Q2
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3649.Number of Perfect Pairs/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3649.Number%20of%20Perfect%20Pairs/README_EN.md
rating: 1715
source: Biweekly Contest 163 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3650.Minimum%20Cost%20Path%20with%20Edge%20Reversals/README.md
rating: 1853
source: 第 163 场双周赛 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3650.Minimum%20Cost%20Path%20with%20Edge%20Reversals/README_EN.md
rating: 1853
source: Biweekly Contest 163 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3651.Minimum%20Cost%20Path%20with%20Teleportations/README.md
rating: 2411
source: 第 163 场双周赛 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3651.Minimum%20Cost%20Path%20with%20Teleportations/README_EN.md
rating: 2411
source: Biweekly Contest 163 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3652.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20using%20Strategy/README.md
rating: 1556
source: 第 463 场周赛 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3652.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20using%20Strategy/README_EN.md
rating: 1556
source: Weekly Contest 463 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3653.XOR%20After%20Range%20Multiplication%20Queries%20I/README.md
rating: 1556
source: 第 463 场周赛 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3653.XOR%20After%20Range%20Multiplication%20Queries%20I/README_EN.md
rating: 1556
source: Weekly Contest 463 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3654.Minimum%20Sum%20After%20Divisible%20Sum%20Deletions/README.md
rating: 2038
source: 第 463 场周赛 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3654.Minimum%20Sum%20After%20Divisible%20Sum%20Deletions/README_EN.md
rating: 2038
source: Weekly Contest 463 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3655.XOR%20After%20Range%20Multiplication%20Queries%20II/README.md
rating: 2453
source: 第 463 场周赛 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3655.XOR%20After%20Range%20Multiplication%20Queries%20II/README_EN.md
rating: 2453
source: Weekly Contest 463 Q4
---

<!-- problem:start -->
Expand Down
156 changes: 156 additions & 0 deletions solution/3600-3699/3657.Find Loyal Customers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3657.Find%20Loyal%20Customers/README.md
tags:
- 数据库
---

<!-- problem:start -->

# [3657. Find Loyal Customers](https://leetcode.cn/problems/find-loyal-customers)

[English Version](/solution/3600-3699/3657.Find%20Loyal%20Customers/README_EN.md)

## 题目描述

<!-- description:start -->

<p>Table: <code>customer_transactions</code></p>

<pre>
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| transaction_id | int |
| customer_id | int |
| transaction_date | date |
| amount | decimal |
| transaction_type | varchar |
+------------------+---------+
transaction_id is the unique identifier for this table.
transaction_type can be either &#39;purchase&#39; or &#39;refund&#39;.
</pre>

<p>Write a solution to find <strong>loyal customers</strong>. A customer is considered <strong>loyal</strong> if they meet ALL the following criteria:</p>

<ul>
<li>Made <strong>at least</strong>&nbsp;<code><font face="monospace">3</font></code>&nbsp;purchase transactions.</li>
<li>Have been active for <strong>at least</strong> <code>30</code> days.</li>
<li>Their <strong>refund rate</strong> is less than <code>20%</code> .</li>
</ul>

<p>Return <em>the result table&nbsp;ordered by</em> <code>customer_id</code> <em>in <strong>ascending</strong> order</em>.</p>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p>customer_transactions table:</p>

<pre class="example-io">
+----------------+-------------+------------------+--------+------------------+
| transaction_id | customer_id | transaction_date | amount | transaction_type |
+----------------+-------------+------------------+--------+------------------+
| 1 | 101 | 2024-01-05 | 150.00 | purchase |
| 2 | 101 | 2024-01-15 | 200.00 | purchase |
| 3 | 101 | 2024-02-10 | 180.00 | purchase |
| 4 | 101 | 2024-02-20 | 250.00 | purchase |
| 5 | 102 | 2024-01-10 | 100.00 | purchase |
| 6 | 102 | 2024-01-12 | 120.00 | purchase |
| 7 | 102 | 2024-01-15 | 80.00 | refund |
| 8 | 102 | 2024-01-18 | 90.00 | refund |
| 9 | 102 | 2024-02-15 | 130.00 | purchase |
| 10 | 103 | 2024-01-01 | 500.00 | purchase |
| 11 | 103 | 2024-01-02 | 450.00 | purchase |
| 12 | 103 | 2024-01-03 | 400.00 | purchase |
| 13 | 104 | 2024-01-01 | 200.00 | purchase |
| 14 | 104 | 2024-02-01 | 250.00 | purchase |
| 15 | 104 | 2024-02-15 | 300.00 | purchase |
| 16 | 104 | 2024-03-01 | 350.00 | purchase |
| 17 | 104 | 2024-03-10 | 280.00 | purchase |
| 18 | 104 | 2024-03-15 | 100.00 | refund |
+----------------+-------------+------------------+--------+------------------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+-------------+
| customer_id |
+-------------+
| 101 |
| 104 |
+-------------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li><strong>Customer 101</strong>:

<ul>
<li>Purchase transactions: 4 (IDs: 1, 2, 3, 4)&nbsp;</li>
<li>Refund transactions: 0</li>
<li>Refund rate: 0/4 = 0% (less than 20%)&nbsp;</li>
<li>Active period: Jan 5 to Feb 20 = 46 days (at least 30 days)&nbsp;</li>
<li>Qualifies as loyal&nbsp;</li>
</ul>
</li>
<li><strong>Customer 102</strong>:
<ul>
<li>Purchase transactions: 3 (IDs: 5, 6, 9)&nbsp;</li>
<li>Refund transactions: 2 (IDs: 7, 8)</li>
<li>Refund rate: 2/5 = 40% (exceeds 20%)&nbsp;</li>
<li>Not loyal&nbsp;</li>
</ul>
</li>
<li><strong>Customer 103</strong>:
<ul>
<li>Purchase transactions: 3 (IDs: 10, 11, 12)&nbsp;</li>
<li>Refund transactions: 0</li>
<li>Refund rate: 0/3 = 0% (less than 20%)&nbsp;</li>
<li>Active period: Jan 1 to Jan 3 = 2 days (less than 30 days)&nbsp;</li>
<li>Not loyal&nbsp;</li>
</ul>
</li>
<li><strong>Customer 104</strong>:
<ul>
<li>Purchase transactions: 5 (IDs: 13, 14, 15, 16, 17)&nbsp;</li>
<li>Refund transactions: 1 (ID: 18)</li>
<li>Refund rate: 1/6 = 16.67% (less than 20%)&nbsp;</li>
<li>Active period: Jan 1 to Mar 15 = 73 days (at least 30 days)&nbsp;</li>
<li>Qualifies as loyal&nbsp;</li>
</ul>
</li>

</ul>

<p>The result table is ordered by customer_id in ascending order.</p>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### MySQL

```sql

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading