I was being interviewed 4 hours ago and here it is.
Spoiler
The solution must be O(n) Time.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
I was being interviewed 4 hours ago and here it is.
Given an array A, that is a permutation of n numbers [1-n]. Find the number of subarrays S that meets the following condition max(S) - min(S) = length(S) - 1.
Example 1:
Input: [4, 3, 1, 2, 5]
Output: 10
Explanation: subarrays that meet the condition are
[4]
[3]
[1]
[2]
[5]
[4 3]
[1 2]
[3 1 2]
[4 3 1 2]
[4 3 1 2 5]
There are 10 subarrays that meet the condition, so the answer should be 10.
The solution must be O(n) Time.
Название |
---|
Not quite convinced an $$$O(N)$$$ solution exists.
By the way, identical problem is already on Codeforces. https://mirror.codeforces.com/problemset/problem/526/F
.. and I know two solutions both of which are $$$O(N \ log \ N)$$$
The problem you mentioned is of 2900 rating and still its NlogN. I highly doubt google will ask such hard problems in phone interviews. As far as I know with my friends their questions revolves around div2 c,d and max e. mostly its c,d. Also in phone interviews their is a high change of geeting a seen leetcode problem.
about >= 2900 not appearing, you cannot be too sure, right? It's the Google! Considering the complexity of Code Jam, I doubt if their problems are mostly simple.
The only thing that shocks me is this kind of problem happens during a phone interview!!! Maybe he got on the interviewer's nerve, who knows :v !
Nope, he's right. Google interviews are lot easier than Code Jam problems. You can do a quick search for similar problems if you want. I'm not sure about problem ratings but it shouldn't be harder than Div1B in the worst case.
Source is da web, a few friends and my experience.
The $$$O(N\ log\ N)$$$ solution is considered 2900. The $$$O(N)$$$ solution could be considered a paper xD.
Who upvotes just by seeing red color?
This problem is one dimensional and the one in codeforces is 2 dimensional.
Anyway your $$$O(N log N)$$$ is like $$$O(sqrt(N) log (N))$$$ for this problem.
People didn't upvote because he's red. People upvote because he's correct.
A 2d NxN grid with N objects where no 2 objects share a row or column is the same thing as a 1d permutation.
Tip: someone with 800 rating points above you is likely to be correct, don't make such a bold claim without thinking.
My bad. I didn't read further after seeing NxN.
Apparently it is possible to do this in O(n):
Link to one Chinese blog post on this
Chinese slides on this
Can someone explain the main ideas in English?
So it's this all over again?
Can someone explain O(nlogn) approach for this problem?
Edit: The best explanation IMO : https://abitofcs.blogspot.com/2015/05/codeforces-526f-pudding-monsters.html