-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b0c1ae
commit 72e4ad5
Showing
12 changed files
with
328 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 |
---|---|---|
|
@@ -354,3 +354,6 @@ Footer navigation | |
Terms | ||
Privacy | ||
Security | ||
|
||
*.DS_Store | ||
|
35 changes: 35 additions & 0 deletions
35
src/NeetCode.Solutions/NeetCode150/BinarySearch/BinarySearchSolution.cs
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,35 @@ | ||
namespace NeetCode.Solutions.NeetCode150.BinarySearch; | ||
|
||
[Level(Level.Easy)] | ||
public class BinarySearchSolution | ||
{ | ||
[TimeComplexity("O(log n)")] | ||
[SpaceComplexity("O(1)")] | ||
public int Search(int[] nums, int target) | ||
{ | ||
|
||
var left = 0; | ||
var right = nums.Length - 1; | ||
|
||
while (left <= right) | ||
{ | ||
var mid = left + (right - left) / 2; | ||
|
||
if (nums[mid] == target) | ||
{ | ||
return mid; | ||
} | ||
else if (nums[mid] < target) | ||
{ | ||
left = mid + 1; | ||
} | ||
else | ||
{ | ||
right = mid - 1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/NeetCode.Solutions/NeetCode150/BinarySearch/EatingBananasSolution.cs
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 @@ | ||
namespace NeetCode.Solutions.NeetCode150.BinarySearch; | ||
|
||
[Level(Level.Medium)] | ||
public class EatingBananasSolution | ||
{ | ||
[TimeComplexity("O(n * log m)")] | ||
[SpaceComplexity("O(1)")] | ||
public int MinEatingSpeed(int[] piles, int h) | ||
{ | ||
int left = 1; | ||
int right = piles.Max(); | ||
int result = right; | ||
|
||
while (left <= right) | ||
{ | ||
var mid = left + (right - left) / 2; | ||
|
||
long totalTime = 0; | ||
|
||
foreach (int p in piles) | ||
{ | ||
totalTime += (int)Math.Ceiling((double)p / mid); | ||
} | ||
|
||
if (totalTime <= h) | ||
{ | ||
result = mid; | ||
right = mid - 1; | ||
} | ||
else | ||
{ | ||
left = mid + 1; | ||
} | ||
|
||
} | ||
return result; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/NeetCode.Solutions/NeetCode150/BinarySearch/FindMinimumInRotatedSortedArraySolution.cs
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,28 @@ | ||
namespace NeetCode.Solutions.NeetCode150.BinarySearch; | ||
|
||
[Level(Level.Medium)] | ||
public class FindMinimumInRotatedSortedArraySolution | ||
{ | ||
[TimeComplexity("O(log n)")] | ||
[SpaceComplexity("O(1)]")] | ||
public int FindMin(int[] nums) { | ||
int left = 0; | ||
int right = nums.Length - 1; | ||
|
||
while (left < right) { | ||
int mid = left + (right - left) / 2; | ||
|
||
// If the mid element is greater than the rightmost element, | ||
// the minimum is in the right half. | ||
if (nums[mid] > nums[right]) { | ||
left = mid + 1; | ||
} else { | ||
// Otherwise, the minimum is in the left half, including mid. | ||
right = mid; | ||
} | ||
} | ||
|
||
// After the loop, left == right, pointing to the minimum element. | ||
return nums[left]; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/NeetCode.Solutions/NeetCode150/BinarySearch/Search2DMatrixSolution.cs
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,36 @@ | ||
namespace NeetCode.Solutions.NeetCode150.BinarySearch; | ||
|
||
[Level(Level.Medium)] | ||
public class Search2DMatrixSolution | ||
{ | ||
[TimeComplexity("O(logm + logn)")] | ||
[SpaceComplexity("O(1)")] | ||
public bool SearchMatrix(int[][] matrix, int target) { | ||
int rows = matrix.Length; | ||
int cols = matrix[0].Length; | ||
|
||
int left = 0; | ||
int right = rows * cols - 1; | ||
|
||
while(left <= right) | ||
{ | ||
int mid = left + (right - left) / 2; | ||
|
||
int row = mid / cols; | ||
int col = mid % cols; | ||
|
||
if(target == matrix[row][col]) | ||
{ | ||
return true; | ||
} | ||
else if (target > matrix[row][col]) | ||
{ | ||
left = mid + 1; | ||
} | ||
else{ | ||
right = mid - 1; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/NeetCode.Solutions/NeetCode150/Stack/CarFleetSolution.cs
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 @@ | ||
namespace NeetCode.Solutions.NeetCode150.Stack; | ||
|
||
[Level(Level.Medium)] | ||
public class CarFleetSolution | ||
{ | ||
public int CarFleet(int target, int[] position, int[] speed) | ||
{ | ||
var cars = new List<(int position, double time)>(); | ||
|
||
// Calculate time to reach target for each car | ||
for (int i = 0; i < position.Length; i++) | ||
{ | ||
double time = (double)(target - position[i]) / speed[i]; | ||
cars.Add((position[i], time)); | ||
} | ||
|
||
// Sort cars by their starting position in descending order | ||
cars.Sort((a, b) => b.position.CompareTo(a.position)); | ||
|
||
var stack = new Stack<double>(); | ||
|
||
// Process each car from the closest to the target to the furthest | ||
foreach (var car in cars) | ||
{ | ||
if (stack.Count == 0 || car.time > stack.Peek()) | ||
{ | ||
// Start a new fleet if this car has a greater time to target | ||
stack.Push(car.time); | ||
} | ||
// If car.time <= stack.Peek(), it joins the current fleet (do nothing) | ||
} | ||
|
||
// The number of fleets is the size of the stack | ||
return stack.Count; | ||
|
||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
tests/NeetCode.Tests/NeetCode150/BinarySearch/BinarySearchTests.cs
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,27 @@ | ||
namespace NeetCode.Tests.NeetCode150.BinarySearch | ||
{ | ||
public class BinarySearchTests | ||
{ | ||
[Test] | ||
public void Case1() | ||
{ | ||
var nums = new int[] { -1, 0, 2, 4, 6, 8 }; | ||
var target = 4; | ||
var solution = new BinarySearchSolution(); | ||
var result = solution.Search(nums, target); | ||
result.Should().Be(3); | ||
} | ||
|
||
[Test] | ||
public void Case2() | ||
{ | ||
var nums = new int[] { -1, 0, 2, 4, 6, 8 }; | ||
var target = 3; | ||
var solution = new BinarySearchSolution(); | ||
var result = solution.Search(nums, target); | ||
result.Should().Be(-1); | ||
} | ||
|
||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
tests/NeetCode.Tests/NeetCode150/BinarySearch/EatingBananasTests.cs
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,27 @@ | ||
namespace NeetCode.Tests.NeetCode150.BinarySearch | ||
{ | ||
public class EatingBananasTests | ||
{ | ||
[Test] | ||
public void Case1() | ||
{ | ||
var piles = new int[] { 1, 4, 3, 2 }; | ||
var h = 9; | ||
var solution = new EatingBananasSolution(); | ||
var result = solution.MinEatingSpeed(piles, h); | ||
result.Should().Be(2); | ||
} | ||
|
||
[Test] | ||
public void Case2() | ||
{ | ||
var piles = new int[] { 25, 10, 23, 4 }; | ||
var h = 4; | ||
var solution = new EatingBananasSolution(); | ||
var result = solution.MinEatingSpeed(piles, h); | ||
result.Should().Be(25); | ||
} | ||
|
||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
tests/NeetCode.Tests/NeetCode150/BinarySearch/FindMinimumInRotatedSortedArrayTests.cs
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 @@ | ||
namespace NeetCode.Tests.NeetCode150.BinarySearch; | ||
public class FindMinimumInRotatedSortedArrayTests | ||
{ | ||
[Test] | ||
public void Case1() | ||
{ | ||
var nums = new int[] { 3, 4, 5, 1, 2 }; | ||
var solution = new FindMinimumInRotatedSortedArraySolution(); | ||
var result = solution.FindMin(nums); | ||
result.Should().Be(1); | ||
} | ||
|
||
[Test] | ||
public void Case2() | ||
{ | ||
var nums = new int[] { 4, 5, 6, 7, 0, 1, 2 }; | ||
var solution = new FindMinimumInRotatedSortedArraySolution(); | ||
var result = solution.FindMin(nums); | ||
result.Should().Be(0); | ||
} | ||
|
||
[Test] | ||
public void Case3() | ||
{ | ||
var nums = new int[] { 11, 13, 15, 17 }; | ||
var solution = new FindMinimumInRotatedSortedArraySolution(); | ||
var result = solution.FindMin(nums); | ||
result.Should().Be(11); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
tests/NeetCode.Tests/NeetCode150/BinarySearch/Search2DMatrixTests.cs
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,35 @@ | ||
namespace NeetCode.Tests.NeetCode150.BinarySearch; | ||
public class Search2DMatrixTests | ||
{ | ||
[Test] | ||
public void Case1() | ||
{ | ||
var matrix = new int[][] | ||
{ | ||
[1, 2, 4, 8], | ||
[10, 11, 12, 13], | ||
[14, 20, 30, 40] | ||
}; | ||
|
||
var target = 15; | ||
var solution = new Search2DMatrixSolution(); | ||
var result = solution.SearchMatrix(matrix, target); | ||
result.Should().BeFalse(); | ||
} | ||
|
||
[Test] | ||
public void Case2() | ||
{ | ||
var matrix = new int[][] | ||
{ | ||
[1, 2, 4, 8], | ||
[10, 11, 12, 13], | ||
[14, 20, 30, 40] | ||
}; | ||
|
||
var target = 10; | ||
var solution = new Search2DMatrixSolution(); | ||
var result = solution.SearchMatrix(matrix, target); | ||
result.Should().BeTrue(); | ||
} | ||
} |
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 @@ | ||
|
||
namespace NeetCode.Tests.NeetCode150.Stack; | ||
|
||
public class CarFleetTests | ||
{ | ||
[Test] | ||
public void Case1() | ||
{ | ||
var target = 12; | ||
var position = new int[] { 1, 4 }; | ||
var speed = new int[] { 3, 2 }; | ||
|
||
var solution = new CarFleetSolution(); | ||
var result = solution.CarFleet(target, position, speed); | ||
result.Should().Be(1); | ||
} | ||
|
||
[Test] | ||
public void Case2() | ||
{ | ||
var target = 10; | ||
var position = new int[] { 4, 1, 0, 7 }; | ||
var speed = new int[] {2, 2, 1, 1 }; | ||
|
||
var solution = new CarFleetSolution(); | ||
var result = solution.CarFleet(target, position, speed); | ||
result.Should().Be(3); | ||
} | ||
} |
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