From 2066323f4bbf9db20c436fff99b6b864e6932d07 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 1 May 2023 00:28:22 +0000 Subject: [PATCH 01/48] Add new daily problem --- .github/data/problems.json | 6 ++++++ .../.gitkeep | 0 2 files changed, 6 insertions(+) create mode 100644 05- May/01- Average Salary Excluding the Minimum and Maximum Salary/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 95f087c63..2c57e7c4d 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -486,5 +486,11 @@ "day": "30", "title": "Remove Max Number of Edges to Keep Graph Fully Traversable" } + ], + "05- May": [ + { + "day": "01", + "title": "Average Salary Excluding the Minimum and Maximum Salary" + } ] } \ No newline at end of file diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/.gitkeep b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/.gitkeep new file mode 100644 index 000000000..e69de29bb From 87837dde0f62dd96b532bc49f894252c09d1fe49 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 2 May 2023 00:54:17 +0000 Subject: [PATCH 02/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/02- Sign of the Product of an Array/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/02- Sign of the Product of an Array/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 2c57e7c4d..3618059de 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -491,6 +491,10 @@ { "day": "01", "title": "Average Salary Excluding the Minimum and Maximum Salary" + }, + { + "day": "02", + "title": "Sign of the Product of an Array" } ] } \ No newline at end of file diff --git a/05- May/02- Sign of the Product of an Array/.gitkeep b/05- May/02- Sign of the Product of an Array/.gitkeep new file mode 100644 index 000000000..e69de29bb From 1d1b6d34381e8f6910d463dfc0e889ec75c41be2 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 3 May 2023 00:27:34 +0000 Subject: [PATCH 03/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/03- Find the Difference of Two Arrays/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/03- Find the Difference of Two Arrays/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 3618059de..492948ea4 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -495,6 +495,10 @@ { "day": "02", "title": "Sign of the Product of an Array" + }, + { + "day": "03", + "title": "Find the Difference of Two Arrays" } ] } \ No newline at end of file diff --git a/05- May/03- Find the Difference of Two Arrays/.gitkeep b/05- May/03- Find the Difference of Two Arrays/.gitkeep new file mode 100644 index 000000000..e69de29bb From f99f307874df0a1183bafc55654b5afddf07d4dd Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 4 May 2023 00:32:13 +0000 Subject: [PATCH 04/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/04- Dota2 Senate/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/04- Dota2 Senate/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 492948ea4..c2da28acf 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -499,6 +499,10 @@ { "day": "03", "title": "Find the Difference of Two Arrays" + }, + { + "day": "04", + "title": "Dota2 Senate" } ] } \ No newline at end of file diff --git a/05- May/04- Dota2 Senate/.gitkeep b/05- May/04- Dota2 Senate/.gitkeep new file mode 100644 index 000000000..e69de29bb From c4fa0548b28b76036b3e7e3da45a1fc408c4295e Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 5 May 2023 00:37:46 +0000 Subject: [PATCH 05/48] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/05- Maximum Number of Vowels in a Substring of Given Length/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index c2da28acf..814f8c3e9 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -503,6 +503,10 @@ { "day": "04", "title": "Dota2 Senate" + }, + { + "day": "05", + "title": "Maximum Number of Vowels in a Substring of Given Length" } ] } \ No newline at end of file diff --git a/05- May/05- Maximum Number of Vowels in a Substring of Given Length/.gitkeep b/05- May/05- Maximum Number of Vowels in a Substring of Given Length/.gitkeep new file mode 100644 index 000000000..e69de29bb From 733953cbe8b7d455ca43ecd4c8797c6f960b4640 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 6 May 2023 00:37:19 +0000 Subject: [PATCH 06/48] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/06- Number of Subsequences That Satisfy the Given Sum Condition/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 814f8c3e9..a9dfea935 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -507,6 +507,10 @@ { "day": "05", "title": "Maximum Number of Vowels in a Substring of Given Length" + }, + { + "day": "06", + "title": "Number of Subsequences That Satisfy the Given Sum Condition" } ] } \ No newline at end of file diff --git a/05- May/06- Number of Subsequences That Satisfy the Given Sum Condition/.gitkeep b/05- May/06- Number of Subsequences That Satisfy the Given Sum Condition/.gitkeep new file mode 100644 index 000000000..e69de29bb From b92fdf3be618a7f7b6847770f8667a54c7fbdbbd Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 7 May 2023 00:39:52 +0000 Subject: [PATCH 07/48] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/07- Find the Longest Valid Obstacle Course at Each Position/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index a9dfea935..d1ddcbc65 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -511,6 +511,10 @@ { "day": "06", "title": "Number of Subsequences That Satisfy the Given Sum Condition" + }, + { + "day": "07", + "title": "Find the Longest Valid Obstacle Course at Each Position" } ] } \ No newline at end of file diff --git a/05- May/07- Find the Longest Valid Obstacle Course at Each Position/.gitkeep b/05- May/07- Find the Longest Valid Obstacle Course at Each Position/.gitkeep new file mode 100644 index 000000000..e69de29bb From 4f9fffd0d692e590bd4793aa460a96ad0d03619c Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 8 May 2023 00:31:19 +0000 Subject: [PATCH 08/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/08- Matrix Diagonal Sum/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/08- Matrix Diagonal Sum/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index d1ddcbc65..cfcdb5ad9 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -515,6 +515,10 @@ { "day": "07", "title": "Find the Longest Valid Obstacle Course at Each Position" + }, + { + "day": "08", + "title": "Matrix Diagonal Sum" } ] } \ No newline at end of file diff --git a/05- May/08- Matrix Diagonal Sum/.gitkeep b/05- May/08- Matrix Diagonal Sum/.gitkeep new file mode 100644 index 000000000..e69de29bb From 3a8feed52397ce5c8c01dfc7011ff568b64689be Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 9 May 2023 00:44:07 +0000 Subject: [PATCH 09/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/09- Spiral Matrix/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/09- Spiral Matrix/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index cfcdb5ad9..30ac76bbb 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -519,6 +519,10 @@ { "day": "08", "title": "Matrix Diagonal Sum" + }, + { + "day": "09", + "title": "Spiral Matrix" } ] } \ No newline at end of file diff --git a/05- May/09- Spiral Matrix/.gitkeep b/05- May/09- Spiral Matrix/.gitkeep new file mode 100644 index 000000000..e69de29bb From 831138ae1ca624056c3cec7db927ebafe38f5f8d Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 10 May 2023 00:33:40 +0000 Subject: [PATCH 10/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/10- Spiral Matrix II/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/10- Spiral Matrix II/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 30ac76bbb..c72160be9 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -523,6 +523,10 @@ { "day": "09", "title": "Spiral Matrix" + }, + { + "day": "10", + "title": "Spiral Matrix II" } ] } \ No newline at end of file diff --git a/05- May/10- Spiral Matrix II/.gitkeep b/05- May/10- Spiral Matrix II/.gitkeep new file mode 100644 index 000000000..e69de29bb From 21f59d5511a047f2e762f447d227909c0ddf3063 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 11 May 2023 00:42:13 +0000 Subject: [PATCH 11/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/11- Uncrossed Lines/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/11- Uncrossed Lines/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index c72160be9..3f3b0c8fc 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -527,6 +527,10 @@ { "day": "10", "title": "Spiral Matrix II" + }, + { + "day": "11", + "title": "Uncrossed Lines" } ] } \ No newline at end of file diff --git a/05- May/11- Uncrossed Lines/.gitkeep b/05- May/11- Uncrossed Lines/.gitkeep new file mode 100644 index 000000000..e69de29bb From bede7e8a615d135638fab1cb73256b3b817ccc3b Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 12 May 2023 00:37:53 +0000 Subject: [PATCH 12/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/12- Solving Questions With Brainpower/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/12- Solving Questions With Brainpower/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 3f3b0c8fc..e90d3d656 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -531,6 +531,10 @@ { "day": "11", "title": "Uncrossed Lines" + }, + { + "day": "12", + "title": "Solving Questions With Brainpower" } ] } \ No newline at end of file diff --git a/05- May/12- Solving Questions With Brainpower/.gitkeep b/05- May/12- Solving Questions With Brainpower/.gitkeep new file mode 100644 index 000000000..e69de29bb From b8cd00e246c4d61c21c73ef2287cdab2173b66d0 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 13 May 2023 00:25:48 +0000 Subject: [PATCH 13/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/13- Count Ways To Build Good Strings/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/13- Count Ways To Build Good Strings/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index e90d3d656..ccc399845 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -535,6 +535,10 @@ { "day": "12", "title": "Solving Questions With Brainpower" + }, + { + "day": "13", + "title": "Count Ways To Build Good Strings" } ] } \ No newline at end of file diff --git a/05- May/13- Count Ways To Build Good Strings/.gitkeep b/05- May/13- Count Ways To Build Good Strings/.gitkeep new file mode 100644 index 000000000..e69de29bb From 97dbca499462055fd156d941f846662fbb3a8107 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 14 May 2023 00:27:28 +0000 Subject: [PATCH 14/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/14- Maximize Score After N Operations/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/14- Maximize Score After N Operations/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ccc399845..0986d48cb 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -539,6 +539,10 @@ { "day": "13", "title": "Count Ways To Build Good Strings" + }, + { + "day": "14", + "title": "Maximize Score After N Operations" } ] } \ No newline at end of file diff --git a/05- May/14- Maximize Score After N Operations/.gitkeep b/05- May/14- Maximize Score After N Operations/.gitkeep new file mode 100644 index 000000000..e69de29bb From 36da88af3820356fbd271ea9591d8b9ed26f6fd6 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 15 May 2023 00:31:59 +0000 Subject: [PATCH 15/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/15- Swapping Nodes in a Linked List/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 0986d48cb..52af9e62f 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -543,6 +543,10 @@ { "day": "14", "title": "Maximize Score After N Operations" + }, + { + "day": "15", + "title": "Swapping Nodes in a Linked List" } ] } \ No newline at end of file diff --git a/05- May/15- Swapping Nodes in a Linked List/.gitkeep b/05- May/15- Swapping Nodes in a Linked List/.gitkeep new file mode 100644 index 000000000..e69de29bb From f7893c620682df1daac954ec4dfc3d6ef4d266d3 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 16 May 2023 00:31:58 +0000 Subject: [PATCH 16/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/16- Swap Nodes in Pairs/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/16- Swap Nodes in Pairs/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 52af9e62f..2464a5b42 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -547,6 +547,10 @@ { "day": "15", "title": "Swapping Nodes in a Linked List" + }, + { + "day": "16", + "title": "Swap Nodes in Pairs" } ] } \ No newline at end of file diff --git a/05- May/16- Swap Nodes in Pairs/.gitkeep b/05- May/16- Swap Nodes in Pairs/.gitkeep new file mode 100644 index 000000000..e69de29bb From 5bb10df4f0f0b6e037da66949259e6232b4e2f37 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 17 May 2023 00:36:14 +0000 Subject: [PATCH 17/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 2464a5b42..ec8dc0827 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -551,6 +551,10 @@ { "day": "16", "title": "Swap Nodes in Pairs" + }, + { + "day": "17", + "title": "Maximum Twin Sum of a Linked List" } ] } \ No newline at end of file diff --git a/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep b/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep new file mode 100644 index 000000000..e69de29bb From 0c3de7003bd5e67270a6c8c8845b55cdd8a0e72b Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 18 May 2023 00:28:02 +0000 Subject: [PATCH 18/48] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/18- Minimum Number of Vertices to Reach All Nodes/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ec8dc0827..029a87496 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -555,6 +555,10 @@ { "day": "17", "title": "Maximum Twin Sum of a Linked List" + }, + { + "day": "18", + "title": "Minimum Number of Vertices to Reach All Nodes" } ] } \ No newline at end of file diff --git a/05- May/18- Minimum Number of Vertices to Reach All Nodes/.gitkeep b/05- May/18- Minimum Number of Vertices to Reach All Nodes/.gitkeep new file mode 100644 index 000000000..e69de29bb From 6fb55e4671e002233f4cc0357bf076f770cfe870 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 19 May 2023 00:32:53 +0000 Subject: [PATCH 19/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/19- Is Graph Bipartite?/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/19- Is Graph Bipartite?/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 029a87496..f4b352015 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -559,6 +559,10 @@ { "day": "18", "title": "Minimum Number of Vertices to Reach All Nodes" + }, + { + "day": "19", + "title": "Is Graph Bipartite?" } ] } \ No newline at end of file diff --git a/05- May/19- Is Graph Bipartite?/.gitkeep b/05- May/19- Is Graph Bipartite?/.gitkeep new file mode 100644 index 000000000..e69de29bb From c3493be221182117820075acc410d22f502085bb Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 20 May 2023 00:27:41 +0000 Subject: [PATCH 20/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/20- Evaluate Division/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/20- Evaluate Division/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index f4b352015..8a6cc2b3d 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -563,6 +563,10 @@ { "day": "19", "title": "Is Graph Bipartite?" + }, + { + "day": "20", + "title": "Evaluate Division" } ] } \ No newline at end of file diff --git a/05- May/20- Evaluate Division/.gitkeep b/05- May/20- Evaluate Division/.gitkeep new file mode 100644 index 000000000..e69de29bb From 832f85303d3633be9d4d4a1dc18ae4ae6865c182 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 21 May 2023 00:31:27 +0000 Subject: [PATCH 21/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/21- Shortest Bridge/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/21- Shortest Bridge/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 8a6cc2b3d..b79aba910 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -567,6 +567,10 @@ { "day": "20", "title": "Evaluate Division" + }, + { + "day": "21", + "title": "Shortest Bridge" } ] } \ No newline at end of file diff --git a/05- May/21- Shortest Bridge/.gitkeep b/05- May/21- Shortest Bridge/.gitkeep new file mode 100644 index 000000000..e69de29bb From 7b3873104e5daa99f3560b7b79219b30785942b5 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 22 May 2023 00:30:48 +0000 Subject: [PATCH 22/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/22- Top K Frequent Elements/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/22- Top K Frequent Elements/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index b79aba910..d0e77ebf4 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -571,6 +571,10 @@ { "day": "21", "title": "Shortest Bridge" + }, + { + "day": "22", + "title": "Top K Frequent Elements" } ] } \ No newline at end of file diff --git a/05- May/22- Top K Frequent Elements/.gitkeep b/05- May/22- Top K Frequent Elements/.gitkeep new file mode 100644 index 000000000..e69de29bb From f7671e57779af4f8509dce15f2aaef99a20ff4b3 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 23 May 2023 00:36:18 +0000 Subject: [PATCH 23/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/23- Kth Largest Element in a Stream/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/23- Kth Largest Element in a Stream/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index d0e77ebf4..b0f66e9cc 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -575,6 +575,10 @@ { "day": "22", "title": "Top K Frequent Elements" + }, + { + "day": "23", + "title": "Kth Largest Element in a Stream" } ] } \ No newline at end of file diff --git a/05- May/23- Kth Largest Element in a Stream/.gitkeep b/05- May/23- Kth Largest Element in a Stream/.gitkeep new file mode 100644 index 000000000..e69de29bb From 3f730289015662bd5f48a2dfaf13b9e91b917373 Mon Sep 17 00:00:00 2001 From: lamasalah32 Date: Tue, 23 May 2023 23:16:27 +0300 Subject: [PATCH 24/48] add 23- Kth Largest Element in a Stream (Lama Salah).cpp --- ...rgest Element in a Stream (Lama Salah).cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 05- May/23- Kth Largest Element in a Stream/23- Kth Largest Element in a Stream (Lama Salah).cpp diff --git a/05- May/23- Kth Largest Element in a Stream/23- Kth Largest Element in a Stream (Lama Salah).cpp b/05- May/23- Kth Largest Element in a Stream/23- Kth Largest Element in a Stream (Lama Salah).cpp new file mode 100644 index 000000000..16e9f9eae --- /dev/null +++ b/05- May/23- Kth Largest Element in a Stream/23- Kth Largest Element in a Stream (Lama Salah).cpp @@ -0,0 +1,39 @@ +// Author: Lama Salah + +class KthLargest { + priority_queue, greater> pq; // Priority queue to store elements in sorted order. + int k; // Declares an integer variable k, representing the kth largest element. + +public: + // Constructor of the KthLargest class. + KthLargest(int k, vector& nums) { + // Initializes the priority queue pq as a min heap. + pq = priority_queue, greater>(); + this->k = k; // Assigns the value of k to the member variable k. + + // Iterate over each element in the vector nums. + for (auto& i : nums) + pq.push(i); // Push the current element into the priority queue pq. + + // Remove the smallest element from pq until its size becomes k. + while (pq.size() > k) + pq.pop(); + } + + // Add a new value to the KthLargest object. + int add(int val) { + pq.push(val); // Push the input value val into the priority queue pq. + + // If the size of pq is greater than k after adding the new value, remove the smallest element from pq. + if (pq.size() > k) pq.pop(); + + // Return the top element of pq, which represents the kth largest element. + return pq.top(); + } +}; + +/** + * Your KthLargest object will be instantiated and called as such: + * KthLargest* obj = new KthLargest(k, nums); + * int param_1 = obj->add(val); + */ \ No newline at end of file From d475ec1aa87c43801a5bc216951827c61f7d1f57 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 24 May 2023 00:30:58 +0000 Subject: [PATCH 25/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/24- Maximum Subsequence Score/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/24- Maximum Subsequence Score/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index b0f66e9cc..59f636cd4 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -579,6 +579,10 @@ { "day": "23", "title": "Kth Largest Element in a Stream" + }, + { + "day": "24", + "title": "Maximum Subsequence Score" } ] } \ No newline at end of file diff --git a/05- May/24- Maximum Subsequence Score/.gitkeep b/05- May/24- Maximum Subsequence Score/.gitkeep new file mode 100644 index 000000000..e69de29bb From 4e135eec8c9fc05644fd03562c9d0317d8916cf4 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 24 May 2023 00:55:29 +0000 Subject: [PATCH 26/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/24- Maximum Subsequence Score/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/24- Maximum Subsequence Score/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index b0f66e9cc..59f636cd4 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -579,6 +579,10 @@ { "day": "23", "title": "Kth Largest Element in a Stream" + }, + { + "day": "24", + "title": "Maximum Subsequence Score" } ] } \ No newline at end of file diff --git a/05- May/24- Maximum Subsequence Score/.gitkeep b/05- May/24- Maximum Subsequence Score/.gitkeep new file mode 100644 index 000000000..e69de29bb From e4556fe4d742d3bca8651bb9398959a3657dc9a0 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 24 May 2023 01:01:35 +0000 Subject: [PATCH 27/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/24- Maximum Subsequence Score/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/24- Maximum Subsequence Score/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index b0f66e9cc..59f636cd4 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -579,6 +579,10 @@ { "day": "23", "title": "Kth Largest Element in a Stream" + }, + { + "day": "24", + "title": "Maximum Subsequence Score" } ] } \ No newline at end of file diff --git a/05- May/24- Maximum Subsequence Score/.gitkeep b/05- May/24- Maximum Subsequence Score/.gitkeep new file mode 100644 index 000000000..e69de29bb From cd2757abf10ea436acf1f507fb732800e2d48f25 Mon Sep 17 00:00:00 2001 From: aboelsooud Date: Wed, 24 May 2023 12:17:15 +0300 Subject: [PATCH 28/48] add the 24'th day problem solution --- ... Subsequence Score (Mahmoud Aboelsoud).cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Mahmoud Aboelsoud).cpp diff --git a/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Mahmoud Aboelsoud).cpp b/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Mahmoud Aboelsoud).cpp new file mode 100644 index 000000000..132b12678 --- /dev/null +++ b/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Mahmoud Aboelsoud).cpp @@ -0,0 +1,52 @@ +// Author: Mahmoud Aboelsoud + +class Solution { +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + // we need to find the maximum score subsequence of size of k + // we caclulate the score by selecting k indexes adn calculate the sum of them from nums1 adn multiply the sum by the minimum value of the selected indexes from nums2 + // we can use a greedy approach to select the k indexes + // we can store the values of nums1 and nums2 in a vector of pairs and sort them in a decreasing order based on the values of nums2 + // after sorting we will to make the second element of the pair as the minimum value of the selected indexes from nums2 + // and multipy it by the smmaion of the max sum of k elements from the first element of the pair in indeces smaller than or equal to i (the selected pos of the min second element of the pair) + + + // v: vector of pairs to store the values of nums1 and nums2 + vector> v; + + // store the values of nums1 and nums2 in v + for(int i = 0; i < nums1.size(); i++){ + v.emplace_back(nums1[i], nums2[i]); + } + + // sort v in a decreasing order based on the values of nums2 + sort(v.begin(), v.end(), [](pair&p1, pair&p2){ + if(p1.second == p2.second) return p1.first > p2.first; + + return p1.second > p2.second; + }); + + // pq: priority queue to store the first element of the pair in a decreasing order + priority_queue, greater> pq; + // sum: the sum of the max sum of k elements from the first element of the pair in indeces smaller than or equal to i (the selected pos of the min second element of the pair) + // ans: the answer + long long sum = 0, ans = 0; + + for(int i = 0; i < v.size(); i++){ + // add the first element of the pair to the sum and push it to the priority queue + sum += v[i].first; + pq.push(v[i].first); + + // if the size of pq is equal to k, update the answer by the max of the current answer and the sum multiplied by the second element of the pair + if(pq.size() == k){ + ans = max(ans, sum * v[i].second); + // subtract the top element of the priority queue from the sum + sum -= pq.top(); + pq.pop(); + } + } + + // return the answer + return ans; + } +}; From a9e770884f42312ac978bbf6afd31817b6ce442c Mon Sep 17 00:00:00 2001 From: Ahmed Hossam <63050133+7oSkaaa@users.noreply.github.com> Date: Wed, 24 May 2023 16:07:22 +0300 Subject: [PATCH 29/48] Create 24- Maximum Subsequence Score (Ahmed Hossam).cpp --- ...ximum Subsequence Score (Ahmed Hossam).cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Ahmed Hossam).cpp diff --git a/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Ahmed Hossam).cpp b/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Ahmed Hossam).cpp new file mode 100644 index 000000000..8ab8ec31a --- /dev/null +++ b/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Ahmed Hossam).cpp @@ -0,0 +1,51 @@ +// Author: Ahmed Hossam + +class Solution { +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + // Get the size of nums1 + int n = nums1.size(); + + // Create a vector of indices from 0 to n - 1 + vector < int > idx(n); + iota(idx.begin(), idx.end(), 0); + + // Sort the indices based on the corresponding values in nums2 + sort(idx.begin(), idx.end(), [&](int i, int j){ + return nums2[i] < nums2[j]; + }); + + // Create a min-heap priority queue + priority_queue < int, vector < int >, greater < int > > pq; + + // Variables to keep track of the current sum and maximum sequence + long long curr_sum = 0, max_seq = 0; + + // Lambda function to add an element to the current sum and the priority queue + auto add = [&](int x){ + curr_sum += x; + pq.push(x); + }; + + // Lambda function to remove the smallest element from the current sum and the priority queue + auto remove = [&](){ + curr_sum -= pq.top(); + pq.pop(); + }; + + // Iterate over the indices in reverse order + for(int i = n - 1; i >= 0; i--){ + // Add the corresponding element from nums1 to the current sum and the priority queue + add(nums1[idx[i]]); + + // If the size of the priority queue reaches k, update the maximum sequence and remove the smallest element + if(pq.size() == k){ + max_seq = max(max_seq, curr_sum * nums2[idx[i]]); + remove(); + } + } + + // Return the maximum sequence + return max_seq; + } +}; From 17b45548829a1aabcd98fd8fd560320a3be591d1 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 24 May 2023 13:08:06 +0000 Subject: [PATCH 30/48] Add new daily problem to README --- 05- May/README.md | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/05- May/README.md b/05- May/README.md index 072bc7bb9..fe5025354 100644 --- a/05- May/README.md +++ b/05- May/README.md @@ -43,6 +43,7 @@ 1. **[Evaluate Division](#20--evaluate-division)** 1. **[Shortest Bridge](#21--shortest-bridge)** 1. **[Kth Largest Element in a Stream](#23--kth-largest-element-in-a-stream)** +1. **[Maximum Subsequence Score](#24--maximum-subsequence-score)**


