forked from xieqilu/Qilu-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path81.SortArrayByFrequency.cs
More file actions
121 lines (100 loc) · 2.97 KB
/
Copy path81.SortArrayByFrequency.cs
File metadata and controls
121 lines (100 loc) · 2.97 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
Problem1:
Find k most frequently occurring numbers
given a list of N integers, write a program to find k most repeating integers.
[1,1,1,2,2,3,4,4] (seems like the list is already sorted)
举例
k=1
[1]
k=2
[1,2]
k=3
[1,2,4].
Follow-up: infinite stream of sorted numbers coming(用什么实现,伪代码,时间复杂度)
Problem2:
Sort array by frequency
given an array of int, return a list in which element are ordered by frequency in the given array.
If two elements have the same frequency, the one appears first in array should also apprea first
in the output list
Example:
array: { 5, 2, 2, 8, 5, 6, 8, 8 }
output: {8,8,8,5,5,2,2,6}
*
*
*/
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
namespace SortArrayByFrequency
{
class Element{
public int occurence{ get; set;}
public int firstIndex{ get; set;}
public Element(int o, int f){
this.occurence = o;
this.firstIndex = f;
}
}
class Finder{
//Sort list by frequency
public static List<int> sortArray(int[] arr){ //Time:O(nlogn), Space:O(n)
//Handle edge case
if (arr.Length == 0)
return null;
Dictionary<int, Element> dt = new Dictionary<int, Element> ();
for (int i = 0; i < arr.Length; i++) { //put all info into dt, Time: O(n)
if (dt.ContainsKey (arr [i])) {
dt [arr [i]].occurence++;
} else {
Element temp = new Element (1, i);
dt.Add (arr [i], temp);
}
}
//sort object in dt by occurence in descending order then by firstIndex in ascending order
//use QuickSort, time: O(nlogn)
List<KeyValuePair<int, Element>> sortedList = dt.OrderByDescending (o => o.Value.occurence).
ThenBy (o => o.Value.firstIndex).ToList ();
//Convert the sortedList to a result list
List<int> result = new List<int> ();
foreach (KeyValuePair<int, Element> kp in sortedList) {
for (int i = 0; i < kp.Value.occurence; i++)
result.Add (kp.Key);
}
return result;
}
//get the k most repeating element in the array
public static List<int> findTopK(int[] arr, int k){ //Time:O(nlogn) Space:O(n)
//handle edge case
if (k == 0 || arr.Length < k)
return null;
Dictionary<int,int> dt = new Dictionary<int, int> ();
for (int i = 0; i < arr.Length; i++) { //put frequency info into dt
if (dt.ContainsKey (arr [i]))
dt [arr [i]]++;
else
dt.Add (arr [i], 1);
}
//convert dt to list, then sort the list
List<KeyValuePair<int,int>> list= dt.OrderByDescending (o => o.Value).ToList ();
List<int> result = new List<int> ();
for (int i = 0; i < k; i++)
result.Add (list [i].Key);
return result;
}
}
class MainClass
{
public static void Main (string[] args)
{
int[] arr = new int[]{ 5, 2, 2, 8, 5, 6, 8, 8 };
List<int> list = Finder.sortArray (arr);
foreach (int i in list)
Console.Write (i + " ");
Console.WriteLine (" ");
List<int> result = Finder.findTopK (arr,3);
foreach (int i in result)
Console.Write (i + " ");
}
}
}