Skip to content

Commit c5b1198

Browse files
Cpp solutions (#44)
* Added-required-code * Added-solution * Updated * formated-readme
1 parent 13d0968 commit c5b1198

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*Runtime: 8 ms, faster than 96.30% of C++ online submissions for Best Time to Buy and Sell Stock II.
2+
Memory Usage: 13.4 MB, less than 5.04% of C++ online submissions for Best Time to Buy and Sell Stock II.
3+
*/
4+
5+
class Solution
6+
{
7+
public:
8+
int maxProfit(vector<int> &prices)
9+
{
10+
int sum = 0;
11+
//If the number of element in the array are zero or one just return zero.
12+
if (prices.size() == 1 or prices.size() == 0)
13+
{
14+
return 0;
15+
}
16+
17+
//Traverse the array and compare the consecutive two elements .
18+
for (int i = 0; i < prices.size() - 1; i++)
19+
{
20+
// If first consecuitve element is less than second subtract both and add in the sum varibale.
21+
if (prices[i] < prices[i + 1])
22+
{
23+
sum += prices[i + 1] - prices[i];
24+
25+
}
26+
}
27+
28+
//Finally return the sum.
29+
return sum;
30+
31+
}
32+
};

C++/Linked-List-Cycle-II.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
class Solution
3+
{
4+
public:
5+
ListNode* detectCycle(ListNode *head)
6+
{
7+
//If head is null or head->next is null dont do anything just return.
8+
if (!head) return NULL;
9+
if (head->next == NULL) return NULL;
10+
//Take two pointers slow and fast both initially pointing to head .
11+
ListNode *fast = head;
12+
ListNode *slow = head;
13+
while (fast and fast->next)
14+
{
15+
// Move fast pointer two step and slow pointer one step forward.
16+
fast = fast->next->next;
17+
slow = slow->next;
18+
//At some time if fast and slow are equal it means there is a cycle .
19+
if (fast == slow) break;
20+
}
21+
22+
if (fast == NULL or fast->next == NULL) return NULL;
23+
//Now we point one pointer to head and increment both the pointers one step untill slow and fast pointer do not become equal.
24+
slow = head;
25+
while (slow != fast)
26+
{
27+
slow = slow->next;
28+
fast = fast->next;
29+
}
30+
31+
return slow;
32+
}
33+
};

C++/Single-Number.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*This solition is the most optiized solution
2+
3+
Time Complexity: O(N)
4+
Space Complexity: O(1)
5+
6+
*/
7+
8+
class Solution
9+
{
10+
public:
11+
int singleNumber(vector<int> &nums)
12+
{
13+
//initialised the xor of numbers in xorr variable
14+
int xorr = 0;
15+
//Linearly traversing the array
16+
for (auto x = 0; x < nums.size(); x++)
17+
{
18+
//Calculating Xor of two numbers .
19+
xorr = xorr ^ nums[x];
20+
21+
}
22+
23+
// Atlast return the final xor .
24+
25+
return xorr;
26+
}
27+
};

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
7070

7171
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
7272
| ---- | --------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | --------- |
73-
| 0136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
73+
| 0136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) <br> [C++](./C++/Single-Number.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
7474
| 0260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium | | |
7575
| 0520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | |
7676

@@ -89,7 +89,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
8989
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | [Java](./Java/Degree-of-an-Array.java) | _O(n)_ | _O(n)_ | Easy | Array | |
9090
| 1089 | [Duplicate Zeroes](https://leetcode.com/problems/duplicate-zeros/) | [JavaScript](./JavaScript/Duplicate-Zeroes.js) | _O(n)_ | _O(n)_ | Easy | Array | |
9191
| 1502 | [Can Make Arithmetic Progression From Sequence](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/) | [Java](./Java/can-make-arithmetic-progression-from-sequence.java) | _O(n)_ | _O(1)_ | Easy | Array | |
92-
| 122 | [Best Time to buy and sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [Python](./Python/best-time-to-buy-and-sell-stock-ii.py) | _O(N)_ | _O(1)_ | Medium | Stocks | |
92+
| 122 | [Best Time to buy and sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [Python](./Python/best-time-to-buy-and-sell-stock-ii.py) <br> [C++](./C++/Best-Time-to-Buy-and-Sell-Stock-II.cpp) | _O(N)_ | _O(1)_ | Medium | Stocks | |
9393
| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | [Python](./Python/pascals-triangle-ii.py) | _O(N^2)_ | _O(K)_ | Easy | | |
9494
| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Java](./Java/running-sum-of-1d-array.java) | _O(N)_ | _O(N)_ | Easy | Simple sum | |
9595
| 42 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [Python](./Python/trapping_rain.py) | _O(N^2)_ | _O(N)_ | Hard | Array | |
@@ -130,7 +130,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
130130
| 19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [Java](./Java/remove-nth-node-from-end-of-list.java) | _O(n)_ | _O(1)_ | Medium | Two pointers | |
131131
| 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Java](./Java/convert-sorted-list-to-binary-search-tree.java) | _O(n)_ | _O(n)_ | Medium | LinkedList | |
132132
| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [Java](./Java/linked-list-cycle.java) | _O(n)_ | _O(1)_ | Easy | Slow-Fast Pointers | |
133-
| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Java](./Java/linked-list-cycle-ii.java) | _O(n)_ | _O(1)_ | Medium | Slow-Fast Pointers | |
133+
| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Java](./Java/linked-list-cycle-ii.java) <br> [C++](./C++/Linked-List-Cycle-II.cpp) | _O(n)_ | _O(1)_ | Medium | Slow-Fast Pointers | |
134134
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [C++](./C++/LRU-Cache.cpp) | _O(1)_ | _O(k)_ | Medium | Hash Map | |
135135
| 186 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Java](./Java/middle-of-the-linked-list.java) | _O(n)_ | _O(1)_ | Easy | Two pointers | |
136136

0 commit comments

Comments
 (0)