Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.38 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Cr7Sund_DS;
  5.  
  6. class Program
  7. {
  8. public static void Main()
  9. {
  10. Solution solution = new Solution();
  11. Console.WriteLine(solution.NthSuperUglyNumber(345, new int[] { 2, 7, 13, 19 }));
  12.  
  13. }
  14.  
  15. public class Solution
  16. {
  17. class Node : IComparable<Node>
  18. {
  19. public int val;
  20. public int index;
  21. public readonly int prim;
  22.  
  23. public Node(int _val, int _index, int _prim)
  24. {
  25. val = _val;
  26. index = _index;
  27. prim = _prim;
  28. }
  29.  
  30. public int CompareTo(Node other)
  31. {
  32. return val.CompareTo(other.val);
  33. }
  34. }
  35.  
  36. public int NthSuperUglyNumber(int n, int[] primes)
  37. {
  38. if (n < 1 || primes.Length < 1) return 0;
  39.  
  40. int[] nums = new int[n];
  41. nums[0] = 1;
  42. var minHeap = new MinHeap<Node>(n);
  43. for (int i = 0; i < primes.Length; i++)
  44. minHeap.Add(new Node(primes[i], 1, primes[i]));
  45.  
  46. for (int i = 1; i < n; i++)
  47. {
  48. nums[i] = minHeap.Top().val;
  49. while (nums[i] == minHeap.Top().val)
  50. {
  51. var min = minHeap.Pop();
  52. minHeap.Add(new Node(
  53. min.prim * nums[min.index],
  54. min.index + 1, min.prim
  55. ));
  56. }
  57. }
  58.  
  59. return nums[n - 1];
  60. }
  61. }
  62. }
  63.  
  64. namespace Cr7Sund_DS
  65. {
  66. using System.Collections.Generic;
  67. using System.Collections;
  68. using System;
  69.  
  70. public class MinHeap<T> : Heap<T>
  71. where T : IComparable<T>
  72. {
  73. public MinHeap(int size) : base(size)
  74. {
  75.  
  76. }
  77.  
  78. public MinHeap() : base()
  79. {
  80.  
  81. }
  82.  
  83. public MinHeap(IEnumerable<T> collection) : base()
  84. {
  85.  
  86. }
  87.  
  88. protected override int Compare(T item, T other)
  89. {
  90. return other.CompareTo(item);
  91. }
  92. }
  93.  
  94. public class MaxHeap<T> : Heap<T>
  95. where T : IComparable<T>
  96. {
  97. public MaxHeap(int size) : base(size)
  98. {
  99.  
  100. }
  101.  
  102. public MaxHeap() : base()
  103. {
  104.  
  105. }
  106.  
  107. public MaxHeap(IEnumerable<T> collection) : base()
  108. {
  109.  
  110. }
  111.  
  112. protected override int Compare(T item, T other)
  113. {
  114. return item.CompareTo(other);
  115. }
  116. }
  117.  
  118. public abstract class Heap<T> : IEnumerable<T>, IEnumerable, ICollection<T>, ICollection
  119. where T : IComparable<T>
  120. {
  121. protected T[] _array;
  122. protected int _lastPos = -1;
  123. int _defaulCapcity = 4;
  124. Object _syncRoot;
  125. static T[] EMPTYARRAY = new T[0];
  126.  
  127. public int Count => _lastPos + 1;
  128. public bool IsReadOnly => false;
  129.  
  130. //Be Cautions, it maybe lead to error in multiple threading
  131. //Did not be testify in real project
  132. public bool IsSynchronized => true;
  133.  
  134. public object SyncRoot
  135. {
  136. get
  137. {
  138. if (_syncRoot == null)
  139. {
  140. System.Threading.Interlocked.CompareExchange<Object>(
  141. ref _syncRoot, new Object(), null);
  142. }
  143. return _syncRoot;
  144. }
  145. }
  146.  
  147. protected abstract int Compare(T item, T other);
  148.  
  149. protected Heap() => _array = EMPTYARRAY;
  150.  
  151. protected Heap(int size) => _array = new T[size];
  152.  
  153. protected Heap(IEnumerable<T> collection)
  154. {
  155. if (collection == null)
  156. throw new NullReferenceException();
  157.  
  158. var c = collection as ICollection<T>;
  159. if (c != null)
  160. {
  161. int count = c.Count;
  162. _array = new T[count];
  163. c.CopyTo(_array, 0);
  164. _lastPos = count - 1;
  165. }
  166. else
  167. {
  168. _array = new T[_defaulCapcity];
  169.  
  170. using (var en = collection.GetEnumerator())
  171. {
  172. while (en.MoveNext())
  173. {
  174. Add(en.Current);
  175. }
  176. }
  177. }
  178. }
  179.  
  180. public bool Empty() => _lastPos < 0 || _array.Length == 0;
  181.  
  182. public T Top() => Empty() ?
  183. throw new IndexOutOfRangeException() : _array[0];
  184.  
  185. bool Full() => _lastPos == _array.Length;
  186.  
  187. public void Clear()
  188. {
  189. Array.Clear(_array, 0, _array.Length);
  190. _lastPos = -1;
  191. }
  192.  
  193. //Don't suggested , Cost O(n)time
  194. public bool Contains(T item)
  195. {
  196. if ((Object)item == null) return false;
  197. for (int i = 0; i < Count; i++)
  198. {
  199. if (_array[i].Equals(item))
  200. {
  201. return true;
  202. }
  203. }
  204.  
  205. return false;
  206. }
  207.  
  208. //implementing ICollection<T>
  209. public void CopyTo(T[] array, int startIndex = 0)
  210. {
  211. if (array == null) throw new NullReferenceException();
  212. if (Empty()) throw new IndexOutOfRangeException("Empty heap");
  213.  
  214. if (startIndex < 0 || startIndex > array.Length)
  215. throw new IndexOutOfRangeException();
  216.  
  217. //it should be out of range excetption
  218. if (array.Length - startIndex < Count)
  219. throw new IndexOutOfRangeException("the left capcity should be larger than copying size ");
  220.  
  221. Array.Copy(_array, 0, array, startIndex, Count);
  222. }
  223.  
  224. public void CopyTo(Array array, int index)
  225. {
  226. _array.CopyTo(array, index);
  227. }
  228.  
  229. public void TrimExcess()
  230. {
  231. int threshold = (int)((double)_array.Length * 0.9);
  232. if (Count < threshold)
  233. {
  234. var newarray = new T[Count];
  235. Array.Copy(_array, 0, newarray, 0, Count);
  236. _array = newarray;
  237. }
  238. }
  239.  
  240. public void Add(T item)
  241. {
  242. ++_lastPos;
  243. if (Full())
  244. {
  245. var newArray = new T[
  246. _array.Length == 0 ? _defaulCapcity : 2 * _array.Length];
  247. //list should use linq.Clone
  248. //be careful the size
  249. Array.Copy(_array, 0, newArray, 0, _lastPos);
  250. _array = newArray;
  251. }
  252. _array[_lastPos] = item;
  253. TrickleUp();
  254. }
  255.  
  256. public T Remove(int index)
  257. {
  258. if (index > _lastPos || Empty()) throw new IndexOutOfRangeException();
  259. var temp = _array[index];
  260.  
  261. Swap(_lastPos--, index);
  262. int parent = (index - 1) / 2;
  263. if (parent >= 0 && Compare(_array[index], _array[parent]) > 0)
  264. TrickleUp(index);
  265. else
  266. TrickleDown(index);
  267.  
  268. return temp;
  269. }
  270.  
  271. // for implementing ICollection
  272. public bool Remove(T item)
  273. {
  274. if (Empty()) throw new IndexOutOfRangeException();
  275. if ((Object)item == null) return false;
  276. for (int i = 0; i <= _lastPos; i++)
  277. {
  278. if (_array[i].Equals(item))
  279. {
  280. Remove(i);
  281. return true;
  282. }
  283. }
  284.  
  285. return false;
  286. }
  287.  
  288. #region custom methos
  289. public T[] ToArray()
  290. {
  291. var returnArray = new T[Count];
  292. CopyTo(returnArray);
  293. return returnArray;
  294. }
  295.  
  296. //should use linq, so decided by yourself
  297. //-------------------------------
  298. // public List<T> ToList () {
  299. // // return ToArray ().ToList ();
  300. // }
  301.  
  302. #endregion
  303.  
  304. #region heap operation
  305.  
  306. public void Push(T item)
  307. {
  308. Add(item);
  309. }
  310.  
  311. public T Pop()
  312. {
  313. if (Empty()) throw new IndexOutOfRangeException("No items left");
  314. T top = _array[0];
  315. if (_lastPos == -1) return top;
  316.  
  317. Swap(_lastPos--, 0);
  318. TrickleDown();
  319. return top;
  320. }
  321.  
  322. public T[] Sort()
  323. {
  324. Console.WriteLine("K");
  325. int goBack = this._lastPos;
  326. while (this._lastPos >= 0)
  327. {
  328. T val = Pop();
  329. }
  330. this._lastPos = goBack;
  331. return _array;
  332. }
  333.  
  334. protected T[] ReverseSort()
  335. {
  336. var queue = new Queue<T>();
  337. int goBack = this._lastPos;
  338. while (this._lastPos >= 0)
  339. {
  340. queue.Enqueue(Pop());
  341. }
  342. this._lastPos = goBack;
  343.  
  344. var newArray = new T[goBack + 1];
  345. int i = -1;
  346. while (queue.Count > 0)
  347. {
  348. newArray[++i] = queue.Dequeue();
  349. }
  350.  
  351. return newArray;
  352. }
  353.  
  354. public T[] Sort(bool IsReverse = false)
  355. {
  356. //change state
  357. //---------------------
  358. var resultArray = ReverseSort();
  359. // Why we need a new Array?
  360. var returnArray = new T[Count];
  361. if (IsReverse)
  362. {
  363. // 1. avoiding changing the newArray because of reference type
  364. Array.Copy(resultArray, 0, returnArray, 0, Count);
  365. }
  366. else
  367. {
  368. // 2. trimming the redundent null elements
  369. CopyTo(returnArray);
  370. }
  371. //go back to keep heap after sort
  372. //Array.Copy(resultArray, 0, _array, 0, Count);
  373. _array = resultArray;
  374.  
  375. return returnArray;
  376. }
  377.  
  378. void TrickleUp()
  379. {
  380. int index = _lastPos;
  381. while (index > 0)
  382. {
  383. int parentIndex = (index - 1) / 2;
  384. if (Compare(_array[index], _array[parentIndex]) <= 0) break;
  385.  
  386. Swap(index, parentIndex);
  387. index = parentIndex;
  388. }
  389. }
  390.  
  391. void TrickleUp(int index)
  392. {
  393. while (index > 0)
  394. {
  395. int parentIndex = (index - 1) / 2;
  396. if (Compare(_array[index], _array[parentIndex]) <= 0) break;
  397.  
  398. Swap(index, parentIndex);
  399. index = parentIndex;
  400. }
  401. }
  402.  
  403. void TrickleDown(int index = 0)
  404. {
  405. while (index >= 0)
  406. {
  407. int leftIndex = (index * 2) + 1;
  408. int rightIndex = (index * 2) + 2;
  409.  
  410. if (leftIndex <= _lastPos)
  411. {
  412. int maxIndex = rightIndex <= _lastPos && Compare(_array[leftIndex], _array[rightIndex]) < 0 ?
  413. rightIndex : leftIndex;
  414. if (Compare(_array[maxIndex], _array[index]) <= 0) break;
  415. Swap(maxIndex, index);
  416. index = maxIndex;
  417. }
  418. else
  419. {
  420. break;
  421. }
  422. }
  423. }
  424.  
  425. //undo deletion if you not trim the arrya before
  426. void UnDoDelete(int steps)
  427. {
  428. steps = Math.Min(steps, _array.Length - Count);
  429. for (int i = steps; i > 0; i--)
  430. {
  431. Add(_array[_lastPos + i]);
  432. }
  433. }
  434.  
  435. void Swap(int a, int b)
  436. {
  437. T temp = _array[a];
  438. _array[a] = _array[b];
  439. _array[b] = temp;
  440. }
  441. #endregion
  442.  
  443. #region IEnumeator
  444. public IEnumerator<T> GetEnumerator()
  445. {
  446. //return ((IEnumerable<T>)_array).GetEnumerator();
  447. return new Enumerator(this);
  448. }
  449.  
  450. IEnumerator IEnumerable.GetEnumerator()
  451. {
  452. //return _array.GetEnumerator();
  453. return new Enumerator(this);
  454. }
  455.  
  456. struct Enumerator : IEnumerator<T>,
  457. IEnumerator
  458. {
  459. Heap<T> _heap;
  460. T _current;
  461. int _index;
  462.  
  463. internal Enumerator(Heap<T> heap)
  464. {
  465. _heap = heap;
  466. _current = default(T);
  467. _index = -2;
  468. }
  469.  
  470. public T Current
  471. {
  472. get
  473. {
  474. //enumNotStarted
  475. if (_index == -2) throw new NullReferenceException();
  476. //enumEnded
  477. if (_index == -1) throw new IndexOutOfRangeException();
  478. return _current;
  479. }
  480. }
  481.  
  482. object IEnumerator.Current => Current;
  483.  
  484. public void Dispose() => _index = -1;
  485.  
  486. public bool MoveNext()
  487. {
  488. bool retVal;
  489. //first call to enumerator
  490. if (_index == -2)
  491. {
  492. _index = _heap._lastPos;
  493. retVal = (_index >= 0);
  494. if (retVal)
  495. {
  496. _current = _heap._array[_index];
  497. }
  498.  
  499. return retVal;
  500. }
  501. if (_index == -1) return false;
  502.  
  503. retVal = (--_index >= 0);
  504. if (retVal)
  505. _current = _heap._array[_index];
  506. else
  507. _current = default(T);
  508.  
  509. return retVal;
  510. }
  511.  
  512. public void Reset()
  513. {
  514. _index = -2;
  515. _current = default(T);
  516. }
  517. }
  518.  
  519. #endregion
  520.  
  521. }
  522.  
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement