Skip to content

Commit d28c28c

Browse files
authored
Merge branch 'neetcode-gh:main' into main
2 parents 8d48179 + e4396d1 commit d28c28c

File tree

104 files changed

+3172
-35
lines changed

Some content is hidden

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

104 files changed

+3172
-35
lines changed

Diff for: README.md

+21-21
Large diffs are not rendered by default.

Diff for: cpp/0077-combinations.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
private:
3+
void backtrack(int start, int n, int k, vector<int> &combination, vector<vector<int>> &res){
4+
//base case, when size of combination is k, we wanna stop
5+
if(combination.size() == k){
6+
res.push_back(combination);
7+
return;
8+
}
9+
10+
for(int i = start; i<=n; i++){
11+
combination.push_back(i);
12+
backtrack(i+1, n, k, combination, res);
13+
combination.pop_back();
14+
}
15+
}
16+
public:
17+
vector<vector<int>> combine(int n, int k) {
18+
vector<vector<int>> res;
19+
20+
//initial empty list to pass to the backtrack function
21+
vector<int> emptyCombination;
22+
23+
backtrack(1, n, k, emptyCombination, res);
24+
25+
return res;
26+
}
27+
};

Diff for: cpp/0191-number-of-1-bits.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,20 @@ class Solution {
2525
return result;
2626
}
2727
};
28+
29+
30+
/* use kernighan's algorithm to only iterate num(set bits) times */
31+
class Solution {
32+
public:
33+
int hammingWeight(uint32_t n) {
34+
unsigned int count = 0;
35+
36+
while(n) {
37+
++count;
38+
// unset rightmost set bit
39+
n = (n & (n - 1));
40+
}
41+
42+
return count;
43+
}
44+
};

