fker's blog

By fker, history, 7 weeks ago, In English

2147A - Shortest Increasing Path

I dont know if it is a good idea to use binary search to find if a valid break point can be set on x axis. although it passed all tests very fast. ~~~~~ left = 1 right = xi find = False while left <= right: mid = (left + right) // 2 if mid < yi and yi < xi — mid: find = True break else: right = mid — 1 if find: print(3) else: print(-1) ~~~~~

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
7 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Where did the binary search come from? There are only 3 possible answers: -1, 2 and 3. You can watch the editorial for detailed explanation, but binary search isn't used here

»
7 weeks ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

binary search is overkill. you can just compare x and y in a certain way and solve it in a few if statements:

solution