@@ -3893,6 +3893,120 @@ class SolutionOth : VersionControl() {
3893
3893
}
3894
3894
}
3895
3895
3896
+ /* *
3897
+ * 275. H-Index II
3898
+ */
3899
+
3900
+ fun hIndex (citations : IntArray ): Int {
3901
+ val n = citations.size
3902
+ var left = 0
3903
+ var right = n - 1
3904
+
3905
+ while (left <= right) {
3906
+ val mid = left + (right - left) / 2
3907
+ if (citations[mid] >= n - mid) {
3908
+ right = mid - 1
3909
+ } else {
3910
+ left = mid + 1
3911
+ }
3912
+ }
3913
+
3914
+ return n - left
3915
+ }
3916
+
3917
+ /* *
3918
+ * 378. Kth Smallest Element in a Sorted Matrix
3919
+ */
3920
+
3921
+ fun kthSmallest (matrix : Array <IntArray >, k : Int ): Int {
3922
+ val pq = PriorityQueue <Int >()
3923
+ val len = matrix.size
3924
+ for (i in 0 until len) {
3925
+ for (j in 0 until len) {
3926
+ pq.offer(matrix[i][j])
3927
+ }
3928
+ }
3929
+ var res = 0
3930
+ for (i in 0 until k) {
3931
+ res = pq.poll()
3932
+ }
3933
+ return res
3934
+ }
3935
+
3936
+ /* *
3937
+ * 74. Search a 2D Matrix
3938
+ */
3939
+
3940
+ fun searchMatrix2D (matrix : Array <IntArray >, target : Int ): Boolean {
3941
+ var row = 0
3942
+ var col = matrix[0 ].size - 1
3943
+ while (row < matrix.size && col >= 0 ) {
3944
+ if (matrix[row][col] == target) return true
3945
+ else if (matrix[row][col] < target) row++
3946
+ else col--
3947
+ }
3948
+ return false
3949
+ }
3950
+
3951
+ /* *
3952
+ * 33. Search in Rotated Sorted Array
3953
+ */
3954
+
3955
+
3956
+ fun search (nums : IntArray , target : Int ): Int {
3957
+ var left = 0
3958
+ var right = nums.size - 1
3959
+
3960
+ while (left <= right) {
3961
+ val mid = left + (right - left) / 2
3962
+
3963
+ if (nums[mid] == target) {
3964
+ return mid
3965
+ }
3966
+
3967
+ if (nums[left] <= nums[mid]) {
3968
+ if (target >= nums[left] && target < nums[mid]) {
3969
+ right = mid - 1
3970
+ } else {
3971
+ left = mid + 1
3972
+ }
3973
+ } else {
3974
+ if (target > nums[mid] && target <= nums[right]) {
3975
+ left = mid + 1
3976
+ } else {
3977
+ right = mid - 1
3978
+ }
3979
+ }
3980
+ }
3981
+
3982
+ return - 1
3983
+ }
3984
+
3985
+ /* *
3986
+ * 81. Search in Rotated Sorted Array II
3987
+ */
3988
+
3989
+
3990
+ fun searchII (nums : IntArray , target : Int ): Boolean {
3991
+ var l = 0
3992
+ var r = nums.size - 1
3993
+ while (l <= r) {
3994
+ var m = l + (r - l) / 2
3995
+ if (nums[m] == target) return true
3996
+ if (nums[l] == nums[m] && nums[m] == nums[r]) {
3997
+ l++
3998
+ r--
3999
+ } else if (nums[l] <= nums[m]) {
4000
+ if (nums[l] <= target && nums[m] >= target) r = m - 1
4001
+ else l = m + 1
4002
+ } else {
4003
+ if (target > nums[m] && target <= nums[r]) l = m + 1
4004
+ else r = m - 1
4005
+ }
4006
+ }
4007
+ return false
4008
+ }
4009
+
3896
4010
3897
4011
3898
4012
0 commit comments