Diff for: cpp/0787-cheapest-flights-within-k-stops.cpp

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
/**
2+
This function uses the Bellman-Ford algorithm to find the cheapest price from source (src) to destination (dst)
3+
with at most k stops allowed. It iteratively relaxes the edges for k+1 iterations, updating the minimum
4+
cost to reach each vertex. The final result is the minimum cost to reach the destination, or -1 if the
5+
destination is not reachable within the given constraints.
6+
7+
Space Complexity: O(n) - space used for the prices array.
8+
Time Complexity: O(k * |flights|) - k iterations, processing all flights in each iteration.
9+
*/
10+
class Solution {
11+
public:
12+
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
13+
vector<int> prices(n, INT_MAX);
14+
prices[src] = 0;
15+
16+
// Perform k+1 iterations of Bellman-Ford algorithm.
17+
for (int i = 0; i < k + 1; i++) {
18+
vector<int> tmpPrices(begin(prices), end(prices));
19+
20+
for (auto it : flights) {
21+
int s = it[0];
22+
int d = it[1];
23+
int p = it[2];
24+
25+
if (prices[s] == INT_MAX) continue;
26+
27+
if (prices[s] + p < tmpPrices[d]) {
28+
tmpPrices[d] = prices[s] + p;
29+
}
30+
}
31+
prices = tmpPrices;
32+
}
33+
return prices[dst] == INT_MAX ? -1 : prices[dst];
34+
}
35+
};
36+
37+
138
/*
239
Given cities connected by flights [from,to,price], also given src, dst, & k:
340
Return cheapest price from src to dst with at most k stops
@@ -75,4 +112,4 @@ class Solution {
75112
}
76113
return distances[dst];
77114
}
78-
};
115+
};

Diff for: cpp/1091-shortest-path-in-binary-matrix.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
4+
int n = grid.size();
5+
if(grid[0][0]==1 || grid[n-1][n-1]==1) return -1;
6+
7+
// {{r,c},length}
8+
queue<pair<pair<int,int>,int>> q;
9+
set<pair<int,int>> visited;
10+
11+
q.push({{0,0},0});
12+
visited.insert(make_pair(0,0));
13+
14+
int drow[] = {0,1,0,-1,1,-1,1,-1};
15+
int dcol[] = {1,0,-1,0,1,-1,-1,1};
16+
17+
while(!q.empty()){
18+
int row = q.front().first.first;
19+
int col = q.front().first.second;
20+
int len = q.front().second;
21+
22+
if(row == n-1 && col == n-1){
23+
return len+1;
24+
}
25+
26+
for(int i=0; i<8; i++){
27+
int nrow = row + drow[i];
28+
int ncol = col + dcol[i];
29+
30+
if(nrow>=0 && nrow<n && ncol>=0 && ncol<n && visited.find({nrow,ncol})==visited.end() && grid[nrow][ncol]==0){
31+
q.push({{nrow,ncol},len+1});
32+
visited.insert(make_pair(nrow,ncol));
33+
}
34+
}
35+
q.pop();
36+
}
37+
return -1;
38+
}
39+
};

Diff for: cpp/1980-find-unique-binary-string.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
This class implements a solution to find a binary string that differs from a given list of binary strings.
3+
4+
Approach:
5+
1. Define a backtrack function to explore all possible binary strings of the same length as the input strings.
6+
2. Use a set to store the input strings to efficiently check for duplicates.
7+
3. Backtrack through all possible binary strings of length equal to the input strings.
8+
4. If a binary string is found that does not exist in the set of input strings, update the result and return.
9+
10+
Variables:
11+
- result: Holds the result, initialized to an empty string.
12+
- backtrack(): Recursive function to generate binary strings and check for uniqueness.
13+
- inputStrings: Input vector of binary strings.
14+
- currentString: Current binary string being constructed during backtracking.
15+
- stringLength: Length of binary strings in the input.
16+
- stringSet: Set to store input binary strings for fast duplicate checking.
17+
18+
Time Complexity: O(2^N * N), where N is the length of the binary strings.
19+
- The function 'backtrack' explores all possible binary strings of length N, which is O(2^N).
20+
- Checking for uniqueness in the set takes O(1) time on average.
21+
Space Complexity: O(2^N * N) considering the space required to store the generated binary strings during backtracking.
22+
*/
23+
24+
class Solution {
25+
public:
26+
string result = "";
27+
28+
void backtrack(vector<string>& inputStrings, string& currentString, int stringLength, set<string>& stringSet) {
29+
if (!result.empty()) return;
30+
if (currentString.size() == stringLength && stringSet.find(currentString) == stringSet.end()) {
31+
result = currentString;
32+
return;
33+
}
34+
if (currentString.size() > stringLength) return;
35+
36+
for (char ch = '0'; ch <= '1'; ++ch) {
37+
currentString.push_back(ch);
38+
backtrack(inputStrings, currentString, stringLength, stringSet);
39+
currentString.pop_back();
40+
}
41+
}
42+
43+
string findDifferentBinaryString(vector<string>& inputStrings) {
44+
int stringLength = inputStrings[0].size();
45+
string currentString = "";
46+
set<string> stringSet(inputStrings.begin(), inputStrings.end());
47+
48+
backtrack(inputStrings, currentString, stringLength, stringSet);
49+
return result;
50+
}
51+
};

Diff for: csharp/0063-unique-paths-ii.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
public int UniquePathsWithObstacles(int[][] obstacleGrid) {
3+
var rowLength = obstacleGrid.Length;
4+
var colLength = obstacleGrid[0].Length;
5+
6+
int[] row = new int[colLength];
7+
Array.Fill(row, 0);
8+
row[colLength - 1] = 1;
9+
10+
for(int i = rowLength - 1; i >= 0; i--)
11+
{
12+
for(int j = colLength - 1; j >= 0; j--)
13+
{
14+
if(obstacleGrid[i][j] == 1)
15+
row[j] = 0;
16+
else if (j + 1 < colLength)
17+
row[j] = row[j] + row[j + 1];
18+
}
19+
}
20+
return row[0];
21+
}
22+
}

