Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode Questions #159

Open
wants to merge 175 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
175 commits
Select commit Hold shift + click to select a range
9f2b706
Added README.md file for Set Mismatch
Dante00007 Nov 13, 2024
1fbf3be
Time: 63 ms (12.49%) | Memory: 36.8 MB (6.38%) - LeetSync
Dante00007 Nov 13, 2024
cab72d4
Added README.md file for Largest Number After Digit Swaps by Parity
Dante00007 Nov 14, 2024
3fdf84b
Time: 0 ms (100.00%) | Memory: 9.1 MB (5.45%) - LeetSync
Dante00007 Nov 14, 2024
99e6ffe
Added README.md file for Delete Greatest Value in Each Row
Dante00007 Nov 15, 2024
d0c7af1
Time: 3 ms (52.74%) | Memory: 12.6 MB (45.01%) - LeetSync
Dante00007 Nov 15, 2024
faf7f4e
Added README.md file for Take Gifts From the Richest Pile
Dante00007 Nov 15, 2024
6106af4
Time: 4 ms (31.05%) | Memory: 13 MB (83.30%) - LeetSync
Dante00007 Nov 15, 2024
2b07992
Added README.md file for Take Gifts From the Richest Pile
Dante00007 Nov 15, 2024
516cca2
Added README.md file for Minimum Number Game
Dante00007 Nov 15, 2024
c4c56f7
Time: 0 ms (100.00%) | Memory: 28.8 MB (17.22%) - LeetSync
Dante00007 Nov 15, 2024
485c306
Added README.md file for Final Array State After K Multiplication Ope…
Dante00007 Nov 15, 2024
b2c58fd
Time: 0 ms (100.00%) | Memory: 27.3 MB (22.43%) - LeetSync
Dante00007 Nov 15, 2024
58fef86
Added README.md file for Find X-Sum of All K-Long Subarrays I
Dante00007 Nov 15, 2024
0c01405
Time: 12 ms (70.80%) | Memory: 34.1 MB (73.44%) - LeetSync
Dante00007 Nov 15, 2024
8176916
Added README.md file for Reorganize String
Dante00007 Nov 16, 2024
b701bbd
Time: 4 ms (7.99%) | Memory: 9.4 MB (15.64%) - LeetSync
Dante00007 Nov 16, 2024
7e770e6
Added README.md file for Reduce Array Size to The Half
Dante00007 Nov 17, 2024
34dcd4e
Time: 151 ms (19.66%) | Memory: 84 MB (36.09%) - LeetSync
Dante00007 Nov 17, 2024
8a52ef1
Added README.md file for Task Scheduler
Dante00007 Nov 19, 2024
2fe128d
Time: 31 ms (46.71%) | Memory: 41.7 MB (54.57%) - LeetSync
Dante00007 Nov 19, 2024
a9e10dc
Added README.md file for Kth Smallest Element in a Sorted Matrix
Dante00007 Nov 21, 2024
260420d
Time: 12 ms (30.54%) | Memory: 17 MB (70.18%) - LeetSync
Dante00007 Nov 21, 2024
39f5987
Added README.md file for Sort Characters By Frequency
Dante00007 Nov 21, 2024
00028b4
Time: 11 ms (11.16%) | Memory: 11.3 MB (34.41%) - LeetSync
Dante00007 Nov 21, 2024
8cf3032
Added README.md file for Maximum Number of Events That Can Be Attended
Dante00007 Nov 23, 2024
7499dfb
Time: 69 ms (64.69%) | Memory: 74.6 MB (64.28%) - LeetSync
Dante00007 Nov 23, 2024
d2879d2
Added README.md file for Longest Happy String
Dante00007 Nov 24, 2024
5cbed0d
Time: 0 ms (100.00%) | Memory: 7.8 MB (54.56%) - LeetSync
Dante00007 Nov 24, 2024
3ed8983
Added README.md file for Longest Happy String
Dante00007 Nov 24, 2024
5532e70
Time: 0 ms (100.00%) | Memory: 7.8 MB (27.41%) - LeetSync
Dante00007 Nov 24, 2024
c592917
Added README.md file for Longest Continuous Subarray With Absolute Di…
Dante00007 Nov 24, 2024
d718cf4
Time: 115 ms (48.38%) | Memory: 88.1 MB (51.04%) - LeetSync
Dante00007 Nov 24, 2024
2bf4a5a
Added README.md file for Avoid Flood in The City
Dante00007 Nov 25, 2024
062f4e2
Time: 202 ms (28.21%) | Memory: 116 MB (44.89%) - LeetSync
Dante00007 Nov 25, 2024
184d156
Added README.md file for Check If Array Pairs Are Divisible by k
Dante00007 Nov 27, 2024
1ee5123
Time: 231 ms (5.48%) | Memory: 95.6 MB (5.13%) - LeetSync
Dante00007 Nov 27, 2024
d9f2412
Added README.md file for Minimum Cost to Hire K Workers
Dante00007 Dec 2, 2024
bd44d47
Added README.md file for Search Insert Position
Dante00007 Dec 21, 2024
600762e
Time: 0 ms (100.00%) | Memory: 13.6 MB (15.64%) - LeetSync
Dante00007 Dec 21, 2024
0f4ee71
Added README.md file for Find Minimum in Rotated Sorted Array
Dante00007 Dec 22, 2024
6e86344
Time: 0 ms (100.00%) | Memory: 14.3 MB (5.27%) - LeetSync
Dante00007 Dec 22, 2024
28906e7
Added README.md file for Sqrt(x)
Dante00007 Dec 22, 2024
c5dfbb3
Time: 0 ms (100.00%) | Memory: 8.5 MB (35.40%) - LeetSync
Dante00007 Dec 22, 2024
cd4adbb
Added README.md file for Sqrt(x)
Dante00007 Dec 22, 2024
7155bd9
Time: 0 ms (100.00%) | Memory: 8.6 MB (6.21%) - LeetSync
Dante00007 Dec 22, 2024
2da4329
Added README.md file for Koko Eating Bananas
Dante00007 Dec 23, 2024
ed0b2c9
Time: 10 ms (51.47%) | Memory: 39.4 MB (5.74%) - LeetSync
Dante00007 Dec 23, 2024
20091b3
Added README.md file for Minimum Number of Days to Make m Bouquets
Dante00007 Dec 23, 2024
d714b17
Time: 25 ms (44.27%) | Memory: 70.4 MB (50.71%) - LeetSync
Dante00007 Dec 23, 2024
d615eee
Added README.md file for Number of Provinces
Dante00007 Dec 26, 2024
09465e8
Time: 10 ms (11.22%) | Memory: 24.6 MB (5.07%) - LeetSync
Dante00007 Dec 26, 2024
3b27324
Added README.md file for Number of Islands
Dante00007 Dec 26, 2024
804d943
Time: 51 ms (14.57%) | Memory: 29.1 MB (11.86%) - LeetSync
Dante00007 Dec 26, 2024
50e561a
Added README.md file for Number of Islands
Dante00007 Dec 26, 2024
506bdd5
Time: 52 ms (14.33%) | Memory: 31 MB (8.70%) - LeetSync
Dante00007 Dec 26, 2024
c83c43e
Added README.md file for Number of Islands
Dante00007 Dec 26, 2024
81550ac
Time: 62 ms (12.62%) | Memory: 31.1 MB (8.57%) - LeetSync
Dante00007 Dec 26, 2024
de69431
Added README.md file for Flood Fill
Dante00007 Dec 26, 2024
e83616f
Time: 0 ms (100.00%) | Memory: 18.5 MB (12.52%) - LeetSync
Dante00007 Dec 26, 2024
bea593f
Added README.md file for Rotting Oranges
Dante00007 Dec 26, 2024
1c4a58e
Time: 7 ms (9.76%) | Memory: 17.5 MB (11.66%) - LeetSync
Dante00007 Dec 26, 2024
2ff7e56
Added README.md file for 01 Matrix
Dante00007 Dec 27, 2024
2cb7978
Time: 25 ms (40.72%) | Memory: 39.1 MB (16.88%) - LeetSync
Dante00007 Dec 27, 2024
edf76a7
Added README.md file for Surrounded Regions
Dante00007 Dec 27, 2024
8875673
Time: 1 ms (35.86%) | Memory: 15.7 MB (16.76%) - LeetSync
Dante00007 Dec 27, 2024
b92de05
Added README.md file for Surrounded Regions
Dante00007 Dec 27, 2024
b50154b
Time: 2 ms (31.91%) | Memory: 15.9 MB (15.05%) - LeetSync
Dante00007 Dec 27, 2024
2922cdd
Added README.md file for Number of Enclaves
Dante00007 Dec 27, 2024
1775b4c
Time: 187 ms (10.60%) | Memory: 56.2 MB (6.33%) - LeetSync
Dante00007 Dec 27, 2024
e34d5a2
Added README.md file for Is Graph Bipartite?
Dante00007 Dec 27, 2024
7409404
Time: 0 ms (100.00%) | Memory: 17.5 MB (21.82%) - LeetSync
Dante00007 Dec 27, 2024
2f3d6e9
Added README.md file for Is Graph Bipartite?
Dante00007 Dec 27, 2024
876436a
Time: 1 ms (22.07%) | Memory: 17.1 MB (45.61%) - LeetSync
Dante00007 Dec 27, 2024
6e56fed
Added README.md file for Find Eventual Safe States
Dante00007 Dec 28, 2024
c286a11
Time: 3 ms (96.00%) | Memory: 53 MB (68.51%) - LeetSync
Dante00007 Dec 28, 2024
861c498
Added README.md file for Course Schedule
Dante00007 Dec 28, 2024
bff3b9e
Time: 3 ms (81.26%) | Memory: 19.3 MB (64.67%) - LeetSync
Dante00007 Dec 28, 2024
e1c93d0
Added README.md file for Course Schedule II
Dante00007 Dec 28, 2024
8ec0cda
Time: 2 ms (80.16%) | Memory: 18.1 MB (55.95%) - LeetSync
Dante00007 Dec 28, 2024
69399aa
Added README.md file for Word Ladder
Dante00007 Dec 29, 2024
7644b95
Time: 331 ms (34.57%) | Memory: 21.1 MB (55.61%) - LeetSync
Dante00007 Dec 29, 2024
adcac67
Added README.md file for Shortest Path in Binary Matrix
Dante00007 Dec 30, 2024
d7776ce
Time: 27 ms (45.82%) | Memory: 26.3 MB (38.51%) - LeetSync
Dante00007 Dec 30, 2024
02563e3
Added README.md file for Path With Minimum Effort
Dante00007 Dec 30, 2024
1ec3b3d
Time: 46 ms (84.24%) | Memory: 24.6 MB (45.99%) - LeetSync
Dante00007 Dec 30, 2024
e26ede2
Added README.md file for Cheapest Flights Within K Stops
Dante00007 Dec 30, 2024
6d6f851
Time: 2 ms (69.78%) | Memory: 18.4 MB (21.06%) - LeetSync
Dante00007 Dec 30, 2024
e35f619
Added README.md file for Cheapest Flights Within K Stops
Dante00007 Dec 30, 2024
24a91d8
Time: 7 ms (30.04%) | Memory: 18.3 MB (25.83%) - LeetSync
Dante00007 Dec 30, 2024
493e1c0
Added README.md file for Number of Ways to Arrive at Destination
Dante00007 Dec 30, 2024
f542e64
Time: 9 ms (61.84%) | Memory: 40.3 MB (10.09%) - LeetSync
Dante00007 Dec 30, 2024
af7212b
Added README.md file for Find the City With the Smallest Number of Ne…
Dante00007 Jan 1, 2025
0910e82
Time: 19 ms (37.77%) | Memory: 17 MB (41.32%) - LeetSync
Dante00007 Jan 1, 2025
4bece40
Added README.md file for Number of Provinces
Dante00007 Jan 2, 2025
166a626
Time: 1 ms (55.62%) | Memory: 19.5 MB (63.53%) - LeetSync
Dante00007 Jan 2, 2025
55ea204
Added README.md file for Number of Operations to Make Network Connected
Dante00007 Jan 2, 2025
b4b1a7e
Time: 23 ms (47.98%) | Memory: 47.2 MB (33.28%) - LeetSync
Dante00007 Jan 2, 2025
4c9c9df
Added README.md file for Number of Operations to Make Network Connected
Dante00007 Jan 2, 2025
fa3641b
Time: 23 ms (47.98%) | Memory: 47.2 MB (35.69%) - LeetSync
Dante00007 Jan 2, 2025
8026690
Added README.md file for Accounts Merge
Dante00007 Jan 2, 2025
422c14e
Time: 42 ms (54.48%) | Memory: 40.4 MB (55.04%) - LeetSync
Dante00007 Jan 2, 2025
c575163
Added README.md file for Making A Large Island
Dante00007 Jan 3, 2025
bda4531
Time: 183 ms (72.49%) | Memory: 115.5 MB (64.27%) - LeetSync
Dante00007 Jan 3, 2025
823eb01
Added README.md file for Most Stones Removed with Same Row or Column
Dante00007 Jan 4, 2025
0f5e45e
Time: 24 ms (50.73%) | Memory: 25.3 MB (20.30%) - LeetSync
Dante00007 Jan 4, 2025
23dddc5
Added README.md file for Number of Provinces
Dante00007 Jan 4, 2025
454c62e
Time: 10 ms (11.08%) | Memory: 23.8 MB (5.98%) - LeetSync
Dante00007 Jan 4, 2025
12944c3
Added README.md file for Number of Islands
Dante00007 Jan 4, 2025
8f097b3
Time: 35 ms (23.64%) | Memory: 19.5 MB (39.85%) - LeetSync
Dante00007 Jan 4, 2025
77e0f11
Added README.md file for Number of Islands
Dante00007 Jan 4, 2025
89ef1a1
Time: 19 ms (96.64%) | Memory: 19.4 MB (40.33%) - LeetSync
Dante00007 Jan 4, 2025
9167803
Added README.md file for Create Hello World Function
Dante00007 Jan 7, 2025
677011c
Time: 50 ms (59.17%) | Memory: 47.9 MB (97.23%) - LeetSync
Dante00007 Jan 7, 2025
7e3931c
Added README.md file for Counter
Dante00007 Jan 7, 2025
b4015cd
Time: 60 ms (9.07%) | Memory: 48.6 MB (84.56%) - LeetSync
Dante00007 Jan 7, 2025
ba0afb0
Added README.md file for Minimum Value to Get Positive Step by Step Sum
Dante00007 Jan 11, 2025
db00d6f
Time: 0 ms (100.00%) | Memory: 10 MB (33.87%) - LeetSync
Dante00007 Jan 11, 2025
37bc1ea
Added README.md file for Course Schedule
Dante00007 Jan 11, 2025
16f5e12
Added README.md file for Course Schedule II
Dante00007 Jan 11, 2025
ee6fe3d
Time: 3 ms (75.54%) | Memory: 18.6 MB (44.53%) - LeetSync
Dante00007 Jan 11, 2025
23b6a99
Added README.md file for Find Eventual Safe States
Dante00007 Jan 11, 2025
4298f32
Time: 44 ms (60.34%) | Memory: 66.1 MB (36.14%) - LeetSync
Dante00007 Jan 11, 2025
258a423
Added README.md file for Remove Duplicates from Sorted Array
Dante00007 Jan 13, 2025
0669c20
Time: 0 ms (100.00%) | Memory: 22.7 MB (41.34%) - LeetSync
Dante00007 Jan 13, 2025
172090e
Added README.md file for Remove Duplicates from Sorted List
Dante00007 Jan 13, 2025
15bcadb
Time: 0 ms (100.00%) | Memory: 16.1 MB (76.18%) - LeetSync
Dante00007 Jan 13, 2025
7bbc89e
Added README.md file for Remove Duplicates from Sorted List
Dante00007 Jan 13, 2025
72fef45
Time: 0 ms (100.00%) | Memory: 44.4 MB (18.39%) - LeetSync
Dante00007 Jan 13, 2025
bfeeca1
Added README.md file for Remove Duplicates from Sorted List
Dante00007 Jan 13, 2025
998b905
Time: 0 ms (100.00%) | Memory: 44.4 MB (18.39%) - LeetSync
Dante00007 Jan 13, 2025
b7196c9
Added README.md file for Reverse Linked List
Dante00007 Jan 13, 2025
a0f159f
Time: 0 ms (100.00%) | Memory: 13.3 MB (72.27%) - LeetSync
Dante00007 Jan 13, 2025
5b77983
Added README.md file for Reverse Linked List
Dante00007 Jan 13, 2025
0a4eb85
Time: 0 ms (100.00%) | Memory: 13.7 MB (8.48%) - LeetSync
Dante00007 Jan 13, 2025
61db06b
Added README.md file for Reverse Linked List
Dante00007 Jan 13, 2025
b8987f5
Time: 0 ms (100.00%) | Memory: 52.5 MB (19.15%) - LeetSync
Dante00007 Jan 13, 2025
297c1e0
Added README.md file for Find the Winner of the Circular Game
Dante00007 Jan 22, 2025
a6ef3b6
Time: 0 ms (100.00%) | Memory: 7.9 MB (79.89%) - LeetSync
Dante00007 Jan 22, 2025
8faf5fd
Added README.md file for Maximum Product Subarray
Dante00007 Jan 24, 2025
d61e42b
Time: 364 ms (5.74%) | Memory: 17.6 MB (94.14%) - LeetSync
Dante00007 Jan 24, 2025
aa479c5
Added README.md file for Maximum Product Subarray
Dante00007 Jan 25, 2025
9174a2e
Time: 0 ms (100.00%) | Memory: 17.6 MB (96.03%) - LeetSync
Dante00007 Jan 25, 2025
94899bf
Added README.md file for Maximum Product Subarray
Dante00007 Jan 25, 2025
9de7b93
Time: 0 ms (100.00%) | Memory: 17.8 MB (20.89%) - LeetSync
Dante00007 Jan 25, 2025
e53186a
Added README.md file for Maximum Gap
Dante00007 Jan 25, 2025
52ff9f8
Time: 53 ms (21.20%) | Memory: 85.8 MB (82.26%) - LeetSync
Dante00007 Jan 25, 2025
7273ce4
Added README.md file for Maximum Gap
Dante00007 Jan 26, 2025
a22964a
Time: 20 ms (83.81%) | Memory: 93.2 MB (32.11%) - LeetSync
Dante00007 Jan 26, 2025
5fffc68
Added README.md file for Maximal Square
Dante00007 Jan 26, 2025
a9a8d80
Time: 14 ms (31.96%) | Memory: 31 MB (34.79%) - LeetSync
Dante00007 Jan 26, 2025
3c00b07
Added README.md file for Missing Number
Dante00007 Jan 28, 2025
b3494f3
Time: 0 ms (100.00%) | Memory: 21.9 MB (14.65%) - LeetSync
Dante00007 Jan 28, 2025
c866854
Added README.md file for Contains Duplicate
Dante00007 Jan 28, 2025
c4d75ed
Time: 99 ms (5.23%) | Memory: 77.2 MB (5.87%) - LeetSync
Dante00007 Jan 28, 2025
ca340e1
Added README.md file for Contains Duplicate
Dante00007 Jan 28, 2025
0b89f6b
Time: 88 ms (5.23%) | Memory: 77.3 MB (5.87%) - LeetSync
Dante00007 Jan 28, 2025
50c2e0a
Added README.md file for Combination Sum
Dante00007 Jan 28, 2025
b814640
Time: 4 ms (45.17%) | Memory: 18 MB (30.46%) - LeetSync
Dante00007 Jan 28, 2025
6e89547
Added README.md file for Valid Parenthesis String
Dante00007 Jan 29, 2025
332c17e
Time: 0 ms (100.00%) | Memory: 8.1 MB (51.03%) - LeetSync
Dante00007 Jan 29, 2025
40064d2
Added README.md file for Minimum Right Shifts to Sort the Array
Dante00007 Feb 17, 2025
86fef1a
Time: 0 ms (100.00%) | Memory: 26.9 MB (70.37%) - LeetSync
Dante00007 Feb 17, 2025
934fadc
Added README.md file for Two Sum
Dante00007 Feb 17, 2025
6daecef
Time: 4 ms (61.22%) | Memory: 15.1 MB (15.11%) - LeetSync
Dante00007 Feb 17, 2025
401efa9
Added README.md file for Container With Most Water
Dante00007 Feb 17, 2025
1c03c0b
Time: 0 ms (100.00%) | Memory: 62.8 MB (71.59%) - LeetSync
Dante00007 Feb 17, 2025
102d787
Added README.md file for 3Sum
Dante00007 Feb 17, 2025
bc16bae
Time: 1259 ms (9.39%) | Memory: 295.6 MB (6.13%) - LeetSync
Dante00007 Feb 17, 2025
2ee0c23
Added README.md file for 3Sum
Dante00007 Feb 17, 2025
a7bde6c
Time: 40 ms (91.84%) | Memory: 29 MB (84.98%) - LeetSync
Dante00007 Feb 17, 2025
86db142
Added README.md file for 3Sum Closest
Dante00007 Feb 17, 2025
618b1e7
Time: 15 ms (35.10%) | Memory: 14.1 MB (21.50%) - LeetSync
Dante00007 Feb 17, 2025
e17257a
Added README.md file for Next Permutation
Dante00007 Feb 20, 2025
b0838a5
Time: 0 ms (100.00%) | Memory: 15.8 MB (33.08%) - LeetSync
Dante00007 Feb 20, 2025
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
41 changes: 41 additions & 0 deletions 1-two-sum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<h2><a href="https://leetcode.com/problems/two-sum">Two Sum</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Given an array of integers <code>nums</code>&nbsp;and an integer <code>target</code>, return <em>indices of the two numbers such that they add up to <code>target</code></em>.</p>

<p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p>

<p>You can return the answer in any order.</p>

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

<pre>
<strong>Input:</strong> nums = [2,7,11,15], target = 9
<strong>Output:</strong> [0,1]
<strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,4], target = 6
<strong>Output:</strong> [1,2]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,3], target = 6
<strong>Output:</strong> [0,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>2 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>
<li><strong>Only one valid answer exists.</strong></li>
</ul>

<p>&nbsp;</p>
<strong>Follow-up:&nbsp;</strong>Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code><font face="monospace">&nbsp;</font>time complexity?
19 changes: 19 additions & 0 deletions 1-two-sum/two-sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int> m;

int i=0;

while(i<nums.size()){
int ele = nums[i];
if(m.find(target-ele)!=m.end()){
return {i,m[target-ele]};
}else{
m[ele] = i;
}
i++;
}
return {-1,-1};
}
};
45 changes: 45 additions & 0 deletions 1036-rotting-oranges/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<h2><a href="https://leetcode.com/problems/rotting-oranges">Rotting Oranges</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>You are given an <code>m x n</code> <code>grid</code> where each cell can have one of three values:</p>

<ul>
<li><code>0</code> representing an empty cell,</li>
<li><code>1</code> representing a fresh orange, or</li>
<li><code>2</code> representing a rotten orange.</li>
</ul>

