forked from xieqilu/Qilu-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path72.KthLongestStrInArray.cs
52 lines (44 loc) · 1.19 KB
/
72.KthLongestStrInArray.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
//find kth longest string in a string array
//assume that all strings in the input array have different length
//similiar to find kth smallest element in an array, use QuickSelect to solve it
//in O(n) time
using System;
using System.Collections.Generic;
using System.Collections;
namespace KthLongestStrInArray
{
public class Finder
{
public static string FindKthLongestStr(List<string> strs, int k) //O(n)
{
if (strs.Count < k)
return null;
List<string> left = new List<string> ();
List<string> right = new List<string> ();
int middle = strs.Count / 2;
string pivot = strs [middle];
foreach (string s in strs) {
if (s.Length > pivot.Length)
right.Add (s);
else if (s.Length < pivot.Length)
left.Add (s);
}
if (right.Count > k - 1)
return FindKthLongestStr (right, k);
else if (right.Count < k - 1)
return FindKthLongestStr (left, k - right.Count - 1);
else
return pivot;
}
}
class MainClass
{
public static void Main (string[] args)
{
List<string> strs = new List<string> (){ "a", "ab", "abc", "abcd", "abcdefg" };
for (int i = 1; i < 6; i++) {
Console.WriteLine (Finder.FindKthLongestStr (strs,i));
}
}
}
}