Skip to content

Added tasks 167-190 #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0101_0200.S0167_two_sum_ii_input_array_is_sorted {

using Xunit;

public class SolutionTest {
[Fact]
public void TwoSum() {
Assert.Equal(new int[] {1, 2}, new Solution().TwoSum(new int[] {2, 7, 11, 15}, 9));
}

[Fact]
public void TwoSum2() {
Assert.Equal(new int[] {1, 3}, new Solution().TwoSum(new int[] {2, 3, 4}, 6));
}

[Fact]
public void TwoSum3() {
Assert.Equal(new int[] {1, 2}, new Solution().TwoSum(new int[] {-1, 0}, -1));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0101_0200.S0172_factorial_trailing_zeroes {

using Xunit;

public class SolutionTest {
[Fact]
public void TrailingZeroes() {
Assert.Equal(0, new Solution().TrailingZeroes(3));
}

[Fact]
public void TrailingZeroes2() {
Assert.Equal(1, new Solution().TrailingZeroes(5));
}

[Fact]
public void TrailingZeroes3() {
Assert.Equal(0, new Solution().TrailingZeroes(0));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace LeetCodeNet.G0101_0200.S0173_binary_search_tree_iterator {

using Xunit;
using System.Collections.Generic;
using LeetCodeNet.Com_github_leetcode;

public class BSTIteratorTest {
[Fact]
public void IteratorBST() {
var left = new TreeNode(3);
var right = new TreeNode(15, new TreeNode(9), new TreeNode(20));
var root = new TreeNode(7, left, right);
var iterator = new BSTIterator(root);
Assert.Equal(3, iterator.Next());
Assert.Equal(7, iterator.Next());
Assert.True(iterator.HasNext());
Assert.Equal(9, iterator.Next());
Assert.True(iterator.HasNext());
Assert.Equal(15, iterator.Next());
Assert.True(iterator.HasNext());
Assert.Equal(20, iterator.Next());
Assert.False(iterator.HasNext());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace LeetCodeNet.G0101_0200.S0188_best_time_to_buy_and_sell_stock_iv {

using Xunit;

public class SolutionTest {
[Fact]
public void MaxProfit() {
Assert.Equal(2, new Solution().MaxProfit(2, new int[] {2, 4, 1}));
}

[Fact]
public void MaxProfit2() {
Assert.Equal(7, new Solution().MaxProfit(2, new int[] {3, 2, 6, 5, 0, 3}));
}
}
}
16 changes: 16 additions & 0 deletions LeetCodeNet.Tests/G0101_0200/S0190_reverse_bits/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace LeetCodeNet.G0101_0200.S0190_reverse_bits {

using Xunit;

public class SolutionTest {
[Fact]
public void reverseBits() {
Assert.Equal(0b00111001011110000010100101000000u, new Solution().reverseBits(0b00000010100101000001111010011100u));
}

[Fact]
public void reverseBits2() {
Assert.Equal(0b10111111111111111111111111111111u, new Solution().reverseBits(0b11111111111111111111111111111101u));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace LeetCodeNet.G0101_0200.S0167_two_sum_ii_input_array_is_sorted {

// #Medium #Array #Binary_Search #Two_Pointers #Algorithm_I_Day_3_Two_Pointers
// #Binary_Search_I_Day_7 #Top_Interview_150_Two_Pointers
// #2025_07_13_Time_0_ms_(100.00%)_Space_50.79_MB_(76.45%)

public class Solution {
public int[] TwoSum(int[] numbers, int target) {
int[] res = new int[2];
int i = 0;
int j = numbers.Length - 1;
while (i < j) {
int sum = numbers[i] + numbers[j];
if (sum == target) {
res[0] = i + 1;
res[1] = j + 1;
return res;
} else if (sum < target) {
i++;
} else {
j--;
}
}
return res;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
167\. Two Sum II - Input Array Is Sorted

Easy

Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 <= index<sub>1</sub> < index<sub>2</sub> <= numbers.length</code>.

Return _the indices of the two numbers,_ <code>index<sub>1</sub></code> _and_ <code>index<sub>2</sub></code>_, **added by one** as an integer array_ <code>[index<sub>1</sub>, index<sub>2</sub>]</code> _of length 2._

The tests are generated such that there is **exactly one solution**. You **may not** use the same element twice.

**Example 1:**

**Input:** numbers = [2,7,11,15], target = 9

**Output:** [1,2]

**Explanation:** The sum of 2 and 7 is 9. Therefore, index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].

**Example 2:**

**Input:** numbers = [2,3,4], target = 6

**Output:** [1,3]

**Explanation:** The sum of 2 and 4 is 6. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 3. We return [1, 3].

**Example 3:**

**Input:** numbers = [\-1,0], target = -1

**Output:** [1,2]

**Explanation:** The sum of -1 and 0 is -1. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].

**Constraints:**

* <code>2 <= numbers.length <= 3 * 10<sup>4</sup></code>
* `-1000 <= numbers[i] <= 1000`
* `numbers` is sorted in **non-decreasing order**.
* `-1000 <= target <= 1000`
* The tests are generated such that there is **exactly one solution**.
17 changes: 17 additions & 0 deletions LeetCodeNet/G0101_0200/S0172_factorial_trailing_zeroes/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace LeetCodeNet.G0101_0200.S0172_factorial_trailing_zeroes {

// #Medium #Top_Interview_Questions #Math #Udemy_Integers #Top_Interview_150_Math
// #2025_07_13_Time_0_ms_(100.00%)_Space_29.27_MB_(31.68%)

public class Solution {
public int TrailingZeroes(int n) {
int baseN = 5;
int count = 0;
while (n >= baseN) {
count += n / baseN;
baseN = baseN * 5;
}
return count;
}
}
}
35 changes: 35 additions & 0 deletions LeetCodeNet/G0101_0200/S0172_factorial_trailing_zeroes/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
172\. Factorial Trailing Zeroes

Medium

Given an integer `n`, return _the number of trailing zeroes in_ `n!`.

Note that `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`.

**Example 1:**

**Input:** n = 3

**Output:** 0

**Explanation:** 3! = 6, no trailing zero.

**Example 2:**

**Input:** n = 5

**Output:** 1

**Explanation:** 5! = 120, one trailing zero.

**Example 3:**

**Input:** n = 0

**Output:** 0

**Constraints:**

* <code>0 <= n <= 10<sup>4</sup></code>

**Follow up:** Could you write a solution that works in logarithmic time complexity?
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace LeetCodeNet.G0101_0200.S0173_binary_search_tree_iterator {

// #Medium #Tree #Binary_Tree #Stack #Design #Binary_Search_Tree #Iterator
// #Data_Structure_II_Day_17_Tree #Programming_Skills_II_Day_16 #Level_2_Day_9_Binary_Search_Tree
// #Top_Interview_150_Binary_Tree_General #2025_07_13_Time_1_ms_(100.00%)_Space_62.77_MB_(66.48%)

using LeetCodeNet.Com_github_leetcode;

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class BSTIterator {
private TreeNode node;

public BSTIterator(TreeNode root) {
node = root;
}

public int Next() {
int res = -1;
while (node != null) {
if (node.left != null) {
TreeNode rightMost = node.left;
while (rightMost.right != null) {
rightMost = rightMost.right;
}
rightMost.right = node;
TreeNode temp = node.left;
node.left = null;
node = temp;
} else {
res = node.val.Value;
node = node.right;
return res;
}
}
return res;
}

public bool HasNext() {
return node != null;
}
}

/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator obj = new BSTIterator(root);
* int param_1 = obj.Next();
* bool param_2 = obj.HasNext();
*/
}
44 changes: 44 additions & 0 deletions LeetCodeNet/G0101_0200/S0173_binary_search_tree_iterator/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
173\. Binary Search Tree Iterator

Medium

Implement the `BSTIterator` class that represents an iterator over the **[in-order traversal](https://en.wikipedia.org/wiki/Tree_traversal#In-order_(LNR))** of a binary search tree (BST):

* `BSTIterator(TreeNode root)` Initializes an object of the `BSTIterator` class. The `root` of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
* `boolean hasNext()` Returns `true` if there exists a number in the traversal to the right of the pointer, otherwise returns `false`.
* `int next()` Moves the pointer to the right, then returns the number at the pointer.

Notice that by initializing the pointer to a non-existent smallest number, the first call to `next()` will return the smallest element in the BST.

You may assume that `next()` calls will always be valid. That is, there will be at least a next number in the in-order traversal when `next()` is called.

**Example 1:**

![](https://assets.leetcode.com/uploads/2018/12/25/bst-tree.png)

**Input** ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]

**Output:** [null, 3, 7, true, 9, true, 15, true, 20, false]

**Explanation:**

BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next(); // return 3
bSTIterator.next(); // return 7
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 9
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 15
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 20
bSTIterator.hasNext(); // return False

**Constraints:**

* The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.
* <code>0 <= Node.val <= 10<sup>6</sup></code>
* At most <code>10<sup>5</sup></code> calls will be made to `hasNext`, and `next`.

**Follow up:**

* Could you implement `next()` and `hasNext()` to run in average `O(1)` time and use `O(h)` memory, where `h` is the height of the tree?
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace LeetCodeNet.G0101_0200.S0188_best_time_to_buy_and_sell_stock_iv {

// #Hard #Array #Dynamic_Programming #Top_Interview_150_Multidimensional_DP
// #2025_07_13_Time_1_ms_(100.00%)_Space_42.53_MB_(87.70%)

public class Solution {
public int MaxProfit(int k, int[] prices) {
int n = prices.Length;
int[] dp = new int[k + 1];
int[] maxdp = new int[k + 1];
for (int i = 0; i <= k; i++) {
maxdp[i] = int.MinValue;
}
for (int i = 1; i <= n; i++) {
maxdp[0] = System.Math.Max(maxdp[0], dp[0] - prices[i - 1]);
for (int j = k; j >= 1; j--) {
maxdp[j] = System.Math.Max(maxdp[j], dp[j] - prices[i - 1]);
dp[j] = System.Math.Max(maxdp[j - 1] + prices[i - 1], dp[j]);
}
}
return dp[k];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
188\. Best Time to Buy and Sell Stock IV

Hard

You are given an integer array `prices` where `prices[i]` is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer `k`.

Find the maximum profit you can achieve. You may complete at most `k` transactions.

**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

**Example 1:**

**Input:** k = 2, prices = [2,4,1]

**Output:** 2

**Explanation:** Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

**Example 2:**

**Input:** k = 2, prices = [3,2,6,5,0,3]

**Output:** 7

**Explanation:** Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.

**Constraints:**

* `0 <= k <= 100`
* `0 <= prices.length <= 1000`
* `0 <= prices[i] <= 1000`
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy