thảo luận Leetcode contest, đường tới Guardian

  • Người tạo chủ đề Người tạo chủ đề freedom.9
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
bài 4 cắm đầu sweep line hoá ra sai :too_sad: chắc phải dùng segment tree 2D hay gì đó để count số lượng point trong rec nhanh
 
bài 4 cắm đầu sweep line hoá ra sai :too_sad: chắc phải dùng segment tree 2D hay gì đó để count số lượng point trong rec nhanh
sweep line x rồi count y bằng ordered_multiset

C++:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
class Solution {
public:
    long long maxRectangleArea(vector<int>& xCoord, vector<int>& yCoord) {
        int n = xCoord.size();
        vector<pair<int, int>> points(n);
       
        for (int i = 0; i < n; i++) {
            points[i] = {xCoord[i], yCoord[i]};
        }
        sort(points.begin(), points.end());

        map<int, vector<int>> x_to_y;
        for (auto p : points) {
            x_to_y[p.first].push_back(p.second);
        }
        for (auto& entry : x_to_y) {
            sort(entry.second.begin(), entry.second.end());
        }
        long long res = -1;
        map<string, int> y_sum;
        unordered_map<string,int>m;
        ordered_multiset s;
        for (auto& [x1, yList] : x_to_y) {
            int y_count = yList.size();
            for (int i = 1; i < y_count; ++i) {
                string z= to_string(yList[i])+","+to_string(yList[i-1]);
                string z1= to_string(x1)+" "+to_string(yList[i])+","+to_string(yList[i-1]);
                int le = s.order_of_key(yList[i-1]);
                int lee = s.order_of_key(yList[i] + 1);
                y_sum[z1]=lee-le;
                if(m.find(z)!=m.end()){
                    int u=m[z];
                    string z2=to_string(u)+" "+z;
                    // cout<<z1<<" "<<z2<<"\n";
                    // cout<<y_sum[z1]<<" "<<y_sum[z2]<<"\n";
                    if(y_sum[z1]-y_sum[z2]-2==0){
                        res = max(res, abs(static_cast<long long>(u - x1)) * abs(static_cast<long long>(yList[i] - yList[i - 1])));
                    }
                }
                m[z]=x1;
            }
           
            for (int i = 0; i < y_count; ++i) {
                s.insert(yList[i]);
            }      
        }

        return res == -1 ? -1 : res;
    }
};
©leetcode
 
sweep line x rồi count y bằng ordered_multiset

C++:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
class Solution {
public:
    long long maxRectangleArea(vector<int>& xCoord, vector<int>& yCoord) {
        int n = xCoord.size();
        vector<pair<int, int>> points(n);
      
        for (int i = 0; i < n; i++) {
            points[i] = {xCoord[i], yCoord[i]};
        }
        sort(points.begin(), points.end());

        map<int, vector<int>> x_to_y;
        for (auto p : points) {
            x_to_y[p.first].push_back(p.second);
        }
        for (auto& entry : x_to_y) {
            sort(entry.second.begin(), entry.second.end());
        }
        long long res = -1;
        map<string, int> y_sum;
        unordered_map<string,int>m;
        ordered_multiset s;
        for (auto& [x1, yList] : x_to_y) {
            int y_count = yList.size();
            for (int i = 1; i < y_count; ++i) {
                string z= to_string(yList[i])+","+to_string(yList[i-1]);
                string z1= to_string(x1)+" "+to_string(yList[i])+","+to_string(yList[i-1]);
                int le = s.order_of_key(yList[i-1]);
                int lee = s.order_of_key(yList[i] + 1);
                y_sum[z1]=lee-le;
                if(m.find(z)!=m.end()){
                    int u=m[z];
                    string z2=to_string(u)+" "+z;
                    // cout<<z1<<" "<<z2<<"\n";
                    // cout<<y_sum[z1]<<" "<<y_sum[z2]<<"\n";
                    if(y_sum[z1]-y_sum[z2]-2==0){
                        res = max(res, abs(static_cast<long long>(u - x1)) * abs(static_cast<long long>(yList[i] - yList[i - 1])));
                    }
                }
                m[z]=x1;
            }
          
            for (int i = 0; i < y_count; ++i) {
                s.insert(yList[i]);
            }     
        }

        return res == -1 ? -1 : res;
    }
};
©leetcode
Bác khủng quá, multiset là cái gì nghe lạ quá, lại có cái mới phải học rồi :adore:
 
1733631549046.png

Nay ko làm, may quá chắc ko lại 3Q gang rồi =((
Làm 3Q nhanh cũng húp được mà :hungry:
Vượt mức 2k3 rồi, có nên dừng ở đây để tiếng thơm còn mãi hay làm tiếp có khi về lại 2k :adore:
 
Toán thì ko ngán, học dần là được, mà xài python ngán quá TLE tùm lum, dfs cũng éo được toàn run time error nản thật. Giờ muốn luyện lại đi học lại c++ mệt vãi :canny:

via theNEXTvoz for iPhone
 
Toán thì ko ngán, học dần là được, mà xài python ngán quá TLE tùm lum, dfs cũng éo được toàn run time error nản thật. Giờ muốn luyện lại đi học lại c++ mệt vãi :canny:

via theNEXTvoz for iPhone
Lên 3k leetcode, 2k2 CF xong học C++ cũng được bác :big_smile: , ô nhà vật lý vẫn python

Xem tệp đính kèm 2823114
Ngôn ngữ không phế, người dùng ngôn ngữ không đúng mới phế, :doubt:
 
ordered_set đây mà các bác code py có sorted_dict/list mà học cái này làm gì :v
này cũng là set thường thôi, mà set thường bên c++ ko có đếm bao nhiều phần tử <= x (chỉ có đếm bằng o(n)) mới vẽ ra cái này
C++ trên leetcode dùng hàm gì để in ra debug vậy bác, hàm print của python nó chơi được hết loại data mà C++ có vẻ ko có :sweat:
 
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
freedom.9,
Người trả lời cuối
freedom.9,
Trả lời
2.480
Lượt xem
130.132
Quay lại
Lên đầu trang