thảo luận Leetcode mỗi ngày

  • Người tạo chủ đề Người tạo chủ đề _Gia_Cat_Luong_
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
Học @MasonMaoSuVuong à
osCpCsi.png
code chay đi cho quen
học cái hay cái tốt chứ có học cọp sol đâu mà phải run hả a
u40wsAh.png
 
Python:
class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        maxSofar = -inf
        count = 0
        ans = 0
        for num in nums:
            if num > maxSofar:
                ans = 1
                maxSofar = num
                count = 1
            elif num == maxSofar:
                count += 1
                ans = max(ans, count)
            else:
                count = 0
        return ans
Mấy bài nay dễ quá, hôm nay tôi sẽ giới thiệu cho anh em 1 thuật toán là sweep line.
Bài toán là, có 1 cái nhà hàng và list của people, với mỗi ith people thì [start, end] là thời điểm mà người này vào nhà hàng hoặc rời nhà hàng, tính xem có tối đa bao nhiêu người ở trong 1 nhà hàng ở 1 đơn vị thời gian.
Làm xong 2 bài cơm thêm này sẽ học được thuật toán sweep line, thêm kiến thức mới vào đầu phá đảo interview :doubt:

Java:
class Solution {
    public int[] fullBloomFlowers(int[][] flowers, int[] people) {
        int n = people.length;
        int[][] ordered_people = new int[n][3];
        for (int i = 0; i < n; i++) {
            ordered_people[i][0] = people[i];
            ordered_people[i][1] = i;
        }

        Arrays.sort(ordered_people, (a, b) -> {
            return a[0] - b[0];
        });
        PriorityQueue<Integer> start_bloom = new PriorityQueue<>();
        PriorityQueue<Integer> end_bloom = new PriorityQueue<>();

        for (int[] flower : flowers) {
            start_bloom.offer(flower[0]);
            end_bloom.offer(flower[1]);
        }

        int current_blooms = 0;
        int start = 0;
        int end = end_bloom.poll();
        int i = 0;
     
        while (i < n) {
            if (!start_bloom.isEmpty()) {
                start = start_bloom.poll();
                current_blooms++;
                while (i < n && ordered_people[i][0] < start  ) {
                    while (end < ordered_people[i][0]) {
                        current_blooms--;
                        end = end_bloom.poll();
                    }
                    ordered_people[i][2] = current_blooms - 1;
                    i++;
                }
                while (end < start) {
                    current_blooms--;
                    end = end_bloom.poll();
                }

            } else {
             
                if (!end_bloom.isEmpty()) {
                 
                    if (end < ordered_people[i][0]) {
                        current_blooms--;
                        end = end_bloom.poll();
                    } else {
                        ordered_people[i][2] = current_blooms;
                        i++;
                    }
                } else {
                    while (i<n && end >= ordered_people[i][0] ) {
                        ordered_people[i][2] = current_blooms;
                        i++;
                    }
                    while (i < n) {
                        ordered_people[i][2] = 0;
                        i++;
                    }
                }
            }
        }
        Arrays.sort(ordered_people, (a, b) -> {
            return a[1] - b[1];
        });
        int[] res = new int[n];
        for (int j = 0; j < n; j++) {
            res[j] = ordered_people[j][2];
        }
        return res;
    }
}
a1ySSL9.gif
brute force + if else từng interval 1 , làm xong phải đi tắm để gột rửa tội lỗi này
 
Sửa lần cuối:
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
_Gia_Cat_Luong_,
Người trả lời cuối
Vipluckystar,
Trả lời
17.755
Lượt xem
1.215.683
Quay lại
Lên đầu trang