<p>Every minute, any fresh orange that is <strong>4-directionally adjacent</strong> to a rotten orange becomes rotten.</p>

<p>Return <em>the minimum number of minutes that must elapse until no cell has a fresh orange</em>. If <em>this is impossible, return</em> <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/16/oranges.png" style="width: 650px; height: 137px;" />
<pre>
<strong>Input:</strong> grid = [[2,1,1],[1,1,0],[0,1,1]]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[2,1,1],[0,1,1],[1,0,1]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,2]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Since there are already no fresh oranges at minute 0, the answer is just 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 10</code></li>
<li><code>grid[i][j]</code> is <code>0</code>, <code>1</code>, or <code>2</code>.</li>
</ul>
51 changes: 51 additions & 0 deletions 1036-rotting-oranges/rotting-oranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution {
public:
int bfs(vector<vector<int>>& grid,vector<vector<int>>& vis,queue<pair<pair<int,int>,int>> &q){
int m = grid.size();
int n = grid[0].size();

int minute = 0;
while(!q.empty()){
int row = q.front().first.first;
int col = q.front().first.second;
int t = q.front().second;
minute = max(minute,t);
q.pop();
vector<pair<int,int>> dir{{0,-1},{0,1},{-1,0},{1,0}};
for(auto [dr,dc]:dir){
int nbrRow = row + dr;
int nbrCol = col + dc;
if(nbrRow>=0 && nbrCol>=0 && nbrRow<m && nbrCol<n &&
vis[nbrRow][nbrCol]!=2 && grid[nbrRow][nbrCol]==1){
q.push({{nbrRow,nbrCol},t+1});
vis[nbrRow][nbrCol] = 2;
}
}
}
return minute;
}
int orangesRotting(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> vis(m,vector<int>(n,0));
queue<pair<pair<int,int>,int>> q;
for(int i=0;i<m;i++){
for(int j = 0;j<n;j++){
if(grid[i][j]==2){
q.push({{i,j},0});
vis[i][j] = 2;
}
}
}
int time = 0;
time = bfs(grid,vis,q);
for(int i=0;i<m;i++){
for(int j = 0;j<n;j++){
if(vis[i][j]!=2 && grid[i][j]==1){
return -1;
}
}
}
return time;
}
};
32 changes: 32 additions & 0 deletions 1073-number-of-enclaves/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<h2><a href="https://leetcode.com/problems/number-of-enclaves">Number of Enclaves</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>You are given an <code>m x n</code> binary matrix <code>grid</code>, where <code>0</code> represents a sea cell and <code>1</code> represents a land cell.</p>

