From e41c1d3a6f5de662ac5aa7327bdf9c99f2507ffd Mon Sep 17 00:00:00 2001 From: paramag Date: Fri, 7 Sep 2018 17:43:30 -0700 Subject: [PATCH] Search in rotated array. --- Algorithm/.vs/Algorithms/v14/.suo | Bin 236544 -> 244224 bytes .../Algorithms.Problems.LeetCode.csproj | 1 + Algorithm/LeetCode/Arrays/SearchInArrays.cs | 196 ++++++++++++++++++ .../MeetingRoom/MeetingRoomProblems.cs | 43 ++++ 4 files changed, 240 insertions(+) create mode 100644 Algorithm/LeetCode/Arrays/SearchInArrays.cs diff --git a/Algorithm/.vs/Algorithms/v14/.suo b/Algorithm/.vs/Algorithms/v14/.suo index 25432cc239df04438086ebcc5312bea05cc8b40a..dc7e30685b14fa69fbe0ec2dc225dda20e8b80a4 100644 GIT binary patch delta 8973 zcmds63s_V~mcCWh-GCtS62TV@wt_DLEg~8pjex;70pn_dAOecYOGIP9Nb6`~62?H6 zLuq160)}=>##L_OO|s5}F>b=HiK2NKO)|##C9^w8HpA+C6K6K;U$?t~5Xk$R{l2aK z{;pG}Ugw-T_f*}w_UHZW*Zu3_CMup3peSQO8-TAmJ3A$t022V&C>SvFCk;c6m)8;p zE%wiN=-HPt=e$X*sA$dMRdlWoiFP*@0q@WlvXbARHKwiL-7xYhR%QAZ40th`-(*oW z{@{)Vh5(pNPbr|ofFMAMF%B3Bcmw}~76Xy{0kZKhyVlRH&+_Xr<^k!zTtM>YBVPb41Qr2{fehewK-Om=Ujp3WF8>qqWp4UTS|=A6|*tP3{(VBi{nt161)y-zloy_Zo5dr~l&>%Y+C$70X7sKgCv{E(d^eQJpJ6 zQ(43Ea$d41k3->USmmVi{^joyk3GkRE2D+3o3A3Z=scJZzUfk$A|P8p|M;U|}G z3aKGE{6zle%}aRt_6fW)@|h;{aH^U^ zmk^T8H!Xh1k&|k1U{DV11%3rI00NNn?|0{FQBE!KBT&i{WF4z2jq6ammfy3gnylqF zR@IOqzA3-Pvl6z9V6Ss z!8d3WkD~+lb^8*-Q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Cgq&iemLf;A zCgZ3QbbK<7D@9t2PQmd;Y_lSN3byphuprNheM@x6E|%ut(Z8nm@aNCZr;+?Z44tSz zDaACtV7I*84y89?4cnDYdBh^%`2lKe?AqP%2hZy}EfhwZhHJ zX+BEkbF`C^ZTu^M$CSJpdP&U-8|Z5OY5@&W&b&lJ)c9Fp;B_xhKjm0C?I7Pjf^w*# zGnM!T+Dw$z-89CxxtX3P-0%pEU<)#$xN!w-`4O42((0r!>fSjzUcyv-2cdqvsDl3K E-vy8vZ2$lO diff --git a/Algorithm/LeetCode/Algorithms.Problems.LeetCode.csproj b/Algorithm/LeetCode/Algorithms.Problems.LeetCode.csproj index 2f49406..f9e9f99 100644 --- a/Algorithm/LeetCode/Algorithms.Problems.LeetCode.csproj +++ b/Algorithm/LeetCode/Algorithms.Problems.LeetCode.csproj @@ -47,6 +47,7 @@ + diff --git a/Algorithm/LeetCode/Arrays/SearchInArrays.cs b/Algorithm/LeetCode/Arrays/SearchInArrays.cs new file mode 100644 index 0000000..b6148ef --- /dev/null +++ b/Algorithm/LeetCode/Arrays/SearchInArrays.cs @@ -0,0 +1,196 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LeetCode.Arrays +{ + [TestClass] + public class SearchInArrays + { + /// + /// Rotated sorted array. + /// In Rotated sorted array, there is a point where a value is less than previous value. + /// For intance: 1 2 3 4 5 6 7 8 is rotated at 4 means 4 5 6 7 8 1 2 3 + /// It is increasing order from 4 and stops at 8 and then starts at 1 and goes in increasing order. + /// Now, finding the pivot where it stops increasing is the first step (i.e, the value is less than previous value) + /// If the target is less than the arr[pivot] and greater than array[n-1] then search in first set. + /// Otherwise search in second set of array. + /// If it's first set then the array range will be 0-pivot + /// If it's second set then the array range will be pivot+1 - (n-1) + /// Both the ranges are in increasing order, so we can apply the binary search. + /// + public int SearchInRotatedSortedArray(int[] array, int target) + { + // first step find the pivot value. + int pivotValue = this.FindPivot(array); + int index =0; + + if (target < array[pivotValue] && target > array[array.Length -1]) + { + // Binary search from 0 to mid-1 + int[] newArray = new int[pivotValue + 1]; + Array.Copy(array, newArray, newArray.Length); + index = this.BinarySearch(newArray, target); + } + else + { + // Binary search from pivot to n + int len = array.Length -1; + int[] newArray = new int[len - pivotValue]; + + Array.Copy(array, pivotValue + 1, newArray, 0, newArray.Length); + index = this.BinarySearch(newArray, target); + + index += pivotValue + 1; + } + + return index; + } + + private int BinarySearch(int[] array, int target) + { + int low = 0; + int high = array.Length - 1; + + while(low<= high) + { + int mid = (low + high) / 2; + + if (array[mid] < target) + { + low = mid + 1; + } + else if(array[mid] > target) + { + high = mid - 1; + } + else + { + return mid; + } + } + + return -1; + } + + private int FindPivot(int[] array) + { + // we can find the pivot in o(log(n)). + int low = 0; + int high = array.Length - 1; + + while (low < high) + { + int mid = (low + high) / 2; + if (array[mid] < array[mid-1]) + { + return mid -1; + } + else + { + low = mid + 1; + } + } + + return 0; + } + + public void SearchForRangeInArray() + { } + + /// + /// Not good impl as it's o(n). + /// + /// + /// + /// + public int SearchInsertPositionInArray(int[] array, int target) + { + int counter = 0; + while (counter < array.Length) + { + if (array[counter] < target) + { + counter += 1; + } + else if (array[counter] == target || array[counter] > target) + { + return counter; + } + } + + return counter; + } + + /// + /// Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. + /// [1,3,5,6], 5 -> 2 + /// [1,3,5,6], 2 -> 1 + /// + public int SearchInsertPositionInSortedArray_BinarySearch(int[] array, int target) + { + int low = 0; + int high = array.Length - 1; + int mid = 0; + while (low <= high) + { + mid = (low + high) / 2; + + if (array[mid] < target) + { + low = mid + 1; + } + else if (array[mid] > target) + { + high = mid - 1; + } + else + { + return mid; + } + } + + return low; + } + + [TestMethod] + public void TestSearchInRotatedSortedArray() + { + int index = this.SearchInRotatedSortedArray(new[] { 4, 5, 6, 7, 8, 1, 2, 3 }, 5); + Assert.AreEqual(index, 1); + + index = this.SearchInRotatedSortedArray(new[] { 3, 4, 5, 6, 7, 8, 1, 2 }, 2); + Assert.AreEqual(index, 7); + + index = this.SearchInRotatedSortedArray(new[] { 7, 8, 1, 2, 3, 4, 5, 6 }, 4); + Assert.AreEqual(index, 5); + } + + [TestMethod] + public void TestSearchInsertPositionInArray() + { + int[] array = { 1, 3, 5, 6 }; + int resultIndex = this.SearchInsertPositionInSortedArray_BinarySearch(array, 5); + + Assert.AreEqual(resultIndex, 2); + + array = new [] { 1, 3, 5, 6 }; + resultIndex = this.SearchInsertPositionInSortedArray_BinarySearch(array, 2); + + Assert.AreEqual(resultIndex, 1); + + array = new[] { 1, 3, 5, 6 }; + resultIndex = this.SearchInsertPositionInSortedArray_BinarySearch(array, 7); + + Assert.AreEqual(resultIndex, 4); + + array = new[] { 1, 3, 5, 6 }; + resultIndex = this.SearchInsertPositionInSortedArray_BinarySearch(array, 0); + + Assert.AreEqual(resultIndex, 0); + } + } +} diff --git a/Algorithm/LeetCode/MeetingRoom/MeetingRoomProblems.cs b/Algorithm/LeetCode/MeetingRoom/MeetingRoomProblems.cs index 5d5181f..2c61112 100644 --- a/Algorithm/LeetCode/MeetingRoom/MeetingRoomProblems.cs +++ b/Algorithm/LeetCode/MeetingRoom/MeetingRoomProblems.cs @@ -25,6 +25,49 @@ public bool CanPersonAttendAllMeetings(List meetings) return true; } + public List InsertMeeting(List meetings, Meeting newMeeting) + { + meetings.Sort(); + List resultMeeting = new List(); + + // In this for-loop just loop through the meetings and add only the current meeting to the list. + // the new meeting can either be inserted in the start, middle or end. + // if the new meeting has to be inserted in the middle instead of current meeting, change the current meeting to new meeting + // if there is a conflict, merge the meeting and set to the new meeting. + // at the end at least one meeting is left to add if there is conflict or new meeting is inserted in the middle. + // add to the end. + foreach(Meeting currentMeeting in meetings) + { + if (currentMeeting.EndTime < newMeeting.StartTime) + { + resultMeeting.Add(currentMeeting); + } + else if (currentMeeting.StartTime > newMeeting.EndTime) + { + resultMeeting.Add(newMeeting); + + // newmeeting is set to currentmeeting so that in next iteration the currentmeeting can be added to the result. + newMeeting = currentMeeting; + } + else if (currentMeeting.EndTime > newMeeting.StartTime) + { + int startTime = Math.Min(currentMeeting.StartTime, newMeeting.StartTime); + int endTime = Math.Max(currentMeeting.EndTime, newMeeting.EndTime); + + Meeting meeting = new Meeting(startTime, endTime); + + // merge the new meeting with current meeting, dont add yet because there could be more meetings to be merged. + // once this condition is not met, then you can add the new meeting to the end. + newMeeting = meeting; + } + } + + // after exit of the loop, the new meeting is still not added to the list. + resultMeeting.Add(newMeeting); + + return resultMeeting; + } + /// /// Return the merged meetings. ///