-
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.
soultion 7 22 74 84 150 739 875 1189
- Loading branch information
1 parent
e3b6a61
commit 2163797
Showing
15 changed files
with
509 additions
and
3 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 |
---|---|---|
@@ -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
57
src/LeetCode.Solutions/150_EvaluateReversePolishNotation.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,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(); | ||
|
||
} | ||
|
||
} |
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
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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
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 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; | ||
} | ||
} |
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,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 not shown.
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 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
37
src/LeetCode.Tests/150_EvaluateReversePolishNotation_Tests.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,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); | ||
} | ||
} |
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 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); | ||
} | ||
} |
Oops, something went wrong.