<p>A <strong>move</strong> consists of walking from one land cell to another adjacent (<strong>4-directionally</strong>) land cell or walking off the boundary of the <code>grid</code>.</p>

<p>Return <em>the number of land cells in</em> <code>grid</code> <em>for which we cannot walk off the boundary of the grid in any number of <strong>moves</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/enclaves1.jpg" style="width: 333px; height: 333px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/enclaves2.jpg" style="width: 333px; height: 333px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All 1s are either on the boundary or can reach the boundary.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 500</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
49 changes: 49 additions & 0 deletions 1073-number-of-enclaves/number-of-enclaves.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution {
public:
void dfs(vector<vector<int>>& grid,vector<vector<int>>& vis,int row,int col){
int m = grid.size();
int n = grid[0].size();
vis[row][col] = 1;
vector<pair<int,int>> dir{{0,1},{0,-1},{1,0},{-1,0}};
for(auto [dr,dc]:dir){
int nbrRow = row +dr;
int nbrCol = col + dc;
if(nbrRow>=0 && nbrCol>=0 && nbrRow<m && nbrCol<n &&
!vis[nbrRow][nbrCol] && grid[nbrRow][nbrCol]!=0){
dfs(grid,vis,nbrRow,nbrCol);
}
}
}
int numEnclaves(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> vis(m,vector<int> (n,0));

for(int j = 0 ; j<n;j++) {
if(!vis[0][j] && grid[0][j] == 1) {
dfs(grid, vis, 0, j);
}
if(!vis[m-1][j] && grid[m-1][j] == 1) {
dfs(grid, vis,m-1,j);
}
}
for(int i = 0;i<m;i++) {
if(!vis[i][0] && grid[i][0] == 1) {
dfs(grid,vis,i,0);
}

if(!vis[i][n-1] && grid[i][n-1] == 1) {
dfs(grid,vis,i,n-1);
}
}
int count = 0;
for(int i = 0;i<m;i++){
for(int j = 0;j<n;j++){
if(grid[i][j]==1 && vis[i][j]==0){
count++;
}
}
}
return count;
}
};
32 changes: 32 additions & 0 deletions 11-container-with-most-water/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<h2><a href="https://leetcode.com/problems/container-with-most-water">Container With Most Water</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>You are given an integer array <code>height</code> of length <code>n</code>. There are <code>n</code> vertical lines drawn such that the two endpoints of the <code>i<sup>th</sup></code> line are <code>(i, 0)</code> and <code>(i, height[i])</code>.</p>

