-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #788 from 0xff-dev/1985
Add solution and test-cases for problem 1985
- Loading branch information
Showing
3 changed files
with
93 additions
and
24 deletions.
There are no files selected for viewing
39 changes: 26 additions & 13 deletions
39
leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 56 additions & 1 deletion
57
leetcode/1901-2000/1985.Find-the-Kth-Largest-Integer-in-the-Array/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,60 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
import ( | ||
"container/heap" | ||
) | ||
|
||
type hp1985 []string | ||
|
||
func greater1985(a, b string) bool { | ||
la, lb := len(a), len(b) | ||
if la > lb { | ||
return true | ||
} else if la < lb { | ||
return false | ||
} | ||
for k := 0; k < la; k++ { | ||
if a[k] == b[k] { | ||
continue | ||
} | ||
return a[k] > b[k] | ||
} | ||
return true | ||
} | ||
func (h *hp1985) Len() int { | ||
return len(*h) | ||
} | ||
|
||
func (h *hp1985) Less(i, j int) bool { | ||
return !greater1985((*h)[i], (*h)[j]) | ||
} | ||
|
||
func (h *hp1985) Swap(i, j int) { | ||
(*h)[i], (*h)[j] = (*h)[j], (*h)[i] | ||
} | ||
func (h *hp1985) Push(x interface{}) { | ||
*h = append(*h, x.(string)) | ||
} | ||
|
||
func (h *hp1985) Pop() interface{} { | ||
old := *h | ||
l := len(*h) | ||
x := old[l-1] | ||
*h = old[:l-1] | ||
return x | ||
} | ||
|
||
func Solution(nums []string, k int) string { | ||
h := hp1985{} | ||
for _, str := range nums { | ||
if h.Len() == k { | ||
if !greater1985(h[0], str) { | ||
heap.Pop(&h) | ||
heap.Push(&h, str) | ||
} | ||
continue | ||
} | ||
heap.Push(&h, str) | ||
} | ||
return h[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters