Skip to content

Commit

Permalink
soultion 7 22 74 84 150 739 875 1189
Browse files Browse the repository at this point in the history
  • Loading branch information
ianoflynnautomation committed Apr 20, 2024
1 parent e3b6a61 commit 2163797
Show file tree
Hide file tree
Showing 15 changed files with 509 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/LeetCode.Solutions/1189_MaximumNumberOfBalloons.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace LeetCode.Solutions;

/// <summary>
/// 1189. Maximum Number of Balloons
/// https://leetcode.com/problems/maximum-number-of-balloons/
/// Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
/// You can use each character in text at most once. Return the maximum number of instances that can be formed.
/// </summary>
public class _1189_MaximumNumberOfBalloons
{
public int MaxNumberOfBalloons(string text)
{
var dict = new Dictionary<char, int>
{
['b'] = 0,
['a'] = 0,
['l'] = 0,
['o'] = 0,
['n'] = 0
};

foreach (var c in text)
{
if (dict.ContainsKey(c))
{
dict[c]++;
}
}

dict['l'] /= 2;
dict['o'] /= 2;

return dict.Values.Min();
}
}
57 changes: 57 additions & 0 deletions src/LeetCode.Solutions/150_EvaluateReversePolishNotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace LeetCode.Solutions;

/// <summary>
/// 150. Evaluate Reverse Polish Notation
/// https://leetcode.com/problems/evaluate-reverse-polish-notation/
/// You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
/// Evaluate the expression. Return an integer that represents the value of the expression.
///
/// Note that:
/// The valid operators are '+', '-', '*', and '/'.
/// Each operand may be an integer or another expression.
/// The division between two integers always truncates toward zero.
/// There will not be any division by zero.
/// The input represents a valid arithmetic expression in a reverse polish notation.
/// The answer and all the intermediate calculations can be represented in a 32-bit integer.

[Level(Level.Medium)]
public class _150_EvaluateReversePolishNotation
{
public int EvalRPN(string[] tokens)
{
var operators = new HashSet<string> { "+", "-", "*", "/" };
var stack = new Stack<int>();

foreach (var token in tokens)
{
if (operators.Contains(token))
{
int number1 = stack.Pop();
int number2 = stack.Pop();

switch (token)
{
case "+":
stack.Push(number2 + number1);
break;
case "-":
stack.Push(number2 - number1);
break;
case "*":
stack.Push(number2 * number1);
break;
case "/":
stack.Push(number2 / number1);
break;
}
}
else
{
stack.Push(int.Parse(token));
}
}
return stack.Pop();

}

}
12 changes: 9 additions & 3 deletions src/LeetCode.Solutions/22_GenerateParentheses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ public IList<string> GenerateParenthesis(int n)
return answer;
}

