Skip to content

Commit d3144e9

Browse files
authored
Merge branch 'ByteByteGoHq:main' into main
2 parents 94448dd + 0571cff commit d3144e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1242
-4
lines changed

Contribute.md renamed to CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
We’re excited to have you contribute to this repository, and your efforts will be fully credited! Whether you want to add solutions in a new programming language or improve existing ones, here’s how you can get started:
44

5-
1. Let me know what you plan to contribute! The best way to do this is by joining our discord server (see page 2 of the book). Communication is important to prevent you labouring on a set of quesitons that someone's already working on.
5+
1. Let me know what you plan to contribute! The best way to do this is by joining our Discord server (see page 2 of the book). Communication is important to prevent you labouring on a set of quesitons that someone's already working on.
66

77
2. Once you've been approved, **fork the repository** to start working on your own solutions locally.
88

99
3. Take a close look at how the Python solutions are organized. Your contributions should align with this structure (e.g., folder organization, file naming).
1010

1111
4. Always **create a new branch** for your additions or changes. We recommend creating separate branches for each chapter you’re contributing. Example: `git checkout -b java-solutions-linked-lists`.
1212

13-
5. Stay true to existing code comments and structure. Feel free to add additional comments for clarity, especially if any language-specific nuances are included.
13+
5. Stay true to existing variable/function naming, code comments and algorithm structure. Feel free to add additional comments for clarity, especially if any language-specific nuances are included.
1414

1515
6. **Test your code**. Be sure your code runs. We're currently working on making all test cases public. In the mean time, please use your own set of diverse test cases.
1616

1717
7. **Open a pull request** (PR) and clearly describe your work in the PR. Respond to comments and make necessary adjustments when requested. Once approved, your PR will be merged!
1818

1919
## Questions or Issues?
20-
If you encounter any issues or have questions about contributing, feel free to reach out via the discord, or email us at [email protected].
20+
If you encounter any issues or have questions about contributing, feel free to reach out via the Discord, or email us at [email protected].
2121

2222
We greatly appreciate you helping us build this repository and making it accessible to more people. Thank you for contributing!

