Skip to content

Commit 3a02344

Browse files
authored
Added tasks 26, 33.
1 parent 7dc40f7 commit 3a02344

File tree

6 files changed

+183
-0
lines changed

6 files changed

+183
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0001_0100.s0026_remove_duplicates_from_sorted_array
2+
3+
// #Easy #Top_Interview_Questions #Array #Two_Pointers
4+
// #2022_03_29_Time_361_ms_(77.19%)_Space_47.7_MB_(25.17%)
5+
6+
class Solution {
7+
fun removeDuplicates(nums: IntArray): Int {
8+
val n = nums.size
9+
var i = 0
10+
var j = 1
11+
if (n <= 1) {
12+
return n
13+
}
14+
while (j <= n - 1) {
15+
if (nums[i] != nums[j]) {
16+
nums[i + 1] = nums[j]
17+
i++
18+
}
19+
j++
20+
}
21+
return i + 1
22+
}
23+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
26\. Remove Duplicates from Sorted Array
2+
3+
Easy
4+
5+
Given an integer array `nums` sorted in **non-decreasing order**, remove the duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears only **once**. The **relative order** of the elements should be kept the **same**.
6+
7+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
8+
9+
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
10+
11+
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
12+
13+
**Custom Judge:**
14+
15+
The judge will test your solution with the following code:
16+
17+
int[] nums = [...]; // Input array
18+
int[] expectedNums = [...]; // The expected answer with correct length
19+
20+
int k = removeDuplicates(nums); // Calls your implementation
21+
22+
assert k == expectedNums.length;
23+
for (int i = 0; i < k; i++) {
24+
assert nums[i] == expectedNums[i];
25+
}
26+
27+
If all assertions pass, then your solution will be **accepted**.
28+
29+
**Example 1:**
30+
31+
**Input:** nums = [1,1,2]
32+
33+
**Output:** 2, nums = [1,2,\_]
34+
35+
**Explanation:** Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
36+
37+
**Example 2:**
38+
39+
**Input:** nums = [0,0,1,1,1,2,2,3,3,4]
40+
41+
**Output:** 5, nums = [0,1,2,3,4,\_,\_,\_,\_,\_]
42+
43+
**Explanation:** Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
44+
45+
**Constraints:**
46+
47+
* <code>0 <= nums.length <= 3 * 10<sup>4</sup></code>
48+
* `-100 <= nums[i] <= 100`
49+
* `nums` is sorted in **non-decreasing** order.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0001_0100.s0033_search_in_rotated_sorted_array
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search
4+
// #Algorithm_II_Day_1_Binary_Search #2022_03_29_Time_184_ms_(86.08%)_Space_37.2_MB_(36.66%)
5+
6+
class Solution {
7+
fun search(nums: IntArray, target: Int): Int {
8+
var mid: Int
9+
var lo = 0
10+
var hi = nums.size - 1
11+
while (lo <= hi) {
12+
mid = (hi - lo shr 1) + lo
13+
if (target == nums[mid]) {
14+
return mid
15+
}
16+
// if this is true, then the possible rotation can only be in the second half
17+
if (nums[lo] <= nums[mid]) {
18+
// the target is in the first half only if it's
19+
if (nums[lo] <= target && target <= nums[mid]) {
20+
// included
21+
hi = mid - 1
22+
} else {
23+
// between nums[lo] and nums[mid]
24+
lo = mid + 1
25+
}
26+
// otherwise, the possible rotation can only be in the first half
27+
} else if (nums[mid] <= target && target <= nums[hi]) {
28+
// the target is in the second half only if it's included
29+
lo = mid + 1
30+
} else {
31+
// between nums[hi] and nums[mid]
32+
hi = mid - 1
33+
}
34+
}
35+
return -1
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
33\. Search in Rotated Sorted Array
2+
3+
Medium
4+
5+
There is an integer array `nums` sorted in ascending order (with **distinct** values).
6+
7+
Prior to being passed to your function, `nums` is **possibly rotated** at an unknown pivot index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,5,6,7]` might be rotated at pivot index `3` and become `[4,5,6,7,0,1,2]`.
8+
9+
Given the array `nums` **after** the possible rotation and an integer `target`, return _the index of_ `target` _if it is in_ `nums`_, or_ `-1` _if it is not in_ `nums`.
10+
11+
You must write an algorithm with `O(log n)` runtime complexity.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [4,5,6,7,0,1,2], target = 0
16+
17+
**Output:** 4
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [4,5,6,7,0,1,2], target = 3
22+
23+
**Output:** -1
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [1], target = 0
28+
29+
**Output:** -1
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 5000`
34+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
35+
* All values of `nums` are **unique**.
36+
* `nums` is an ascending array that is possibly rotated.
37+
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g0001_0100.s0026_remove_duplicates_from_sorted_array
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import java.util.Arrays
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun removeDuplicates() {
11+
val array = intArrayOf(1, 1, 2)
12+
val end = Solution().removeDuplicates(array)
13+
assertThat(Arrays.toString(Arrays.copyOfRange(array, 0, end)), equalTo("[1, 2]"))
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0033_search_in_rotated_sorted_array
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun search() {
10+
assertThat(Solution().search(intArrayOf(4, 5, 6, 7, 0, 1, 2), 0), equalTo(4))
11+
}
12+
13+
@Test
14+
fun search2() {
15+
assertThat(Solution().search(intArrayOf(4, 5, 6, 7, 0, 1, 2), 3), equalTo(-1))
16+
}
17+
18+
@Test
19+
fun search3() {
20+
assertThat(Solution().search(intArrayOf(1), 0), equalTo(-1))
21+
}
22+
}

0 commit comments

Comments
 (0)