LeetCode 378. Kth Smallest Element in a Sorted Matrix
題目連結 http://tinyurl.com/y8sc949p
Given a n x n matrix where each of the rows and columns are sorted in
ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth
distinct element.
Example:
matrix = [[ 1, 5, 9],
[10,11,13],
[12,13,15]]
k = 8
return 13
目前正在研究用binary search解這題 http://tinyurl.com/ybqw4ubd
YUANGAO1023提到的Solution 2: Binary Search
大致的結構我都看懂了
但是不懂的是為什麼是在while迴圈的最後
是else hi = mid; 而不是 else hi = mid - 1;
我有自己代數字實際跑一遍, else hi = mid; 是正確的,但是卻不知道為什麼這是正確
也不懂為什麼 hi = mid - 1就不行
謝謝