@@ -1239,4 +1240,72 @@ public: } }; ``` + +
+

+ +## 24) [Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score/) + +### Difficulty + +![](https://img.shields.io/badge/Medium-orange?style=for-the-badge) + +### Related Topic + +`Array` `Greedy` `Sorting` `Heap (Priority Queue)` + +### Code + + +```cpp +class Solution { +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + // Get the size of nums1 + int n = nums1.size(); + + // Create a vector of indices from 0 to n - 1 + vector < int > idx(n); + iota(idx.begin(), idx.end(), 0); + + // Sort the indices based on the corresponding values in nums2 + sort(idx.begin(), idx.end(), [&](int i, int j){ + return nums2[i] < nums2[j]; + }); + + // Create a min-heap priority queue + priority_queue < int, vector < int >, greater < int > > pq; + + // Variables to keep track of the current sum and maximum sequence + long long curr_sum = 0, max_seq = 0; + + // Lambda function to add an element to the current sum and the priority queue + auto add = [&](int x){ + curr_sum += x; + pq.push(x); + }; + + // Lambda function to remove the smallest element from the current sum and the priority queue + auto remove = [&](){ + curr_sum -= pq.top(); + pq.pop(); + }; + + // Iterate over the indices in reverse order + for(int i = n - 1; i >= 0; i--){ + // Add the corresponding element from nums1 to the current sum and the priority queue + add(nums1[idx[i]]); + + // If the size of the priority queue reaches k, update the maximum sequence and remove the smallest element + if(pq.size() == k){ + max_seq = max(max_seq, curr_sum * nums2[idx[i]]); + remove(); + } + } + + // Return the maximum sequence + return max_seq; + } +}; +``` \ No newline at end of file From b7eb338d35cbf0f0dd8c94f395f41077adad21c8 Mon Sep 17 00:00:00 2001 From: Mina Magdy <71466151+MiinaMagdy@users.noreply.github.com> Date: Wed, 24 May 2023 17:15:39 +0300 Subject: [PATCH 31/48] Create 24- Maximum Subsequence Score (Mina Magdy).cpp --- ...Maximum Subsequence Score (Mina Magdy).cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Mina Magdy).cpp diff --git a/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Mina Magdy).cpp b/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Mina Magdy).cpp new file mode 100644 index 000000000..8faf18722 --- /dev/null +++ b/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Mina Magdy).cpp @@ -0,0 +1,42 @@ +// Author: Mina Magdy + +class Solution { +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + int n = nums1.size(); // Number of elements in nums1 + vector idx(n); // Vector to store indices of nums1 + iota(idx.begin(), idx.end(), 0); // Initialize idx with indices from 0 to n-1 + sort(idx.begin(), idx.end(), [&](auto &a, auto &b) { + return nums1[a] < nums1[b]; // Sort idx based on the values of nums1 in ascending order + }); + vector> vec; + for (int k = 0; k < n; k++) { + vec.emplace_back(nums2[idx[k]], k); // Create a vector of pairs (nums2 value, corresponding idx[k]) + } + sort(vec.begin(), vec.end()); // Sort vec in ascending order based on nums2 values + int j = n - 1; // Pointer for iterating nums1 in reverse order + int cnt = 0; // Counter for the number of elements chosen + long long sum = 0, mx = 0; // sum: sum of chosen nums1 values, mx: maximum score + set removedIndices; // Set to keep track of removed indices + for (int v = 0; v < n; v++) { + auto &[vl, id] = vec[v]; // vl: nums2 value, id: corresponding idx[k] + removedIndices.insert(idx[id]); // Add the corresponding idx to removedIndices set + if (id > j) { + cnt--; // Decrement the counter if id is greater than j + sum -= nums1[idx[id]]; // Subtract the value at idx[id] from the sum + } + while (cnt < k - 1 && j >= 0) { + if (removedIndices.count(idx[j])) { // Skip the index if it is already removed + j--; + continue; + } + cnt++; // Increment the counter + sum += nums1[idx[j--]]; // Add the value at idx[j] to the sum and move the pointer j backwards + } + if (cnt == k - 1) { + mx = max(mx, (sum + nums1[idx[id]]) * vl); // Calculate the score and update mx if it's greater + } + } + return mx; // Return the maximum score + } +}; From 25853e91b1f2713584f885e323d377dc342008bb Mon Sep 17 00:00:00 2001 From: Noura Algohary Date: Wed, 24 May 2023 18:40:51 +0300 Subject: [PATCH 32/48] add day 24 challenge --- ...mum Subsequence Score (Noura Algohary).cpp | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Noura Algohary).cpp diff --git a/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Noura Algohary).cpp b/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Noura Algohary).cpp new file mode 100644 index 000000000..1763fff1c --- /dev/null +++ b/05- May/24- Maximum Subsequence Score/24- Maximum Subsequence Score (Noura Algohary).cpp @@ -0,0 +1,59 @@ +// Author: Noura Algohary +class Solution { +public: + void sort2Vectors(vector& vec1, vector& vec2, int n) +{ + pair pairt[n]; + + // Storing the respective array + // elements in pairs. + for (int i = 0; i < n; i++) + { + pairt[i].first = vec2[i]; + pairt[i].second = vec1[i]; + } + + // Sorting the pair array. + sort(pairt, pairt + n); + + // Modifying original vectors + for (int i = 0; i < n; i++) + { + vec2[i] = pairt[i].first; + vec1[i] = pairt[i].second; + } +} + long long maxScore(vector& nums1, vector& nums2, int k) { + priority_queue pq; + long long maxProduct = 0, subSum = 0; + int n = nums2.size(); + + // sort two vectors according to the second one + sort2Vectors(nums1, nums2, n); + + // start with the + for(int i =n-1 ; i> n-k; i--) + { + subSum += nums1[i]; + // negative sign to store nums in ascending order + pq.push(-nums1[i]); + } + + for(int i = n-k; i>=0; i--) + { + // remove the smallest num from sum + subSum += nums1[i]; + + // remove the smallest num from priority queue + pq.push(-nums1[i]); + + if(subSum * nums2[i] > maxProduct) + maxProduct = subSum * nums2[i]; + + subSum += pq.top(); + + pq.pop(); + } + return maxProduct; + } +}; \ No newline at end of file From 851e6a337bf1db6e6b02a8e66ca39bbb671bfc0d Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 25 May 2023 00:33:00 +0000 Subject: [PATCH 33/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/25- New 21 Game/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/25- New 21 Game/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 59f636cd4..55739d23c 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -583,6 +583,10 @@ { "day": "24", "title": "Maximum Subsequence Score" + }, + { + "day": "25", + "title": "New 21 Game" } ] } \ No newline at end of file diff --git a/05- May/25- New 21 Game/.gitkeep b/05- May/25- New 21 Game/.gitkeep new file mode 100644 index 000000000..e69de29bb From ce05cf113f29d292843bbab7b9e93f03fb753f11 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 25 May 2023 00:33:58 +0000 Subject: [PATCH 34/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/25- New 21 Game/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/25- New 21 Game/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 59f636cd4..55739d23c 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -583,6 +583,10 @@ { "day": "24", "title": "Maximum Subsequence Score" + }, + { + "day": "25", + "title": "New 21 Game" } ] } \ No newline at end of file diff --git a/05- May/25- New 21 Game/.gitkeep b/05- May/25- New 21 Game/.gitkeep new file mode 100644 index 000000000..e69de29bb From d8c25ad1a3c6a97866f19717c2918fb8f1817ecd Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 25 May 2023 01:06:29 +0000 Subject: [PATCH 35/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/25- New 21 Game/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/25- New 21 Game/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 59f636cd4..55739d23c 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -583,6 +583,10 @@ { "day": "24", "title": "Maximum Subsequence Score" + }, + { + "day": "25", + "title": "New 21 Game" } ] } \ No newline at end of file diff --git a/05- May/25- New 21 Game/.gitkeep b/05- May/25- New 21 Game/.gitkeep new file mode 100644 index 000000000..e69de29bb From 008afa74cb77a015cd664dac9f722b3ac8d0f851 Mon Sep 17 00:00:00 2001 From: Mina Magdy <71466151+MiinaMagdy@users.noreply.github.com> Date: Thu, 25 May 2023 10:35:09 +0300 Subject: [PATCH 36/48] Create 25- New 21 Game (Mina Magdy).cpp --- .../25- New 21 Game (Mina Magdy).cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 05- May/25- New 21 Game/25- New 21 Game (Mina Magdy).cpp diff --git a/05- May/25- New 21 Game/25- New 21 Game (Mina Magdy).cpp b/05- May/25- New 21 Game/25- New 21 Game (Mina Magdy).cpp new file mode 100644 index 000000000..f222d2352 --- /dev/null +++ b/05- May/25- New 21 Game/25- New 21 Game (Mina Magdy).cpp @@ -0,0 +1,33 @@ +// Author: Mina Magdy + +class Solution { +public: + // Function to calculate the winning probability in the New 21 Game + double new21Game(int n, int k, int maxPts, int curr = 0) { + // Base case: If k is 0, the game is already over, and the probability of winning is 1. + if (k == 0) return 1; + + double dp[n + 1]; // Array to store the probabilities + memset(dp, -1, sizeof(dp)); // Initialize the array with -1 + + double sum = 0; // Variable to store the sum of probabilities + int r = 0; // Variable to store the right endpoint of the interval + + // Fill the probabilities for the rightmost interval [k, min(n, (k-1)+maxPts)] + for (int i = k; i <= min(n, (k - 1) + maxPts); i++) { + dp[i] = 1; // Set the winning probability to 1 for these values + sum += dp[i]; // Update the sum + r = i; // Update the right endpoint + } + + // Calculate the probabilities for the remaining values in reverse order + for (int i = k - 1; i >= 0; i--) { + dp[i] = sum / maxPts; // Calculate the average of the next maxPts values + if (r - i + 1 > maxPts) + sum -= dp[r--]; // If the window size exceeds maxPts, remove the leftmost probability + sum += dp[i]; // Add the current probability to the sum + } + + return dp[0]; // Return the winning probability for starting with 0 points + } +}; From 662c5cd31c0a8c61f8a96b571688094f001bd225 Mon Sep 17 00:00:00 2001 From: Ahmed Hossam <63050133+7oSkaaa@users.noreply.github.com> Date: Thu, 25 May 2023 20:54:50 +0300 Subject: [PATCH 37/48] Create 25- New 21 Game (Ahmed Hossam).cpp --- .../25- New 21 Game (Ahmed Hossam).cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 05- May/25- New 21 Game/25- New 21 Game (Ahmed Hossam).cpp diff --git a/05- May/25- New 21 Game/25- New 21 Game (Ahmed Hossam).cpp b/05- May/25- New 21 Game/25- New 21 Game (Ahmed Hossam).cpp new file mode 100644 index 000000000..f49cd49aa --- /dev/null +++ b/05- May/25- New 21 Game/25- New 21 Game (Ahmed Hossam).cpp @@ -0,0 +1,36 @@ +// Author: Ahmed Hossam + +class Solution { +public: + double new21Game(int n, int k, int maxPts) { + if (k == 0 || n >= k + maxPts) return 1.0; + + // Create a vector to store the probabilities. + vector < double > dp(n + 1); + + // Initialize the current sum and the answer variables. + double currSum = 1.0, ans = 0.0; + + // Set the initial probability for getting 0 points to 1.0. + dp[0] = 1.0; + + // Calculate the probabilities for getting different points. + for (int i = 1; i <= n; i++) { + // Calculate the probability of getting i points. + dp[i] = currSum / maxPts; + + // Update the current sum or the answer based on the value of i. + // If i is less than k, update the current sum. + // Otherwise, update the answer. + (i < k ? currSum : ans) += dp[i]; + + // If i - maxPts is greater than or equal to 0, + // subtract the probability of getting (i - maxPts) points from the current sum. + if (i - maxPts >= 0) + currSum -= dp[i - maxPts]; + } + + // Return the final answer. + return ans; + } +}; From 89d540c25d25cbb90a2501fcae77c290fd64ef46 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 25 May 2023 17:55:23 +0000 Subject: [PATCH 38/48] Add new daily problem to README --- 05- May/README.md | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/05- May/README.md b/05- May/README.md index fe5025354..d508db079 100644 --- a/05- May/README.md +++ b/05- May/README.md @@ -44,6 +44,7 @@ 1. **[Shortest Bridge](#21--shortest-bridge)** 1. **[Kth Largest Element in a Stream](#23--kth-largest-element-in-a-stream)** 1. **[Maximum Subsequence Score](#24--maximum-subsequence-score)** +1. **[New 21 Game](#25--new-21-game)**


@@ -1257,6 +1258,74 @@ public: ### Code +```cpp +class Solution { +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + // Get the size of nums1 + int n = nums1.size(); + + // Create a vector of indices from 0 to n - 1 + vector < int > idx(n); + iota(idx.begin(), idx.end(), 0); + + // Sort the indices based on the corresponding values in nums2 + sort(idx.begin(), idx.end(), [&](int i, int j){ + return nums2[i] < nums2[j]; + }); + + // Create a min-heap priority queue + priority_queue < int, vector < int >, greater < int > > pq; + + // Variables to keep track of the current sum and maximum sequence + long long curr_sum = 0, max_seq = 0; + + // Lambda function to add an element to the current sum and the priority queue + auto add = [&](int x){ + curr_sum += x; + pq.push(x); + }; + + // Lambda function to remove the smallest element from the current sum and the priority queue + auto remove = [&](){ + curr_sum -= pq.top(); + pq.pop(); + }; + + // Iterate over the indices in reverse order + for(int i = n - 1; i >= 0; i--){ + // Add the corresponding element from nums1 to the current sum and the priority queue + add(nums1[idx[i]]); + + // If the size of the priority queue reaches k, update the maximum sequence and remove the smallest element + if(pq.size() == k){ + max_seq = max(max_seq, curr_sum * nums2[idx[i]]); + remove(); + } + } + + // Return the maximum sequence + return max_seq; + } +}; +``` + +
+

+ +## 25) [New 21 Game](https://leetcode.com/problems/new-21-game/) + +### Difficulty + +![](https://img.shields.io/badge/Medium-orange?style=for-the-badge) + +### Related Topic + +`Math` `Dynamic Programming` `Sliding Window` `Probability and Statistics` + +### Code + + ```cpp class Solution { public: From cf4d6ddb7e87fbccb27eac5a883b02d0e0766382 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 26 May 2023 00:00:05 +0000 Subject: [PATCH 39/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/26- Stone Game II/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/26- Stone Game II/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 55739d23c..769d81634 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -587,6 +587,10 @@ { "day": "25", "title": "New 21 Game" + }, + { + "day": "26", + "title": "Stone Game II" } ] } \ No newline at end of file diff --git a/05- May/26- Stone Game II/.gitkeep b/05- May/26- Stone Game II/.gitkeep new file mode 100644 index 000000000..e69de29bb From 24249979db47100b4a2c3f95fb69c4c967eb2e87 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 26 May 2023 00:30:47 +0000 Subject: [PATCH 40/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/26- Stone Game II/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/26- Stone Game II/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 55739d23c..769d81634 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -587,6 +587,10 @@ { "day": "25", "title": "New 21 Game" + }, + { + "day": "26", + "title": "Stone Game II" } ] } \ No newline at end of file diff --git a/05- May/26- Stone Game II/.gitkeep b/05- May/26- Stone Game II/.gitkeep new file mode 100644 index 000000000..e69de29bb From 7a6369d5c6160c1e101e46a703825599a59f8c81 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 26 May 2023 00:59:26 +0000 Subject: [PATCH 41/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/26- Stone Game II/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/26- Stone Game II/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 55739d23c..769d81634 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -587,6 +587,10 @@ { "day": "25", "title": "New 21 Game" + }, + { + "day": "26", + "title": "Stone Game II" } ] } \ No newline at end of file diff --git a/05- May/26- Stone Game II/.gitkeep b/05- May/26- Stone Game II/.gitkeep new file mode 100644 index 000000000..e69de29bb From f4f5aa22ce4675369c994ee7a9d7fefd9a0992b0 Mon Sep 17 00:00:00 2001 From: Mina Magdy <71466151+MiinaMagdy@users.noreply.github.com> Date: Fri, 26 May 2023 14:27:21 +0300 Subject: [PATCH 42/48] Create 26- Stone Game II (Mina Magdy).cpp --- .../26- Stone Game II (Mina Magdy).cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 05- May/26- Stone Game II/26- Stone Game II (Mina Magdy).cpp diff --git a/05- May/26- Stone Game II/26- Stone Game II (Mina Magdy).cpp b/05- May/26- Stone Game II/26- Stone Game II (Mina Magdy).cpp new file mode 100644 index 000000000..bc104a656 --- /dev/null +++ b/05- May/26- Stone Game II/26- Stone Game II (Mina Magdy).cpp @@ -0,0 +1,47 @@ +// Author: Mina Magdy + +// Define a class Solution +class Solution { +public: + vector>> dp; // Dynamic programming memoization table + + // Constructor to initialize the dp table + Solution() { + dp.assign(100, vector>(100, vector(2, -1))); + } + + // Recursive function to play the stone game + int rec(vector& piles, int idx = 0, int M = 1, int turn = 1) { + // Base case: if all piles have been visited, return 0 + if (idx >= piles.size()) + return 0; + + // Check if the current state has already been calculated + int &ret = dp[idx][M][turn]; + if (~ret) + return ret; + + // Turn 1: Alex's turn + if (turn) { + ret = 0; + int sum = 0; + for (int X = 1; idx + X - 1 < piles.size() && X <= 2 * M; X++) { + sum += piles[idx + X - 1]; + ret = max(ret, sum + rec(piles, idx + X, max(M, X), !turn)); + } + } + // Turn 0: Lee's turn + else { + ret = 1e9; + for (int X = 1; idx + X - 1 < piles.size() && X <= 2 * M; X++) { + ret = min(ret, rec(piles, idx + X, max(M, X), !turn)); + } + } + return ret; + } + + // Function to calculate the maximum number of stones Alex can get + int stoneGameII(vector& piles) { + return rec(piles); + } +}; From 89269a109ca7bb0e181234924457f27f61865412 Mon Sep 17 00:00:00 2001 From: Emad Mohamed <108467668+EmadMoh-178@users.noreply.github.com> Date: Fri, 26 May 2023 15:43:55 +0300 Subject: [PATCH 43/48] Create 26- Stone Game II (Emad Mohamed).cpp --- .../26- Stone Game II (Emad Mohamed).cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 05- May/26- Stone Game II/26- Stone Game II (Emad Mohamed).cpp diff --git a/05- May/26- Stone Game II/26- Stone Game II (Emad Mohamed).cpp b/05- May/26- Stone Game II/26- Stone Game II (Emad Mohamed).cpp new file mode 100644 index 000000000..e55786ce0 --- /dev/null +++ b/05- May/26- Stone Game II/26- Stone Game II (Emad Mohamed).cpp @@ -0,0 +1,42 @@ +// Author: Emad Mohamed + /* +0 -> Alice’s turn +1 -> Bob’s turn +We will use dp to try all the possible solutions. +We are calculating dp relative to Alice only so +If turn is 0 (Alice’s turn) then dp refers to the highest score Alice can get. +If turn is 1 (Bob’s turn) then dp refers to the lowest score Alice can get from that position. +So Alice will try to maximize the result because that is her score +while Bob will try to find the minimum result because it’s Alice’s score. +Thus we will only add the sum in Alice’s turn. +*/ +class Solution { + int dp[100][1001][2], n; + int findBest(int idx, int m, bool turn, vector&piles){ + if(idx == n) + return 0; + int& ret = dp[idx][m][turn]; + if(~ret) return ret; + if(!turn){ + //Alice will try to maxmimize + ret = INT_MIN; + int sum = 0; + for(int x = 1; x <= 2 * m && idx + x <= n; x++){ + sum += piles[idx + x - 1]; + ret = max(ret, sum + findBest(idx + x, max(m, x), !turn, piles)); + } + }else{ + //Bob will try to minimize + ret = INT_MAX; + for(int x = 1; x <= 2 * m && idx + x <= n; x++) + ret = min(ret, findBest(idx + x, max(m, x), !turn, piles)); + } + return ret; + } +public: + int stoneGameII(vector& piles) { + memset(dp, -1, sizeof(dp)); + n = piles.size(); + return findBest(0, 1, 0, piles); + } +}; From 99d32a50ed16cb7fd32e86b39f6b3a3ac1e27324 Mon Sep 17 00:00:00 2001 From: Ahmed Hossam <63050133+7oSkaaa@users.noreply.github.com> Date: Fri, 26 May 2023 16:16:38 +0300 Subject: [PATCH 44/48] Create 26- Stone Game II (Ahmed Hossam).cpp --- .../26- Stone Game II (Ahmed Hossam).cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 05- May/26- Stone Game II/26- Stone Game II (Ahmed Hossam).cpp diff --git a/05- May/26- Stone Game II/26- Stone Game II (Ahmed Hossam).cpp b/05- May/26- Stone Game II/26- Stone Game II (Ahmed Hossam).cpp new file mode 100644 index 000000000..984b851ea --- /dev/null +++ b/05- May/26- Stone Game II/26- Stone Game II (Ahmed Hossam).cpp @@ -0,0 +1,41 @@ +// Author: Ahmed Hossam + +class Solution { +public: + int stoneGameII(vector& piles) { + int n = piles.size(); + + // dp[i][m] represents the maximum number of stones the player can obtain + // when starting at pile i with a maximum pick of m. + vector < vector < int > > dp(n, vector < int > (2 * n + 5)); + + // sum[i] stores the sum of stones from pile i to the end. + vector < int > sum(n + 5); + + // Calculate the sum of stones from each pile to the end. + for (int i = n - 1; i >= 0; i--) + sum[i] = piles[i] + sum[i + 1]; + + // Iterate over the piles from right to left. + for (int i = n - 1; i >= 0; i--) { + // Iterate over the maximum pick from 1 to n. + for (int m = 1; m <= n; m++) { + if (i + 2 * m >= n) + // If there are not enough piles remaining, take all the stones. + dp[i][m] = sum[i]; + else { + // Consider all possible picks from 1 to 2 * m. + for (int x = 1; x <= 2 * m; x++) + // Calculate the maximum stones the player can obtain + // by either taking x stones and recursively solving for the remaining piles, + // or not taking any stones and letting the other player play. + dp[i][m] = max(dp[i][m], sum[i] - dp[i + x][max(m, x)]); + } + } + } + + // The maximum number of stones the first player can obtain starting from the first pile + // with a maximum pick of 1 is stored in dp[0][1]. + return dp[0][1]; + } +}; From a622e5edd4079d60735ec0d96d97871a3e7891b3 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 26 May 2023 13:17:23 +0000 Subject: [PATCH 45/48] Add new daily problem to README --- 05- May/README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/05- May/README.md b/05- May/README.md index d508db079..c3786bffb 100644 --- a/05- May/README.md +++ b/05- May/README.md @@ -45,6 +45,7 @@ 1. **[Kth Largest Element in a Stream](#23--kth-largest-element-in-a-stream)** 1. **[Maximum Subsequence Score](#24--maximum-subsequence-score)** 1. **[New 21 Game](#25--new-21-game)** +1. **[Stone Game II](#26--stone-game-ii)**


@@ -1377,4 +1378,62 @@ public: } }; ``` + +
+

+ +## 26) [Stone Game II](https://leetcode.com/problems/stone-game-ii/) + +### Difficulty + +![](https://img.shields.io/badge/Medium-orange?style=for-the-badge) + +### Related Topic + +`Array` `Math` `Dynamic Programming` `Game Theory` + +### Code + + +```cpp +class Solution { +public: + int stoneGameII(vector& piles) { + int n = piles.size(); + + // dp[i][m] represents the maximum number of stones the player can obtain + // when starting at pile i with a maximum pick of m. + vector < vector < int > > dp(n, vector < int > (2 * n + 5)); + + // sum[i] stores the sum of stones from pile i to the end. + vector < int > sum(n + 5); + + // Calculate the sum of stones from each pile to the end. + for (int i = n - 1; i >= 0; i--) + sum[i] = piles[i] + sum[i + 1]; + + // Iterate over the piles from right to left. + for (int i = n - 1; i >= 0; i--) { + // Iterate over the maximum pick from 1 to n. + for (int m = 1; m <= n; m++) { + if (i + 2 * m >= n) + // If there are not enough piles remaining, take all the stones. + dp[i][m] = sum[i]; + else { + // Consider all possible picks from 1 to 2 * m. + for (int x = 1; x <= 2 * m; x++) + // Calculate the maximum stones the player can obtain + // by either taking x stones and recursively solving for the remaining piles, + // or not taking any stones and letting the other player play. + dp[i][m] = max(dp[i][m], sum[i] - dp[i + x][max(m, x)]); + } + } + } + + // The maximum number of stones the first player can obtain starting from the first pile + // with a maximum pick of 1 is stored in dp[0][1]. + return dp[0][1]; + } +}; +``` \ No newline at end of file From 681ed5cf0e4da5e38642575653725ee1b14e513b Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 27 May 2023 00:00:20 +0000 Subject: [PATCH 46/48] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/27- Stone Game III/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/27- Stone Game III/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 769d81634..245aba683 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -591,6 +591,10 @@ { "day": "26", "title": "Stone Game II" + }, + { + "day": "27", + "title": "Stone Game III" } ] } \ No newline at end of file diff --git a/05- May/27- Stone Game III/.gitkeep b/05- May/27- Stone Game III/.gitkeep new file mode 100644 index 000000000..e69de29bb From dcbc24ac36246510481b12921554579a714cbbc2 Mon Sep 17 00:00:00 2001 From: Omar Sanad <108091921+OmarSanad3@users.noreply.github.com> Date: Sat, 27 May 2023 03:31:51 +0300 Subject: [PATCH 47/48] Added Sanad's Solution, May 27th --- .../27- Stone Game III (Omar Sanad).cpp | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 05- May/27- Stone Game III/27- Stone Game III (Omar Sanad).cpp diff --git a/05- May/27- Stone Game III/27- Stone Game III (Omar Sanad).cpp b/05- May/27- Stone Game III/27- Stone Game III (Omar Sanad).cpp new file mode 100644 index 000000000..bfbd4dc87 --- /dev/null +++ b/05- May/27- Stone Game III/27- Stone Game III (Omar Sanad).cpp @@ -0,0 +1,74 @@ +// author : Omar Sanad + +/* We can solve this problem using dynamic programming +we will try for each player to take 1, 2, or 3 stones, +To mark who will win, we can take the sum of Alice's stones as positive, and sum of Bob's stones as negative, +In Alice's turn she will try to maximize the result, as if the final result is positive she wins +In Bob's turn he will try to minimize the resutl, as if the final result is negative he wins +If the final result is zero, it's a tie + */ +class Solution { +public: + // declaring a dp table for memoization + // declaring n (the size of the stones array) + int dp[50005][2], n; + + // declare the stones array inside the class itself, to use it anywhere inside the class + vector stoneValue; + + // the recursion function + int rec(int idx, bool Alice_turn) { + // if we have no stones, we end the game + if (idx >= n) + return 0; + + // initializing a variable ret to store the dp of the current state by reference to it, for ease use + int &ret = dp[idx][Alice_turn]; + + // if we have already calculated this state, then no need to calc it again, we just return its stored value + if (~ret) return ret; + + // else we initialize the ret with either one of the two values + // If this is Alice's turn, we initialize it to INT_MIN, as Alice wants to maximize + // else if this Bob's turn, we initialize it to INT_MAX, as Bob wants to minimize + ret = Alice_turn ? INT_MIN : INT_MAX; + + // declare a variable to store the curren sum which we will store the sum of 1st element, then 1st and 2nd, then 1st,2nd, and 3rd stones + int curSum = 0; + + // if this is Alice's turn, we would like to maximize the answer, and we will add the sum as positive + if (Alice_turn) { // I need to maximize + for (int i = 0; i < 3 && i + idx < n; i++) { + curSum += stoneValue[idx + i]; + ret = max(ret, curSum + rec(i + idx + 1, !Alice_turn)); + } + } + // else if it's Bob's turn, we would like to minimize the answer, and we will add the sum as negative + else{ // I need to minimize + for (int i = 0; i < 3 && i + idx < n; i++) { + curSum -= stoneValue[idx + i]; + ret = min(ret, curSum + rec(i + idx + 1, !Alice_turn)); + } + } + + // return the answer of this state + return ret; + } + + string stoneGameIII(vector& stoneValue) { + // fill the dp table with -1, mark as not yet calculated + memset(dp, -1, sizeof(dp)); + + // assign the given stoneValue to the stoneValue which is declared inside the class itself + this->stoneValue = stoneValue; + + // assign the size of the given array to n + n = stoneValue.size(); + + // return the answer to the problem + // if the answer is "positive" -> "Alice" + // if "negative" -> "Bob" + // if "ZERO" -> "Tie" + return rec(0, true) > 0 ? "Alice" : rec(0, true) < 0 ? "Bob" : "Tie"; + } +}; From 45eb5076eef2b9b217fe26be0cf1ca6dc03af2f6 Mon Sep 17 00:00:00 2001 From: Omar Sanad <108091921+OmarSanad3@users.noreply.github.com> Date: Sat, 27 May 2023 04:03:32 +0300 Subject: [PATCH 48/48] Added also the dp iterative rolling back solution. --- .../27- Stone Game III (Omar Sanad).cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/05- May/27- Stone Game III/27- Stone Game III (Omar Sanad).cpp b/05- May/27- Stone Game III/27- Stone Game III (Omar Sanad).cpp index bfbd4dc87..7a1ee8a0c 100644 --- a/05- May/27- Stone Game III/27- Stone Game III (Omar Sanad).cpp +++ b/05- May/27- Stone Game III/27- Stone Game III (Omar Sanad).cpp @@ -1,5 +1,7 @@ // author : Omar Sanad +/*IMPORTANT: Below you will find another solution using dp iterative with rolling back */ + /* We can solve this problem using dynamic programming we will try for each player to take 1, 2, or 3 stones, To mark who will win, we can take the sum of Alice's stones as positive, and sum of Bob's stones as negative, @@ -72,3 +74,24 @@ class Solution { return rec(0, true) > 0 ? "Alice" : rec(0, true) < 0 ? "Bob" : "Tie"; } }; + + +//================================================================================================================================================================================================================ +//================================================================================================================================================================================================================ +//================================================================================================================================================================================================================ +// the dp iterative solution +class Solution { +public: + string stoneGameIII(vector& sv) { + int dp[4][2]{}, n = sv.size(); + for (int i = 0; i < 3; i++) + sv.push_back(0); + + for (int idx = n - 1; idx >= 0; idx--) { + dp[idx % 4][0] = min({-sv[idx] + dp[(idx + 1) % 4][1], -sv[idx] - sv[idx + 1] + dp[(idx + 2) % 4][1], -sv[idx] - sv[idx + 1] - sv[idx + 2] + dp[(idx + 3) % 4][1]}); + dp[idx % 4][1] = max({sv[idx] + dp[(idx + 1) % 4][0], sv[idx] + sv[idx + 1] + dp[(idx + 2) % 4][0], sv[idx] + sv[idx + 1] + sv[idx + 2] + dp[(idx + 3) % 4][0]}); + } + + return dp[0][true] > 0 ? "Alice" : dp[0][true] < 0 ? "Bob" : "Tie"; + } +};