<p>Find two lines that together with the x-axis form a container, such that the container contains the most water.</p>

<p>Return <em>the maximum amount of water a container can store</em>.</p>

<p><strong>Notice</strong> that you may not slant the container.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg" style="width: 600px; height: 287px;" />
<pre>
<strong>Input:</strong> height = [1,8,6,2,5,4,8,3,7]
<strong>Output:</strong> 49
<strong>Explanation:</strong> The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> height = [1,1]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>n == height.length</code></li>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= height[i] &lt;= 10<sup>4</sup></code></li>
</ul>
16 changes: 16 additions & 0 deletions 11-container-with-most-water/container-with-most-water.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int maxArea(vector<int>& nums) {
int n = nums.size();
int j = n-1;
int i = 0;
int maxArea = INT_MIN;
while(i<j){
int area = min(nums[i],nums[j]) * (j-i);
maxArea = max(area,maxArea);
if(nums[i]<nums[j]) i++;
else j--;
}
return maxArea;
}
};
42 changes: 42 additions & 0 deletions 1171-shortest-path-in-binary-matrix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<h2><a href="https://leetcode.com/problems/shortest-path-in-binary-matrix">Shortest Path in Binary Matrix</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>Given an <code>n x n</code> binary matrix <code>grid</code>, return <em>the length of the shortest <strong>clear path</strong> in the matrix</em>. If there is no clear path, return <code>-1</code>.</p>

