Skip to content

Commit

Permalink
add binary search solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
ianoflynnautomation committed Nov 5, 2024
1 parent 4b0c1ae commit 72e4ad5
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,6 @@ Footer navigation
Terms
Privacy
Security

*.DS_Store

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;
}

}
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;
}
}
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];
}
}
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 + log⁡n)")]
[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 src/NeetCode.Solutions/NeetCode150/Stack/CarFleetSolution.cs
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 tests/NeetCode.Tests/NeetCode150/BinarySearch/BinarySearchTests.cs
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);
}

}

}
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);
}

}

}
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);
}

}
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();
}
}
29 changes: 29 additions & 0 deletions tests/NeetCode.Tests/NeetCode150/Stack/CarFleetTests.cs
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);
}
}
1 change: 1 addition & 0 deletions tests/NeetCode.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
global using NeetCode.Solutions.NeetCode150.Stack;
global using NeetCode.Solutions.NeetCode150.TwoPointers;
global using NeetCode.Solutions.NeetCode150.ArraysAndHashing;
global using NeetCode.Solutions.NeetCode150.BinarySearch;
global using FluentAssertions;
global using System.Collections.Generic;

0 comments on commit 72e4ad5

Please sign in to comment.