-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added c++ Solutions of leetcode Co-Authored-By: Vaibhav Nirmal <[email protected]>
- Loading branch information
1 parent
85f9da9
commit 0f09cea
Showing
1,267 changed files
with
40,117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<h2>542. 01 Matrix</h2><h3>Medium</h3><hr><div><p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> | ||
|
||
<p>The distance between two adjacent cells is <code>1</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-1-grid.jpg" style="width: 253px; height: 253px;"> | ||
<pre><strong>Input:</strong> mat = [[0,0,0],[0,1,0],[0,0,0]] | ||
<strong>Output:</strong> [[0,0,0],[0,1,0],[0,0,0]] | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-2-grid.jpg" style="width: 253px; height: 253px;"> | ||
<pre><strong>Input:</strong> mat = [[0,0,0],[0,1,0],[1,1,1]] | ||
<strong>Output:</strong> [[0,0,0],[0,1,0],[1,2,1]] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>m == mat.length</code></li> | ||
<li><code>n == mat[i].length</code></li> | ||
<li><code>1 <= m, n <= 10<sup>4</sup></code></li> | ||
<li><code>1 <= m * n <= 10<sup>4</sup></code></li> | ||
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li> | ||
<li>There is at least one <code>0</code> in <code>mat</code>.</li> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// { Driver Code Starts | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
|
||
class Solution{ | ||
public: | ||
string onesComplement(string S,int N){ | ||
//code here | ||
int i=0; | ||
string result=""; | ||
while(i<N){ | ||
if(S[i]=='1'){ | ||
result.append("0"); | ||
} | ||
else{ | ||
result.append("1"); | ||
} | ||
i++; | ||
} | ||
return result; | ||
|
||
} | ||
}; | ||
|
||
// { Driver Code Starts. | ||
int main() | ||
{ | ||
int t; | ||
cin>>t; | ||
while(t--) | ||
{ | ||
int n; | ||
cin>>n; | ||
string s; | ||
cin>>s; | ||
Solution ob; | ||
cout<<ob.onesComplement(s,n)<<"\n"; | ||
} | ||
} // } Driver Code Ends |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 1's Complement | ||
## Easy | ||
<div class="problem-statement"> | ||
<p></p><p><span style="font-size:18px">Given an<strong> N </strong>bit binary number, find the 1's complement of the number. The ones' complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa).</span><br> | ||
</p> | ||
|
||
<p><strong><span style="font-size:18px">Example 1:</span></strong></p> | ||
|
||
<pre><strong><span style="font-size:18px">Input:</span> | ||
</strong><span style="font-size:18px">N = 3 | ||
S = 101 | ||
<strong>Output: | ||
</strong>010 | ||
<strong>Explanation: | ||
</strong>We get the output by converting 1's in S | ||
to 0 and 0s to 1</span><strong> | ||
</strong></pre> | ||
|
||
<p><strong><span style="font-size:18px">Example 2:</span></strong></p> | ||
|
||
<pre><span style="font-size:18px"><strong>Input:</strong> | ||
N = 2 | ||
S = 10 | ||
<strong>Output:</strong> | ||
01 | ||
<strong>Explanation:</strong> | ||
We get the output by converting 1's in S | ||
to 0 and 0s to 1</span> | ||
</pre> | ||
|
||
<p><br> | ||
<span style="font-size:18px"><strong>Your Task: </strong><br> | ||
You don't need to read input or print anything. Your task is to complete the function <strong>onesComplement()</strong> which takes the binary string S, its size N<strong> </strong>as input parameters and returns 1's complement of S of size N.</span><br> | ||
</p> | ||
|
||
<p><span style="font-size:18px"><strong>Expected Time Complexity:</strong> O(N)<br> | ||
<strong>Expected Space Complexity:</strong> O(N)</span><br> | ||
</p> | ||
|
||
<p><span style="font-size:18px"><strong>Constraints:</strong><br> | ||
1<=N<=100</span></p> | ||
<p></p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
unordered_map <int,int> m; | ||
vector <int> v; | ||
|
||
for(int i=0;i<nums.size();i++) | ||
{ | ||
if(m.find(target - nums[i]) != m.end()) | ||
{ | ||
v.push_back(i); | ||
v.push_back(m[target-nums[i]]); | ||
} | ||
else | ||
{ | ||
m[nums[i]] = i; | ||
} | ||
} | ||
|
||
return v; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<h2><a href="https://leetcode.com/problems/two-sum/">1. Two Sum</a></h2><h3>Easy</h3><hr><div><p>Given an array of integers <code>nums</code> 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> </p> | ||
<p><strong>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>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [3,2,4], target = 6 | ||
<strong>Output:</strong> [1,2] | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [3,3], target = 6 | ||
<strong>Output:</strong> [0,1] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>2 <= nums.length <= 10<sup>4</sup></code></li> | ||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> | ||
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li> | ||
<li><strong>Only one valid answer exists.</strong></li> | ||
</ul> | ||
|
||
<p> </p> | ||
<strong>Follow-up: </strong>Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<h2><a href="https://leetcode.com/problems/same-tree/">100. Same Tree</a></h2><h3>Easy</h3><hr><div><p>Given the roots of two binary trees <code>p</code> and <code>q</code>, write a function to check if they are the same or not.</p> | ||
|
||
<p>Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg" style="width: 622px; height: 182px;"> | ||
<pre><strong>Input:</strong> p = [1,2,3], q = [1,2,3] | ||
<strong>Output:</strong> true | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg" style="width: 382px; height: 182px;"> | ||
<pre><strong>Input:</strong> p = [1,2], q = [1,null,2] | ||
<strong>Output:</strong> false | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg" style="width: 622px; height: 182px;"> | ||
<pre><strong>Input:</strong> p = [1,2,1], q = [1,1,2] | ||
<strong>Output:</strong> false | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in both trees is in the range <code>[0, 100]</code>.</li> | ||
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li> | ||
</ul> | ||
</div> |
7 changes: 7 additions & 0 deletions
7
1009-complement-of-base-10-integer/1009-complement-of-base-10-integer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Solution { | ||
public: | ||
int bitwiseComplement(int N, int c = 1) { | ||
while (c < N) c = (c << 1) + 1; | ||
return N ^ c; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<h2><a href="https://leetcode.com/problems/complement-of-base-10-integer/">1009. Complement of Base 10 Integer</a></h2><h3>Easy</h3><hr><div><p>The <strong>complement</strong> of an integer is the integer you get when you flip all the <code>0</code>'s to <code>1</code>'s and all the <code>1</code>'s to <code>0</code>'s in its binary representation.</p> | ||
|
||
<ul> | ||
<li>For example, The integer <code>5</code> is <code>"101"</code> in binary and its <strong>complement</strong> is <code>"010"</code> which is the integer <code>2</code>.</li> | ||
</ul> | ||
|
||
<p>Given an integer <code>n</code>, return <em>its complement</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> n = 5 | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation:</strong> 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> n = 7 | ||
<strong>Output:</strong> 0 | ||
<strong>Explanation:</strong> 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10. | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> n = 10 | ||
<strong>Output:</strong> 5 | ||
<strong>Explanation:</strong> 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>0 <= n < 10<sup>9</sup></code></li> | ||
</ul> | ||
|
||
<p> </p> | ||
<p><strong>Note:</strong> This question is the same as 476: <a href="https://leetcode.com/problems/number-complement/" target="_blank">https://leetcode.com/problems/number-complement/</a></p> | ||
</div> |
12 changes: 12 additions & 0 deletions
12
...al-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution { | ||
public: | ||
int numPairsDivisibleBy60(vector<int>& time) { | ||
vector<long long int> modd(60); | ||
long long int total = 0; | ||
for(auto a:time) modd[a%60]++; | ||
for(int i=1;i<=29;i++) total += (modd[i] * modd[60-i]); | ||
total = total + modd[30]*(modd[30]-1)/2 ; | ||
total += modd[0]*(modd[0]-1)/2; | ||
return total; | ||
} | ||
}; |
1 change: 1 addition & 0 deletions
1
1010-pairs-of-songs-with-total-durations-divisible-by-60/NOTES.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
30 changes: 30 additions & 0 deletions
30
1010-pairs-of-songs-with-total-durations-divisible-by-60/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<h2><a href="https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/">1010. Pairs of Songs With Total Durations Divisible by 60</a></h2><h3>Medium</h3><hr><div><p>You are given a list of songs where the <code>i<sup>th</sup></code> song has a duration of <code>time[i]</code> seconds.</p> | ||
|
||
<p>Return <em>the number of pairs of songs for which their total duration in seconds is divisible by</em> <code>60</code>. Formally, we want the number of indices <code>i</code>, <code>j</code> such that <code>i < j</code> with <code>(time[i] + time[j]) % 60 == 0</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> time = [30,20,150,100,40] | ||
<strong>Output:</strong> 3 | ||
<strong>Explanation:</strong> Three pairs have a total duration divisible by 60: | ||
(time[0] = 30, time[2] = 150): total duration 180 | ||
(time[1] = 20, time[3] = 100): total duration 120 | ||
(time[1] = 20, time[4] = 40): total duration 60 | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> time = [60,60,60] | ||
<strong>Output:</strong> 3 | ||
<strong>Explanation:</strong> All three pairs have a total duration of 120, which is divisible by 60. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= time.length <= 6 * 10<sup>4</sup></code></li> | ||
<li><code>1 <= time[i] <= 500</code></li> | ||
</ul> | ||
</div> |
51 changes: 51 additions & 0 deletions
51
...-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class Solution { | ||
public: | ||
bool isPossible(int mid, vector<int>& nums, int days) | ||
{ | ||
int count = 1; | ||
int curr_sum = 0; | ||
for(int i=0;i<nums.size();i++) | ||
{ | ||
if(curr_sum + nums[i] > mid) | ||
{ | ||
count++; | ||
if(count > days) | ||
return false; | ||
|
||
curr_sum = nums[i]; | ||
} | ||
else | ||
{ | ||
curr_sum += nums[i]; | ||
} | ||
} | ||
cout<<"count mid "<<count<<" "<<mid<<endl; | ||
return true; | ||
} | ||
int shipWithinDays(vector<int>& nums, int days) { | ||
|
||
int low = 0; | ||
int high = 0; | ||
|
||
for(int i=0;i<nums.size();i++) | ||
{ | ||
low = max(low,nums[i]); | ||
high += nums[i]; | ||
} | ||
int ans = 0; | ||
while(low <= high) | ||
{ | ||
int mid = (low+high)>>1; | ||
//cout<<mid<<" "; | ||
if(isPossible(mid,nums,days)) | ||
{ | ||
|
||
ans = mid; | ||
high = mid - 1; | ||
} | ||
else | ||
low = mid + 1; | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Oops, something went wrong.