Diff for: go/1669-merge-in-between-linked-lists.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func mergeInBetween(list1 *ListNode, a int, b int, list2 *ListNode) *ListNode {
9+
curr := list1
10+
i := 0
11+
for i < a-1 {
12+
curr = curr.Next
13+
i++
14+
}
15+
16+
head := curr
17+
for i <= b {
18+
curr = curr.Next
19+
i++
20+
}
21+
head.Next = list2
22+
23+
for list2.Next != nil {
24+
list2 = list2.Next
25+
}
26+
27+
list2.Next = curr
28+
29+
return list1
30+
}

Diff for: java/0049-group-anagrams.java

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
class Solution {
2+
public List<List<String>> groupAnagrams(String[] strs) {
3+
Map<String, List<String>> res = new HashMap<>();
4+
5+
for (String s : strs) {
6+
int[] count = new int[26];
7+
8+
for (char c : s.toCharArray()) {
9+
count[c - 'a']++;
10+
}
11+
12+
StringBuilder sb = new StringBuilder();
13+
for (int i = 0; i < 26; i++) {
14+
sb.append('#');
15+
sb.append(count[i]);
16+
}
17+
String key = sb.toString();
18+
19+
if (!res.containsKey(key)) {
20+
res.put(key, new ArrayList<>());
21+
}
22+
res.get(key).add(s);
23+
}
24+
25+
return new ArrayList<>(res.values());
26+
}
27+
}
28+
-------------------------------------------------------------------------
129
class Solution {
230
public List<List<String>> groupAnagrams(String[] strs) {
331
Map<String, List<String>> map = new HashMap<>();

Diff for: java/0059-spiral-matrix-ii.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int[][] generateMatrix(int n) {
3+
int[][] ans = new int[n][n];
4+
5+
int r1=0, r2=n-1;
6+
int c1=0, c2=n-1;
7+
int elem = 1;
8+
while(r2>=r1 && c2>=c1){
9+
for(int i=c1; i<=c2; i++){
10+
ans[r1][i] = elem++;
11+
}
12+
for(int j=r1+1; j<=r2-1; j++){
13+
ans[j][c2] = elem++;
14+
}
15+
if(r2>r1 && c2>c1){
16+
for (int i = c2; i >= c1; i--){
17+
ans[r2][i] = elem++;
18+
}
19+
for (int j = r2-1; j>=r1+1; j--){
20+
ans[j][c1] = elem++;
21+
}
22+
}
23+
r1++;
24+
r2--;
25+
c1++;
26+
c2--;
27+
}
28+
return ans;
29+
}
30+
}

Diff for: java/0095-unique-binary-search-trees-ii.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public List<TreeNode> generateTrees(int n) {
3+
return generate(1, n);
4+
}
5+
6+
private List<TreeNode> generate(int left, int right) {
7+
if (left > right) {
8+
List<TreeNode> r = new ArrayList<>();
9+
r.add(null);
10+
return r;
11+
}
12+
13+
List<TreeNode> res = new ArrayList<>();
14+
for (int val = left; val <= right; val++) {
15+
for (TreeNode leftSubtree : generate(left, val - 1)) {
16+
for (TreeNode rightSubtree: generate(val + 1, right)) {
17+
TreeNode root = new TreeNode(val);
18+
root.left = leftSubtree;
19+
root.right = rightSubtree;
20+
res.add(root);
21+
}
22+
}
23+
}
24+
25+
return res;
26+
}
27+
}

Diff for: java/0144-binary-tree-preorder-traversal.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
18+
static void preOrder(TreeNode root, List<Integer> res){
19+
20+
21+
if(root == null){
22+
return;
23+
}
24+
res.add(root.val);
25+
preOrder(root.left,res);
26+
preOrder(root.right,res);
27+
28+
}
29+
30+
public List<Integer> preorderTraversal(TreeNode root) {
31+
32+
List<Integer> res = new ArrayList<>();
33+
34+
preOrder(root,res);
35+
36+
return res;
37+
38+
}
39+
}

Diff for: java/0201-bitwise-and-of-numbers-range.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int rangeBitwiseAnd(int left, int right) {
3+
int i = 0;
4+
while(left != right){
5+
left = left >> 1;
6+
right = right >> 1;
7+
i += 1;
8+
}
9+
return left << i;
10+
}
11+
}

0 commit comments

Comments
 (0)