-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution80.cs
102 lines (88 loc) · 2.87 KB
/
Solution80.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
namespace LeetCode.Solutions;
public class Solution80
{
/// <summary>
/// 80. Remove Duplicates from Sorted Array II
/// <a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii">See the problem</a>
/// </summary>
public int RemoveDuplicates(int[] nums)
{
if (nums.Length <= 2)
{
return nums.Length;
}
var lastItemIndex = nums.Length - 1;
int? prevNum = null;
for (var i = 1; i <= lastItemIndex; i++)
{
if (nums[i] == nums[i - 1] && !prevNum.HasValue)
{
prevNum = nums[i];
continue;
}
if (nums[i] == nums[i - 1] && nums[i] == prevNum)
{
var shiftCount = lastItemIndex - i;
ShiftRight(nums, i, lastItemIndex, shiftCount);
i--;
lastItemIndex--;
}
else
{
prevNum = null;
}
}
return lastItemIndex + 1;
}
/// <summary>
/// Optimized Easiest approach suggested by ChatGPT
/// </summary>
public int RemoveDuplicates2(int[] nums)
{
if (nums.Length <= 2)
{
return nums.Length;
}
var newLength = 2; // Start from 2 as the first two elements can always be part of the result
for (var i = 2; i < nums.Length; i++)
{
// Check if current element is a duplicate of the element at newLength - 2
if (nums[i] != nums[newLength - 2])
{
nums[newLength] = nums[i];
newLength++;
}
}
return newLength;
}
/// <summary>
/// Shift array elements to right by k steps
/// </summary>
/// <param name="array">Array of integers</param>
/// <param name="startIndex">Start index of array portion</param>
/// <param name="lastIndex">Last index of array portion</param>
/// <param name="shiftCount">Number of steps to shift elements to right</param>
private void ShiftRight(int[] array, int startIndex, int lastIndex, int shiftCount)
{
var segmentSize = lastIndex - startIndex + 1;
shiftCount %= segmentSize; // Normalize shift count
// Reverse the entire segment
Reverse(array, startIndex, lastIndex);
// Reverse the first part
Reverse(array, startIndex, startIndex + shiftCount - 1);
// Reverse the second part
Reverse(array, startIndex + shiftCount, lastIndex);
}
/// <summary>
/// Reverse a segment of the array
/// </summary>
private void Reverse(int[] array, int start, int end)
{
while (start < end)
{
(array[start], array[end]) = (array[end], array[start]);
start++;
end--;
}
}
}