private void backtracking(List<string> answer, string currentString,
int leftCount, int rightCount, int n)
private void backtracking(
List<string> answer,
string currentString,
int leftCount,
int rightCount,
int n)
{
if (currentString.Length == 2 * n)
{
answer.Add(currentString); return;
answer.Add(currentString);

return;
}

if (leftCount < n)
Expand Down
24 changes: 24 additions & 0 deletions src/LeetCode.Solutions/739_DailyTemperatures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace LeetCode.Solutions;

public class _739_DailyTemperatures
{
public int[] DailyTemperatures(int[] temperatures)
{
int[] results = new int[temperatures.Length];

Stack<int> stack = new Stack<int>();

for (int i = 0; i < temperatures.Length; i++)
{
while (stack.Count > 0 && temperatures[stack.Peek()] < temperatures[i])
{
int index = stack.Pop();
results[index] = i - index;
}

stack.Push(i);
}

return results;
}
}
44 changes: 44 additions & 0 deletions src/LeetCode.Solutions/74_SearchA2DMatrix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace LeetCode.Solutions;

/// <summary>
/// 74. Search a 2D Matrix
/// https://leetcode.com/problems/search-a-2d-matrix/
/// Write an efficient algorithm that searches for a value in an m x n matrix.
/// This matrix has the following properties:
/// Integers in each row are sorted from left to right.
///
/// </summary>

[Level(Level.Medium)]
public class _74_SearchA2DMatrix
{

public bool SearchMatrix(int[][] matrix, int target)
{
var row = matrix.Length;
var col = matrix[0].Length;
var left = 0;
var right = row * col - 1;

while(left <= right)
{
var mid = left + (right - left) / 2;
var midValue = matrix[mid / col][mid % col];

if(midValue == target)
{
return true;
}
else if(midValue < target)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}

return false;
}
}
1 change: 1 addition & 0 deletions src/LeetCode.Solutions/7_ReverseInteger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class _7_ReverseInteger
public int Reverse(int x)
{
int result = 0;

while (x != 0)
{
int pop = x % 10;
Expand Down
38 changes: 38 additions & 0 deletions src/LeetCode.Solutions/84_LargestRectangleInHistogram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace LeetCode.Solutions;

/// <summary>
/// 84. Largest Rectangle in Histogram
/// https://leetcode.com/problems/largest-rectangle-in-histogram/
/// Given an array of integers heights representing the histogram's bar height where the width of each bar is 1,
/// return the area of the largest rectangle in the histogram.
/// </summary>

[Level(Level.Hard)]
public class _84_LargestRectangleInHistogram
{
public int LargestRectangleArea(int[] heights)
{

var stack = new Stack<int>();

int max = 0;

for (int i = 0; i <= heights.Length; i++)
{

var height = i < heights.Length ? heights[i] : 0;

while (stack.Count != 0 && heights[stack.Peek()] > height)
{
var currentHeight = heights[stack.Pop()];
var previousIndex = stack.Count == 0 ? -1 : stack.Peek();

max = Math.Max(max, currentHeight * (i - 1 - previousIndex));
}

stack.Push(i);
}

return max;
}
}
45 changes: 45 additions & 0 deletions src/LeetCode.Solutions/875_KokoEatingBananas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace LeetCode.Solutions;
/// <summary>
/// 975. Koko Eating Bananas
/// https://leetcode.com/problems/koko-eating-bananas/
///
/// Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas.
/// The guards have gone and will come back in h hours.
/// Koko can decide her bananas-per-hour eating speed of k.
/// Each hour, she chooses some pile of bananas and eats k bananas from that pile.
/// If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.
/// Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
/// Return the minimum integer k such that she can eat all the bananas within h hours.
/// </summary>

[Level(Level.Medium)]
public class _875_KokoEatingBananas
{
public int MinEatingSpeed(int[] piles, int h)
{
int left = 1;
int right = piles.Max();

while (left < right)
{
int mid = left + (right - left) / 2;
int hours = 0;

foreach (int pile in piles)
{
hours += (pile + mid - 1) / mid;
}

if (hours > h)
{
left = mid + 1;
}
else
{
right = mid;
}
}

return left;
}
}
Binary file modified src/LeetCode.Tests/.DS_Store
Binary file not shown.
31 changes: 31 additions & 0 deletions src/LeetCode.Tests/1189_MaximumNumberOfBalloons_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace LeetCode.Tests;

public class _1189_MaximumNumberOfBalloons_Tests
{
[Test]
public void Case1()
{
var solution = new _1189_MaximumNumberOfBalloons();
var result = solution.MaxNumberOfBalloons("nlaebolko");

Assert.AreEqual(1, result);
}

[Test]
public void Case2()
{
var solution = new _1189_MaximumNumberOfBalloons();
var result = solution.MaxNumberOfBalloons("loonbalxballpoon");

Assert.AreEqual(2, result);
}

[Test]
public void Case3()
{
var solution = new _1189_MaximumNumberOfBalloons();
var result = solution.MaxNumberOfBalloons("leetcode");

Assert.AreEqual(0, result);
}
}
37 changes: 37 additions & 0 deletions src/LeetCode.Tests/150_EvaluateReversePolishNotation_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace LeetCode.Tests;

public class _150_EvaluateReversePolishNotation_Tests
{
[Test]
public void Case1()
{
var tokens = new[] { "2", "1", "+", "3", "*" };

var solution = new _150_EvaluateReversePolishNotation();
var result = solution.EvalRPN(tokens);

result.Should().Be(9);
}

[Test]
public void Case2()
{
var tokens = new[] { "4", "13", "5", "/", "+" };

var solution = new _150_EvaluateReversePolishNotation();
var result = solution.EvalRPN(tokens);

result.Should().Be(6);
}

[Test]
public void Case3()
{
var tokens = new[] { "10","6","9","3","+","-11","*","/","*","17","+","5","+"};

var solution = new _150_EvaluateReversePolishNotation();
var result = solution.EvalRPN(tokens);

result.Should().Be(22);
}
}
38 changes: 38 additions & 0 deletions src/LeetCode.Tests/739_DailyTemperatures_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

namespace LeetCode.Tests;

public class _739_DailyTemperatures_Tests
{
[Test]
public void Case1()
{
var temperatures = new[] { 73, 74, 75, 71, 69, 72, 76, 73 };

var solution = new _739_DailyTemperatures();
var result = solution.DailyTemperatures(temperatures);

result.Should().Equal(1, 1, 4, 2, 1, 1, 0, 0);
}

[Test]
public void Case2()
{
var temperatures = new[] { 30, 40, 50, 60 };

var solution = new _739_DailyTemperatures();
var result = solution.DailyTemperatures(temperatures);

result.Should().Equal(1, 1, 1, 0);
}

[Test]
public void Case3()
{
var temperatures = new[] { 30, 60, 90 };

var solution = new _739_DailyTemperatures();
var result = solution.DailyTemperatures(temperatures);

result.Should().Equal(1, 1, 0);
}
}
Loading

0 comments on commit 2163797

Please sign in to comment.