Skip to content

Commit

Permalink
Report Contiguous Dates
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Jan 12, 2024
1 parent cd27a18 commit e212d80
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@
| 1220. | [Count Vowels Permutation](https://leetcode.com/problems/count-vowels-permutation/) | [C++](./solutions/count-vowels-permutation/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | | <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> | |
| 1221. | [Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/) | [C++](./solutions/split-a-string-in-balanced-strings/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Counting-7f7333?style=flat-square'/> | |
| 1222. | [Queens That Can Attack the King](https://leetcode.com/problems/queens-that-can-attack-the-king/) | [C++](./solutions/queens-that-can-attack-the-king/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>2</sup>) / O(N<sup>2</sup>)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Matrix-5e337f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 1225. | [Report Contiguous Dates 🔒](https://leetcode.com/problems/report-contiguous-dates/) | [SQL](./extra/sql/report-contiguous-dates/solution.sql), [PY](./extra/pandas/report-contiguous-dates/solution.py) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 1227. | [Airplane Seat Assignment Probability](https://leetcode.com/problems/airplane-seat-assignment-probability/) | [C++](./solutions/airplane-seat-assignment-probability/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(1) / O(1)| <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Brainteaser-7f3333?style=flat-square'/> <img src='https://img.shields.io/badge/Probability and Statistics-7f3933?style=flat-square'/> | |
| 1228. | [Missing Number In Arithmetic Progression 🔒](https://leetcode.com/problems/missing-number-in-arithmetic-progression/) | [C++](./solutions/missing-number-in-arithmetic-progression/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(N) / O(1)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> | |
| 1230. | [Toss Strange Coins 🔒](https://leetcode.com/problems/toss-strange-coins/) | [C++](./solutions/toss-strange-coins/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NT) / O(T)| <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Probability and Statistics-7f3933?style=flat-square'/> | |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 1220. | [Count Vowels Permutation](https://leetcode.com/problems/count-vowels-permutation/) | [C++](./solutions/count-vowels-permutation/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | | <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> | |
| 1221. | [Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/) | [C++](./solutions/split-a-string-in-balanced-strings/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Counting-7f7333?style=flat-square'/> | |
| 1222. | [Queens That Can Attack the King](https://leetcode.com/problems/queens-that-can-attack-the-king/) | [C++](./solutions/queens-that-can-attack-the-king/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>2</sup>) / O(N<sup>2</sup>)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Matrix-5e337f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 1225. | [Report Contiguous Dates 🔒](https://leetcode.com/problems/report-contiguous-dates/) | [SQL](./extra/sql/report-contiguous-dates/solution.sql), [PY](./extra/pandas/report-contiguous-dates/solution.py) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 1227. | [Airplane Seat Assignment Probability](https://leetcode.com/problems/airplane-seat-assignment-probability/) | [C++](./solutions/airplane-seat-assignment-probability/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(1) / O(1)| <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Brainteaser-7f3333?style=flat-square'/> <img src='https://img.shields.io/badge/Probability and Statistics-7f3933?style=flat-square'/> | |
| 1228. | [Missing Number In Arithmetic Progression 🔒](https://leetcode.com/problems/missing-number-in-arithmetic-progression/) | [C++](./solutions/missing-number-in-arithmetic-progression/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(N) / O(1)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> | |
| 1230. | [Toss Strange Coins 🔒](https://leetcode.com/problems/toss-strange-coins/) | [C++](./solutions/toss-strange-coins/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NT) / O(T)| <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Probability and Statistics-7f3933?style=flat-square'/> | |
Expand Down Expand Up @@ -1095,4 +1096,3 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 1547. | [Minimum Cost to Cut a Stick](https://leetcode.com/problems/minimum-cost-to-cut-a-stick/) | [C++](./solutions/minimum-cost-to-cut-a-stick/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | |
| 1549. | [The Most Recent Orders for Each Product 🔒](https://leetcode.com/problems/the-most-recent-orders-for-each-product/) | [SQL](./extra/sql/the-most-recent-orders-for-each-product/solution.sql), [PY](./extra/pandas/the-most-recent-orders-for-each-product/solution.py) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 1550. | [Three Consecutive Odds](https://leetcode.com/problems/three-consecutive-odds/) | [C++](./solutions/three-consecutive-odds/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> | |
| 1551. | [Minimum Operations to Make Array Equal](https://leetcode.com/problems/minimum-operations-to-make-array-equal/) | [C++](./solutions/minimum-operations-to-make-array-equal/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(1) / O(1)| <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> | |
29 changes: 29 additions & 0 deletions extra/pandas/report-contiguous-dates/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pandas as pd


def report_contiguous_dates(
failed: pd.DataFrame, succeeded: pd.DataFrame
) -> pd.DataFrame:
return (
pd.concat(
[
failed.assign(task_state="failed").rename(
columns={"fail_date": "task_date"}
),
succeeded.assign(task_state="succeeded").rename(
columns={"success_date": "task_date"}
),
]
)
.query("task_date.between('2019-01-01', '2019-12-31')")
.sort_values(by="task_date")
.assign(num=lambda x: x.task_date.rank() - x.index)
.groupby(by=["num", "task_state"])
.agg(
start_date=("task_date", "min"),
end_date=("task_date", "max"),
)
.reset_index(names=["num", "period_state"])
.drop(columns="num")
.sort_values(by="start_date")
)
23 changes: 23 additions & 0 deletions extra/sql/report-contiguous-dates/solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
WITH TasksStates AS (
SELECT fail_date AS task_date,
"failed" AS task_state,
ROW_NUMBER() OVER (ORDER BY fail_date) AS num
FROM Failed
WHERE fail_date BETWEEN '2019-01-01' AND '2019-12-31'
UNION ALL
SELECT success_date AS task_date,
"succeeded" AS task_state,
ROW_NUMBER() OVER (ORDER BY success_date) AS num
FROM Succeeded
WHERE success_date BETWEEN '2019-01-01' AND '2019-12-31'
), Tasks AS (
SELECT task_state, task_date,
ROW_NUMBER() OVER (ORDER BY task_date) - num AS num
FROM TasksStates
)
SELECT task_state AS period_state,
MIN(task_date) AS start_date,
MAX(task_date) AS end_date
FROM Tasks
GROUP BY num, task_state
ORDER BY start_date;

0 comments on commit e212d80

Please sign in to comment.