LICENSE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This work is licensed under [CC BY-NC-ND 4.0](https://creativecommons.org/licenses/by-nc-nd/4.0/?ref=chooser-v1)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ going to reinforce your learning, and better prepare you for high‐stakes codin
1212
https://bit.ly/run-code
1313

1414
## Contributing to the Repo
15-
We welcome contributions to the repository! Of course, your contributions will be credited. If you’d like to add solutions in a language of your choice, or amend an existing solution in any language, please read the instructions provided in [Contribute.md](Contribute.md).
15+
We welcome contributions to the repository! Of course, your contributions will be credited. If you’d like to add solutions in a language of your choice, or amend an existing solution in any language, please read the instructions provided in [CONTRIBUTING.md](CONTRIBUTING.md).
1616

1717
## 📙 About the Book
1818

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
4+
int geometricSequenceTriplets(std::vector<int>& nums, int r) {
5+
// Use 'unordered_map' which initializes non-existent keys to 0
6+
// for frequency counting.
7+
std::unordered_map<int, int> leftMap;
8+
std::unordered_map<int, int> rightMap;
9+
int count = 0;
10+
// Populate 'right_map' with the frequency of each element in the array.
11+
for (const auto& x : nums) {
12+
rightMap[x]++;
13+
}
14+
// Search for geometric triplets that have x as the center.
15+
for (const auto& x : nums) {
16+
// Decrement the frequency of x in 'right_map' since x is now being
17+
// processed and is no longer to the right.
18+
rightMap[x]--;
19+
if (r != 0 && x % r == 0) {
20+
count += leftMap[x / r] * rightMap[x * r];
21+
}
22+
// Increment the frequency of x in 'left_map' since it'll be a part of the
23+
// left side of the array once we iterate to the next value of x.
24+
leftMap[x]++;
25+
}
26+
return count;
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <vector>
2+
#include <unordered_set>
3+
#include <algorithm>
4+
5+
int longestChainOfConsecutiveNumbers(std::vector<int>& nums) {
6+
if (nums.empty()) {
7+
return 0;
8+
}
9+
std::unordered_set<int> numSet(nums.begin(), nums.end());
10+
int longestChain = 0;
11+
for (int num : numSet) {
12+
// If the current number is the smallest number in its chain, search for
13+
// the length of its chain.
14+
if (numSet.find(num - 1) == numSet.end()) {
15+
int currentNum = num;
16+
int currentChain = 1;
17+
// Continue to find the next consecutive numbers in the chain.
18+
while (numSet.find(currentNum + 1) != numSet.end()) {
19+
currentNum += 1;
20+
currentChain += 1;
21+
}
22+
longestChain = std::max(longestChain, currentChain);
23+
}
24+
}
25+
return longestChain;
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <vector>
2+
#include <algorithm>
3+
4+
int longestChainOfConsecutiveNumbersBruteForce(std::vector<int>& nums) {
5+
if (nums.empty()) {
6+
return 0;
7+
}
8+
int longestChain = 0;
9+
// Look for chains of consecutive numbers that start from each number.
10+
for (int num : nums) {
11+
int currentNum = num;
12+
int currentChain = 1;
13+
// Continue to find the next consecutive numbers in the chain.
14+
while (std::find(nums.begin(), nums.end(), currentNum + 1) != nums.end()) {
15+
currentNum += 1;
16+
currentChain += 1;
17+
}
18+
longestChain = std::max(longestChain, currentChain);
19+
}
20+
return longestChain;
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
4+
std::vector<int> pairSumUnsorted(std::vector<int>& nums, int target) {
5+
std::unordered_map<int, int> hashMap;
6+
for (int i = 0; i < nums.size(); ++i) {
7+
int complement = target - nums[i];
8+
if (hashMap.find(complement) != hashMap.end()) {
9+
return {hashMap[complement], i};
10+
}
11+
hashMap[nums[i]] = i;
12+
}
13+
return {};
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
4+
std::vector<int> pairSumUnsortedTwoPass(std::vector<int>& nums, int target) {
5+
std::unordered_map<int, int> numMap;
6+
// First pass: Populate the hash map with each number and its index.
7+
for (int i = 0; i < nums.size(); i++) {
8+
numMap[nums[i]] = i;
9+
}
10+
// Second pass: Check for each number's complement in the hash map.
11+
for (int i = 0; i < nums.size(); i++) {
12+
int complement = target - nums[i];
13+
if (numMap.find(complement) != numMap.end() && numMap[complement] != i) {
14+
return {i, numMap[complement]};
15+
}
16+
}
17+
return {};
18+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <vector>
2+
#include <set>
3+
4+
bool verifySudokuBoard(std::vector<std::vector<int>>& board) {
5+
// Create hash sets for each row, column, and subgrid to keep
6+
// track of numbers previously seen on any given row, column, or
7+
// subgrid.
8+
std::vector<std::set<int>> rowSets(9);
9+
std::vector<std::set<int>> columnSets(9);
10+
std::vector<std::vector<std::set<int>>> subgridSets(3, std::vector<std::set<int>>(3));
11+
for (int r = 0; r < 9; r++) {
12+
for (int c = 0; c < 9; c++) {
13+
int num = board[r][c];
14+
if (num == 0) {
15+
continue;
16+
}
17+
// Check if 'num' has been seen in the current row,
18+
// column, or subgrid.
19+
if (rowSets[r].find(num) != rowSets[r].end()) {
20+
return false;
21+
}
22+
if (columnSets[c].find(num) != columnSets[c].end()) {
23+
return false;
24+
}
25+
if (subgridSets[r / 3][c / 3].find(num) != subgridSets[r / 3][c / 3].end()) {
26+
return false;
27+
}
28+
// If we passed the above checks, mark this value as seen
29+
// by adding it to its corresponding hash sets.
30+
rowSets[r].insert(num);
31+
columnSets[c].insert(num);
32+
subgridSets[r / 3][c / 3].insert(num);
33+
}
34+
}
35+
return true;
36+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
void zeroStriping(std::vector<std::vector<int>>& matrix) {
5+
if (matrix.empty() || matrix[0].empty()) {
6+
return;
7+
}
8+
int m = matrix.size();
9+
int n = matrix[0].size();
10+
// Check if the first row initially contains a zero.
11+
bool firstRowHasZero = false;
12+
for (int c = 0; c < n; c++) {
13+
if (matrix[0][c] == 0) {
14+
firstRowHasZero = true;
15+
break;
16+
}
17+
}
18+
// Check if the first column initially contains a zero.
19+
bool firstColHasZero = false;
20+
for (int r = 0; r < m; r++) {
21+
if (matrix[r][0] == 0) {
22+
firstColHasZero = true;
23+
break;
24+
}
25+
}
26+
// Use the first row and column as markers. If an element in the
27+
// submatrix is zero, mark its corresponding row and column in the
28+
// first row and column as 0.
29+
for (int r = 1; r < m; r++) {
30+
for (int c = 1; c < n; c++) {
31+
if (matrix[r][c] == 0) {
32+
matrix[0][c] = 0;
33+
matrix[r][0] = 0;
34+
}
35+
}
36+
}
37+
// Update the submatrix using the markers in the first row and
38+
// column.
39+
for (int r = 1; r < m; r++) {
40+
for (int c = 1; c < n; c++) {
41+
if (matrix[0][c] == 0 || matrix[r][0] == 0) {
42+
matrix[r][c] = 0;
43+
}
44+
}
45+
}
46+
// If the first row had a zero initially, set all elements in the
47+
// first row to zero.
48+
if (firstRowHasZero) {
49+
for (int c = 0; c < n; c++) {
50+
matrix[0][c] = 0;
51+
}
52+
}
53+
// If the first column had a zero initially, set all elements in
54+
// the first column to zero.
55+
if (firstColHasZero) {
56+
for (int r = 0; r < m; r++) {
57+
matrix[r][0] = 0;
58+
}
59+
}
60+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <vector>
2+
#include <unordered_set>
3+
4+
void zeroStripingHashSets(std::vector<std::vector<int>>& matrix) {
5+
if (matrix.empty() || matrix[0].empty()) {
6+
return;
7+
}
8+
int m = matrix.size();
9+
int n = matrix[0].size();
10+
std::unordered_set<int> zeroRows;
11+
std::unordered_set<int> zeroCols;
12+
// # Pass 1: Traverse through the matrix to identify the rows and
13+
// columns containing zeros and store their indexes in the
14+
// appropriate hash sets.
15+
for (int r = 0; r < m; r++) {
16+
for (int c = 0; c < n; c++) {
17+
if (matrix[r][c] == 0) {
18+
zeroRows.insert(r);
19+
zeroCols.insert(c);
20+
}
21+
}
22+
}
23+
// Pass 2: Set any cell in the matrix to zero if its row index is
24+
// in 'zero_rows' or its column index is in 'zero_cols'.
25+
for (int r = 0; r < m; r++) {
26+
for (int c = 0; c < n; c++) {
27+
if (zeroRows.find(r) != zeroRows.end() || zeroCols.find(c) != zeroCols.end()) {
28+
matrix[r][c] = 0;
29+
}
30+
}
31+
}
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition of MultiLevelListNode:
3+
* class MultiLevelListNode {
4+
* public:
5+
* int val;
6+
* MultiLevelListNode* next;
7+
* MultiLevelListNode* child;
8+
* MultiLevelListNode(int val = 0, MultiLevelListNode* next = nullptr, MultiLevelListNode* child = nullptr)
9+
* : val(val), next(next), child(child) {}
10+
* };
11+
*/
12+
13+
MultiLevelListNode* flattenMultiLevelList(MultiLevelListNode* head) {
14+
if (!head) {
15+
return nullptr;
16+
}
17+
MultiLevelListNode* tail = head;
18+
// Find the tail of the linked list at the first level.
19+
while (tail->next) {
20+
tail = tail->next;
21+
}
22+
MultiLevelListNode* curr = head;
23+
// Process each node at the current level. If a node has a child linked list,
24+
// append it to the tail and then update the tail to the end of the extended
25+
// linked list. Continue until all nodes at the current level are processed.
26+
while (curr) {
27+
if (curr->child) {
28+
tail->next = curr->child;
29+
// Disconnect the child linked list from the current node.
30+
curr->child = nullptr;
31+
while (tail->next) {
32+
tail = tail->next;
33+
}
34+
}
35+
curr = curr->next;
36+
}
37+
return head;
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition of ListNode:
3+
* struct ListNode {
4+
* int val;
5+
* ListNode* next;
6+
* ListNode(int val = 0, ListNode* next = nullptr) : val(val), next(next) {}
7+
* };
8+
*/
9+
10+
ListNode* linkedListIntersection(ListNode* head_A, ListNode* head_B) {
11+
ListNode* ptr_A = head_A;
12+
ListNode* ptr_B = head_B;
13+
// Traverse through list A with 'ptr_A' and list B with 'ptr_B'
14+
// until they meet.
15+
while (ptr_A != ptr_B) {
16+
// Traverse list A -> list B by first traversing 'ptr_A' and
17+
// then, upon reaching the end of list A, continue the
18+
// traversal from the head of list B.
19+
if (ptr_A != nullptr) {
20+
ptr_A = ptr_A->next;
21+
} else {
22+
ptr_A = head_B;
23+
}
24+
// Simultaneously, traverse list B -> list A.
25+
if (ptr_B != nullptr) {
26+
ptr_B = ptr_B->next;
27+
} else {
28+
ptr_B = head_A;
29+
}
30+
}
31+
// At this point, 'ptr_A' and 'ptr_B' either point to the
32+
// intersection node or both are null if the lists do not
33+
// intersect. Return either pointer.
34+
return ptr_A;
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition of ListNode:
3+
* struct ListNode {
4+
* int val;
5+
* ListNode* next;
6+
* ListNode(int val = 0, ListNode* next = nullptr) : val(val), next(next) {}
7+
* };
8+
*/
9+
10+
ListNode* linkedListReversal(ListNode* head) {
11+
ListNode* currNode = head;
12+
ListNode* prevNode = nullptr;
13+
// Reverse the direction of each node's pointer until 'currNode'
14+
// is null.
15+
while (currNode != nullptr) {
16+
ListNode* nextNode = currNode->next;
17+
currNode->next = prevNode;
18+
prevNode = currNode;
19+
currNode = nextNode;
20+
}
21+
// 'prevNode' will be pointing at the head of the reversed linked
22+
// list.
23+
return prevNode;
24+
}

0 commit comments

Comments
 (0)