Skip to content

Commit 105575a

Browse files
committed
finish 208,215
1 parent 67602c7 commit 105575a

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 208. Implement Trie (Prefix Tree)
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Design, Trie.
5+
- Similar Questions: Add and Search Word - Data structure design, Design Search Autocomplete System, Replace Words, Implement Magic Dictionary.
6+
7+
## Problem
8+
9+
Implement a trie with ```insert```, ```search```, and ```startsWith``` methods.
10+
11+
**Example:**
12+
13+
```
14+
Trie trie = new Trie();
15+
16+
trie.insert("apple");
17+
trie.search("apple"); // returns true
18+
trie.search("app"); // returns false
19+
trie.startsWith("app"); // returns true
20+
trie.insert("app");
21+
trie.search("app"); // returns true
22+
```
23+
24+
**Note:**
25+
26+
- You may assume that all inputs are consist of lowercase letters ```a-z```.
27+
- All inputs are guaranteed to be non-empty strings.
28+
29+
## Solution
30+
31+
```javascript
32+
var Node = function () {
33+
this.children = {};
34+
this.isWord = false;
35+
};
36+
37+
/**
38+
* Initialize your data structure here.
39+
*/
40+
var Trie = function() {
41+
this.root = new Node();
42+
};
43+
44+
/**
45+
* Inserts a word into the trie.
46+
* @param {string} word
47+
* @return {void}
48+
*/
49+
Trie.prototype.insert = function(word) {
50+
var len = word.length;
51+
var node = this.root;
52+
var char = 0;
53+
for (var i = 0; i < len; i++) {
54+
char = word[i];
55+
if (!node[char]) node[char] = new Node();
56+
node = node[char];
57+
}
58+
node.isWord = true;
59+
};
60+
61+
/**
62+
* Returns if the word is in the trie.
63+
* @param {string} word
64+
* @return {boolean}
65+
*/
66+
Trie.prototype.search = function(word) {
67+
var len = word.length;
68+
var node = this.root;
69+
var char = 0;
70+
for (var i = 0; i < len; i++) {
71+
char = word[i];
72+
if (!node[char]) return false;
73+
node = node[char];
74+
}
75+
return node.isWord;
76+
};
77+
78+
/**
79+
* Returns if there is any word in the trie that starts with the given prefix.
80+
* @param {string} prefix
81+
* @return {boolean}
82+
*/
83+
Trie.prototype.startsWith = function(prefix) {
84+
var len = prefix.length;
85+
var node = this.root;
86+
var char = 0;
87+
for (var i = 0; i < len; i++) {
88+
char = prefix[i];
89+
if (!node[char]) return false;
90+
node = node[char];
91+
}
92+
return true;
93+
};
94+
95+
/**
96+
* Your Trie object will be instantiated and called as such:
97+
* var obj = Object.create(Trie).createNew()
98+
* obj.insert(word)
99+
* var param_2 = obj.search(word)
100+
* var param_3 = obj.startsWith(prefix)
101+
*/
102+
```
103+
104+
**Explain:**
105+
106+
nope.
107+
108+
**Complexity:**
109+
110+
* Time complexity : O(h).
111+
* Space complexity : O(h).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 215. Kth Largest Element in an Array
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Divide and Conquer, Heap.
5+
- Similar Questions: Wiggle Sort II, Top K Frequent Elements, Third Maximum Number, Kth Largest Element in a Stream.
6+
7+
## Problem
8+
9+
Find the **k**th largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: [3,2,1,5,6,4] and k = 2
15+
Output: 5
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: [3,2,3,1,2,4,5,5,6] and k = 4
22+
Output: 4
23+
```
24+
25+
**Note: **
26+
You may assume k is always valid, 1 ≤ k ≤ array's length.
27+
28+
## Solution
29+
30+
```javascript
31+
/**
32+
* @param {number[]} nums
33+
* @param {number} k
34+
* @return {number}
35+
*/
36+
var findKthLargest = function(nums, k) {
37+
return quickSelect(nums, 0, nums.length - 1, k);
38+
};
39+
40+
var quickSelect = function (nums, left, right, k) {
41+
var le = left;
42+
var ri = right;
43+
var mid = nums[right];
44+
while (le < ri) {
45+
if (nums[le++] > mid) swap(nums, --le, --ri);
46+
}
47+
swap(nums, le, right);
48+
var len = right - le;
49+
if (len === k - 1) return nums[le];
50+
else if (len < k - 1) return quickSelect(nums, left, le - 1, k - len - 1);
51+
else return quickSelect(nums, le + 1, right, k);
52+
};
53+
54+
var swap = function (nums, i, j) {
55+
var tmp = nums[i];
56+
nums[i] = nums[j];
57+
nums[j] = tmp;
58+
}
59+
```
60+
61+
**Explain:**
62+
63+
nope.
64+
65+
**Complexity:**
66+
67+
* Time complexity : O(n).
68+
* Space complexity : O(1).

0 commit comments

Comments
 (0)