Skip to content

Commit 51c2c3d

Browse files
committed
Added tasks 167-190
1 parent e83b5b7 commit 51c2c3d

File tree

6 files changed

+360
-0
lines changed

6 files changed

+360
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 167\. Two Sum II - Input Array Is Sorted
5+
6+
Easy
7+
8+
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>.
9+
10+
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._
11+
12+
The tests are generated such that there is **exactly one solution**. You **may not** use the same element twice.
13+
14+
**Example 1:**
15+
16+
**Input:** numbers = [2,7,11,15], target = 9
17+
18+
**Output:** [1,2]
19+
20+
**Explanation:** The sum of 2 and 7 is 9. Therefore, index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].
21+
22+
**Example 2:**
23+
24+
**Input:** numbers = [2,3,4], target = 6
25+
26+
**Output:** [1,3]
27+
28+
**Explanation:** The sum of 2 and 4 is 6. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 3. We return [1, 3].
29+
30+
**Example 3:**
31+
32+
**Input:** numbers = [\-1,0], target = -1
33+
34+
**Output:** [1,2]
35+
36+
**Explanation:** The sum of -1 and 0 is -1. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].
37+
38+
**Constraints:**
39+
40+
* <code>2 <= numbers.length <= 3 * 10<sup>4</sup></code>
41+
* `-1000 <= numbers[i] <= 1000`
42+
* `numbers` is sorted in **non-decreasing order**.
43+
* `-1000 <= target <= 1000`
44+
* The tests are generated such that there is **exactly one solution**.
45+
46+
## Solution
47+
48+
```csharp
49+
public class Solution {
50+
public int[] TwoSum(int[] numbers, int target) {
51+
int[] res = new int[2];
52+
int i = 0;
53+
int j = numbers.Length - 1;
54+
while (i < j) {
55+
int sum = numbers[i] + numbers[j];
56+
if (sum == target) {
57+
res[0] = i + 1;
58+
res[1] = j + 1;
59+
return res;
60+
} else if (sum < target) {
61+
i++;
62+
} else {
63+
j--;
64+
}
65+
}
66+
return res;
67+
}
68+
}
69+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 172\. Factorial Trailing Zeroes
5+
6+
Medium
7+
8+
Given an integer `n`, return _the number of trailing zeroes in_ `n!`.
9+
10+
Note that `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 3
15+
16+
**Output:** 0
17+
18+
**Explanation:** 3! = 6, no trailing zero.
19+
20+
**Example 2:**
21+
22+
**Input:** n = 5
23+
24+
**Output:** 1
25+
26+
**Explanation:** 5! = 120, one trailing zero.
27+
28+
**Example 3:**
29+
30+
**Input:** n = 0
31+
32+
**Output:** 0
33+
34+
**Constraints:**
35+
36+
* <code>0 <= n <= 10<sup>4</sup></code>
37+
38+
**Follow up:** Could you write a solution that works in logarithmic time complexity?
39+
40+
## Solution
41+
42+
```csharp
43+
public class Solution {
44+
public int TrailingZeroes(int n) {
45+
int baseN = 5;
46+
int count = 0;
47+
while (n >= baseN) {
48+
count += n / baseN;
49+
baseN = baseN * 5;
50+
}
51+
return count;
52+
}
53+
}
54+
```
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 173\. Binary Search Tree Iterator
5+
6+
Medium
7+
8+
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):
9+
10+
* `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.
11+
* `boolean hasNext()` Returns `true` if there exists a number in the traversal to the right of the pointer, otherwise returns `false`.
12+
* `int next()` Moves the pointer to the right, then returns the number at the pointer.
13+
14+
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.
15+
16+
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.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2018/12/25/bst-tree.png)
21+
22+
**Input** ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
23+
24+
**Output:** [null, 3, 7, true, 9, true, 15, true, 20, false]
25+
26+
**Explanation:**
27+
28+
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
29+
bSTIterator.next(); // return 3
30+
bSTIterator.next(); // return 7
31+
bSTIterator.hasNext(); // return True
32+
bSTIterator.next(); // return 9
33+
bSTIterator.hasNext(); // return True
34+
bSTIterator.next(); // return 15
35+
bSTIterator.hasNext(); // return True
36+
bSTIterator.next(); // return 20
37+
bSTIterator.hasNext(); // return False
38+
39+
**Constraints:**
40+
41+
* The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.
42+
* <code>0 <= Node.val <= 10<sup>6</sup></code>
43+
* At most <code>10<sup>5</sup></code> calls will be made to `hasNext`, and `next`.
44+
45+
**Follow up:**
46+
47+
* 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?
48+
49+
## Solution
50+
51+
```csharp
52+
using LeetCodeNet.Com_github_leetcode;
53+
54+
/**
55+
* Definition for a binary tree node.
56+
* public class TreeNode {
57+
* public int val;
58+
* public TreeNode left;
59+
* public TreeNode right;
60+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
61+
* this.val = val;
62+
* this.left = left;
63+
* this.right = right;
64+
* }
65+
* }
66+
*/
67+
public class BSTIterator {
68+
private TreeNode node;
69+
70+
public BSTIterator(TreeNode root) {
71+
node = root;
72+
}
73+
74+
public int Next() {
75+
int res = -1;
76+
while (node != null) {
77+
if (node.left != null) {
78+
TreeNode rightMost = node.left;
79+
while (rightMost.right != null) {
80+
rightMost = rightMost.right;
81+
}
82+
rightMost.right = node;
83+
TreeNode temp = node.left;
84+
node.left = null;
85+
node = temp;
86+
} else {
87+
res = node.val.Value;
88+
node = node.right;
89+
return res;
90+
}
91+
}
92+
return res;
93+
}
94+
95+
public bool HasNext() {
96+
return node != null;
97+
}
98+
}
99+
100+
/**
101+
* Your BSTIterator object will be instantiated and called as such:
102+
* BSTIterator obj = new BSTIterator(root);
103+
* int param_1 = obj.Next();
104+
* bool param_2 = obj.HasNext();
105+
}
106+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 188\. Best Time to Buy and Sell Stock IV
5+
6+
Hard
7+
8+
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`.
9+
10+
Find the maximum profit you can achieve. You may complete at most `k` transactions.
11+
12+
**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
13+
14+
**Example 1:**
15+
16+
**Input:** k = 2, prices = [2,4,1]
17+
18+
**Output:** 2
19+
20+
**Explanation:** Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
21+
22+
**Example 2:**
23+
24+
**Input:** k = 2, prices = [3,2,6,5,0,3]
25+
26+
**Output:** 7
27+
28+
**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.
29+
30+
**Constraints:**
31+
32+
* `0 <= k <= 100`
33+
* `0 <= prices.length <= 1000`
34+
* `0 <= prices[i] <= 1000`
35+
36+
## Solution
37+
38+
```csharp
39+
public class Solution {
40+
public int MaxProfit(int k, int[] prices) {
41+
int n = prices.Length;
42+
int[] dp = new int[k + 1];
43+
int[] maxdp = new int[k + 1];
44+
for (int i = 0; i <= k; i++) {
45+
maxdp[i] = int.MinValue;
46+
}
47+
for (int i = 1; i <= n; i++) {
48+
maxdp[0] = System.Math.Max(maxdp[0], dp[0] - prices[i - 1]);
49+
for (int j = k; j >= 1; j--) {
50+
maxdp[j] = System.Math.Max(maxdp[j], dp[j] - prices[i - 1]);
51+
dp[j] = System.Math.Max(maxdp[j - 1] + prices[i - 1], dp[j]);
52+
}
53+
}
54+
return dp[k];
55+
}
56+
}
57+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 190\. Reverse Bits
5+
6+
Easy
7+
8+
Reverse bits of a given 32 bits unsigned integer.
9+
10+
**Note:**
11+
12+
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
13+
* In Java, the compiler represents the signed integers using [2's complement notation](https://en.wikipedia.org/wiki/Two%27s_complement). Therefore, in **Example 2** above, the input represents the signed integer `-3` and the output represents the signed integer `-1073741825`.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 00000010100101000001111010011100
18+
19+
**Output:** 964176192 (00111001011110000010100101000000)
20+
21+
**Explanation:** The input binary string **00000010100101000001111010011100** represents the unsigned integer 43261596, so return 964176192 which its binary representation is **00111001011110000010100101000000**.
22+
23+
**Example 2:**
24+
25+
**Input:** n = 11111111111111111111111111111101
26+
27+
**Output:** 3221225471 (10111111111111111111111111111111)
28+
29+
**Explanation:** The input binary string **11111111111111111111111111111101** represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is **10111111111111111111111111111111**.
30+
31+
**Constraints:**
32+
33+
* The input must be a **binary string** of length `32`
34+
35+
**Follow up:** If this function is called many times, how would you optimize it?
36+
37+
## Solution
38+
39+
```csharp
40+
public class Solution {
41+
// you need treat n as an unsigned value
42+
public uint reverseBits(uint n) {
43+
uint ret = 0;
44+
// because there are 32 bits in total
45+
for (int i = 0; i < 32; i++) {
46+
ret = ret << 1;
47+
// If the bit is 1 we OR it with 1, ie add 1
48+
if ((n & 1) > 0) {
49+
ret = ret | 1;
50+
}
51+
n = n >> 1;
52+
}
53+
return ret;
54+
}
55+
}
56+
```

0 commit comments

Comments
 (0)