Skip to content

Commit 782474b

Browse files
committed
feat:leetcode
1 parent ac7639b commit 782474b

File tree

11 files changed

+107
-49
lines changed

11 files changed

+107
-49
lines changed

AddBinary/add_binary.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class E {
180180
b = temp;
181181
}
182182
var carry = '0';
183-
int k = 0;
183+
// int k = 0;
184184
int j = b.length - 1;
185185
for (int i = a.length - 1; i >= 0; i--) {
186186
if (j >= 0) {

LongestPathWithDifferentAdjacentCharacters/longest_path_with_Different_adjacent_characters.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class A {
6767
return maxi;
6868
}
6969

70-
int iterate(Node root) {
70+
int iterate(Node? root) {
7171
if (root == null) {
7272
return 0;
7373
}

MaximumProductOfSplittedBinaryTree/maximum_product_of_splitted_binary_tree.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class A {
5151
int maxProd = 0;
5252
int maxProduct(TreeNode? root) {
5353
dfs(root);
54-
int rootProd = checkMax(root);
54+
// int rootProd = checkMax(root);
5555
return (maxProd % (pow(10, 9) + 7)).toInt();
5656
}
5757

MinimumTimeToMakeRopeColorful/minimum_time_to_make_rope_colorful.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ class A {
5353
int sum = 0, maxSum = 0;
5454
for (int i = 0; i < colors.length; i++) {
5555
sum += neededTime[i];
56-
int max = neededTime[i], count = 0;
56+
int max = neededTime[i];
57+
// int count = 0;
5758
for (int j = i + 1;
5859
j < colors.length && colors.codeUnitAt(i) == colors.codeUnitAt(j);
5960
j++) {
6061
sum += neededTime[j];
61-
count++;
62+
// count++;
6263
if (neededTime[j] > max) max = neededTime[j];
6364
i = j;
6465
}

NumberOfGoodPaths/number_of_good_paths.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class C {
228228
}
229229

230230
// traverse node values from lowest to highest
231-
MapEntry<int, List<int>> curr = tm.entries.first;
231+
MapEntry<int, List<int>>? curr = tm.entries.first;
232232
while (curr != null) {
233233
List<int> listNodes = curr.value;
234234

TheSkylineProblem/the_skyline_problem.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import 'dart:math';
4848

4949
class A {
5050
List<List<int>> getSkyline(List<List<int>> buildings) {
51-
int n = buildings.length;
51+
// int n = buildings.length;
5252
List<int> keys = [];
5353
for (List<int> building in buildings) {
5454
keys.add(building[0]);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
3+
*/
4+
5+
import 'dart:collection';
6+
7+
class A {
8+
List<int> topKFrequent(List<int> words, int k) {
9+
final HashMap<int, int> hashMap = HashMap<int, int>();
10+
11+
for (final int word in words) {
12+
if (hashMap[word] == null) {
13+
hashMap[word] = 1;
14+
} else {
15+
hashMap[word] = hashMap[word]! + 1;
16+
}
17+
}
18+
19+
final List<MapEntry<int, int>> sortedHashMap =
20+
hashMap.entries.toList(growable: true)
21+
..sort((a, b) {
22+
if (a.value.compareTo(b.value) == 0) {
23+
return a.key.compareTo(b.key);
24+
}
25+
return b.value.compareTo(a.value);
26+
});
27+
28+
return sortedHashMap.sublist(0, k).map((e) => e.key).toList();
29+
}
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import "container/heap"
4+
5+
type Word struct {
6+
word int
7+
frequency int
8+
}
9+
10+
func newWord(word int, frequency int) *Word {
11+
return &Word{word: word, frequency: frequency}
12+
}
13+
14+
type H struct {
15+
items []*Word
16+
}
17+
18+
func (h *H) Len() int { return len(h.items) }
19+
20+
func (h *H) Less(i, j int) bool {
21+
if h.items[i].frequency != h.items[j].frequency {
22+
return h.items[i].frequency < h.items[j].frequency
23+
}
24+
return h.items[i].word > h.items[j].word
25+
}
26+
27+
func (h *H) Swap(i, j int) { h.items[i], h.items[j] = h.items[j], h.items[i] }
28+
29+
func (h *H) Push(i interface{}) { h.items = append(h.items, i.(*Word)) }
30+
31+
func (h *H) Pop() interface{} {
32+
n := len(h.items)
33+
item := h.items[n-1]
34+
h.items = h.items[:n-1]
35+
return item
36+
}
37+
38+
func topKFrequent(words []int, k int) []int {
39+
counts := map[int]int{}
40+
h := &H{}
41+
for _, v := range words {
42+
counts[v]++
43+
44+
}
45+
for key, v := range counts {
46+
heap.Push(h, newWord(key, v))
47+
if h.Len() > k {
48+
heap.Pop(h)
49+
}
50+
}
51+
result := []int{}
52+
for h.Len() > 0 {
53+
result = append([]int{heap.Pop(h).(*Word).word}, result...)
54+
}
55+
return result
56+
}

TopKFrequentElements/top_k_frequent_elements.md

Whitespace-only changes.

TopKFrequentWords/top_k_frequent_words.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class A {
3939
List<String> topKFrequent(List<String> words, int k) {
4040
final hashMap = <String, int>{};
4141

42-
for (final word in words) {
42+
for (final String word in words) {
4343
if (hashMap[word] == null) {
4444
hashMap[word] = 1;
4545
} else {

0 commit comments

Comments
 (0)