Войти в систему

Home
    - Создать дневник
    - Написать в дневник
       - Подробный режим

LJ.Rossia.org
    - Новости сайта
    - Общие настройки
    - Sitemap
    - Оплата
    - ljr-fif

Редактировать...
    - Настройки
    - Список друзей
    - Дневник
    - Картинки
    - Пароль
    - Вид дневника

Сообщества

Настроить S2

Помощь
    - Забыли пароль?
    - FAQ
    - Тех. поддержка



Пишет nancygold ([info]nancygold)
@ 2024-05-05 21:30:00


Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Настроение: amused
Entry tags:computing

Any sub O(n^2) soluton?
Latest FizzBuzz:
https://leetcode.com/problems/the-skyline-problem/

can you find an O(n*log2(n)) solution?

My O(n^2) solution (inner for loop gives the square)

  Input: [2 9 10] [3 7 15] [5 12 12] [15 20 10] [19 24 8]
  Output:
  Open!
  CurId MaxY -1
  for X,Y,Id,End Input.i{[@?1 ?0]}{[?0 ?2 ?3 0],[?1 ?2 ?3 1]}.j.sortBy(?0):
    less End:
      Open.Id = Y
      if MaxY < Y:
        [Id Y](:&CurId,&MaxY)
        push X,Y Output
      pass
    Open.del Id
    less Id><CurId: pass
    [-1 -1](:&CurId,&MaxY)
    less Open.n: | push X,0 Output; pass
    Open.l.maxf(?1)(:[&CurId &MaxY])
    push X,MaxY Output

  say Output.f





(Добавить комментарий)


(Анонимно)
2024-05-06 17:21 (ссылка)
я русский дебил, я умею только пить водку, колоться солями и нюхать клей

(Ответить)


(Анонимно)
2024-05-06 17:54 (ссылка)
The Skyline Problem on LeetCode is a classic problem involving skyline generation given a list of buildings' heights and positions. The typical approach involves using a data structure like a heap or a priority queue to efficiently track the heights of the buildings at various positions.


The solution complexity for the skyline problem can be broken down as follows:

1. Sorting: Sorting the building endpoints based on their x-coordinates takes O(n log n) time, where n is the number of buildings.

2. Building the skyline: As we iterate through the sorted building endpoints, we maintain a priority queue (heap) to keep track of the heights of the buildings. Each building endpoint (start or end) is processed once, resulting in a total of 2n operations. Insertion and deletion in the priority queue take O(log n) time. Therefore, the overall time complexity for building the skyline is O(n log n).

3. Constructing the output: Constructing the final skyline involves identifying the critical points where the height changes. This step involves traversing the sorted list of critical points, which takes O(n) time.

Overall, the time complexity is dominated by the sorting step, resulting in O(n log n) complexity.

(Ответить) (Ветвь дискуссии)


[info]nancygold
2024-05-06 23:19 (ссылка)
Yup. Using heap in place of a hashtable is the correct soluton

(Ответить) (Уровень выше)


(Анонимно)
2024-05-06 17:55 (ссылка)
To solve the "Skyline Problem" on LeetCode, you can follow these steps:

1. **Extract Events**: Convert the buildings' left and right edges into "events" containing their x-coordinates and heights. Each left edge will have a negative height to indicate the start of a building, and each right edge will have a positive height to indicate the end of a building.

2. **Sort Events**: Sort the events based on their x-coordinates. If two events have the same x-coordinate, prioritize the one with a lower height. This step ensures that we process the events in order from left to right.

3. **Process Events**: Iterate through the sorted events. Maintain a data structure (e.g., a heap or sorted array) to keep track of the current maximum height of active buildings. As you encounter each event:

- If it's the start of a building (negative height), add its height to the active set.
- If it's the end of a building (positive height), remove its height from the active set.
- Update the result whenever the maximum height changes.

4. **Generate Output**: Convert the result into the final skyline outline format. This typically involves removing redundant points (e.g., consecutive points with the same height) and formatting the output according to the problem's requirements.

By following these steps, you can efficiently find the skyline outline of the given buildings. You can implement this algorithm in various programming languages such as Python, C++, or JavaScript.

(Ответить)


(Анонимно)
2024-05-06 17:56 (ссылка)
Here's a Python solution to solve the problem "The Skyline Problem" on LeetCode:

```python
import heapq

def getSkyline(buildings):
events = [(L, -H, R) for L, R, H in buildings]
events += list({(R, 0, 0) for _, R, _ in buildings})
events.sort()
res = [[0, 0]]
hp = [(0, float("inf"))]
for L, negH, R in events:
while L >= hp[0][1]:
heapq.heappop(hp)
if negH:
heapq.heappush(hp, (negH, R))
if res[-1][1] + hp[0][0]:
res.append([L, -hp[0][0]])
return res[1:]

# Example usage:
buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
print(getSkyline(buildings))
```

This function takes a list of buildings represented as triplets `[left, right, height]` and returns the skyline outline. You can test it with the example provided or with your own inputs.

(Ответить)