<p>A <strong>clear path</strong> in a binary matrix is a path from the <strong>top-left</strong> cell (i.e., <code>(0, 0)</code>) to the <strong>bottom-right</strong> cell (i.e., <code>(n - 1, n - 1)</code>) such that:</p>

<ul>
<li>All the visited cells of the path are <code>0</code>.</li>
<li>All the adjacent cells of the path are <strong>8-directionally</strong> connected (i.e., they are different and they share an edge or a corner).</li>
</ul>

<p>The <strong>length of a clear path</strong> is the number of visited cells of this path.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example1_1.png" style="width: 500px; height: 234px;" />
<pre>
<strong>Input:</strong> grid = [[0,1],[1,0]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example2_1.png" style="height: 216px; width: 500px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0],[1,1,0],[1,1,0]]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,0,0],[1,1,0],[1,1,0]]
<strong>Output:</strong> -1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>n == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>grid[i][j] is 0 or 1</code></li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
int n = grid.size();
vector<vector<int>> dist(n,vector<int> (n,1e9));
queue< pair<int,pair<int,int>> > q;

vector<int> dx{-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy{-1, 0, 1, -1, 1, -1, 0, 1};

if(grid[0][0]!=1){
q.push({1,{0,0}});
dist[0][0] = 1;
}
while(!q.empty()){
int cost = q.front().first;
int row = q.front().second.first;
int col = q.front().second.second;
q.pop();
for(int i=0;i<8;i++){
int nurow = dx[i] + row;
int nucol = dy[i] + col;

if(nurow >= 0 && nurow < n && nucol >= 0 && nucol < n &&
(cost+1<dist[nurow][nucol]) && grid[nurow][nucol]==0){
dist[nurow][nucol] = cost + 1;
q.push({cost+1,{nurow,nucol}});
}
}
}
if(dist[n-1][n-1]==1e9) return -1;
return dist[n-1][n-1];
}
};
Loading