-2

I'm doing a leetcode problem about squares intersecting. Here's how I was able to figure it out in Python, but how would I write it in C++?

def intersections(rects):
    events = []
    for A in rects:
        events.append((A.x, 'insert', A.y, A.Y))
        events.append((A.X, 'remove', A.y, A.Y))
    intersections = 0
    ys = SortedList()
    Ys = SortedList()
    for x, op, y, Y in sorted(events):
        if op == 'insert':
            intersections += ys.bisect_right(Y) - Ys.bisect_left(y)
            ys.add(y)
            Ys.add(Y)
        else:
            ys.remove(y)
            Ys.remove(Y)
    return intersections
khelwood
  • 55,782
  • 14
  • 81
  • 108
wasi hassan
  • 63
  • 10
  • 4
    Direct translation between languages (doesn't matter if it's programming, written or spoken) almost never turn out well. Instead take the language-agnostic algorithm and implement it in the new language. – Some programmer dude Apr 07 '22 at 08:55
  • 1
    What is `SortedList`? It's not defined here. Anyway, don't just ask us to convert the code to C++, you won't learn anything that way. Try it yourself first. – Jussi Nurminen Apr 07 '22 at 08:56
  • Not sure as to why there are downvotes; particularly on the question – DanielBell99 Apr 07 '22 at 08:57
  • 2
    Moreover, you can't learn C++ by trying to solve problems and trying to figure out/guess how it works. You must pick a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) of your choice and start with that :) – Fareanor Apr 07 '22 at 08:58
  • And please take some time to refresh [the help pages](http://stackoverflow.com/help), especially ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask) – Some programmer dude Apr 07 '22 at 08:59
  • @Fareanor If you're already experienced w/ programming but in a different language, looking at what the equivalents are is where everyone starts. This question is like Day 0 discovery, with an aim to get a feel for things. An already experienced programmer of course knows that a book and courses is where learning actually begins. – DanielBell99 Apr 07 '22 at 09:03
  • @StressedBoi_69420 You still have to have a good reference to learn C++, because it isn't amenable to "try it and see", undefined behaviour means you can't generalise what you see when you try. – Caleth Apr 07 '22 at 09:05
  • 1
    @StressedBoi_69420 _"looking at what the equivalents are is where everyone starts"_ No it is certainly not ! If languages were just a matter of literal translation, there would not be several languages. Each language have its own paradigms, rules, etc... Direct translation from one to another is the best way to write wrong code. The good way to do this is to extract the algorithm/idea/process from the original code (with your paper and your pen) and then reimplement it in the new language taking into account the new language rules/paradigms, etc... – Fareanor Apr 07 '22 at 09:06
  • @Fareanor Yes, direct translation isn't a good idea. Looking at what the alternative code semantics are is exactly what programmers would intuitively do on their first look into another language - not to learn but to start to get an understanding for it... – DanielBell99 Apr 07 '22 at 09:10
  • _"exactly what programmers would intuitively do"_ I strongly disagree with that. I learned several languages, and for each, I started from the languages bases, not from some other language I already know, exactly for the reasons mentioned above. And I know no programmer that have learned the way you suggests. Each language do things its own way, each language have its strengths and weaknesses, hence why literal equivalents are almost always a terribly bad idea. – Fareanor Apr 07 '22 at 09:14
  • *I know `print()`. What does C++ use? `cout <<`*. is exactly how many take their first steps into a new language. – DanielBell99 Apr 07 '22 at 09:19
  • @Fareanor As said, an already experienced programmer will already know of book and courses... This isn't for his "learning" per se, but rather a first dip. – DanielBell99 Apr 07 '22 at 09:21

1 Answers1

-3

I haven't touched C++ in some time... So this is merely an attempt to start with.

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct Rect {
    int x, X, y, Y;
};

int intersections(vector<Rect> &rects) {
    vector<pair<int, pair<char, pair<int, int>>>> events;
    for (auto &A : rects) {
        events.emplace_back(A.x, make_pair('i', make_pair(A.y, A.Y)));
        events.emplace_back(A.X, make_pair('r', make_pair(A.y, A.Y)));
    }
    sort(events.begin(), events.end());
    int intersections = 0, n = events.size();
    vector<int> ys, Ys;
    for (int i = 0; i < n; i++) {
        int x = events[i].first, y = events[i].second.second.first, Y = events[i].second.second.second;
        if (events[i].second.first == 'i') {
            intersections += upper_bound(ys.begin(), ys.end(), Y) - lower_bound(ys.begin(), ys.end(), y);
            ys.push_back(y);
            Ys.push_back(Y);
        } else {
            ys.erase(lower_bound(ys.begin(), ys.end(), y));
            Ys.erase(lower_bound(Ys.begin(), Ys.end(), Y));
        }
    }
    return intersections;
}

int main()
DanielBell99
  • 896
  • 5
  • 25
  • 57
  • 1
    Code-only answers tend to promote [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming), which isn't a good thing. Please take some time to read about [how to write good answers](https://stackoverflow.com/help/how-to-answer). And make sure the question is good enough to even warrant an answer (Stack Overflow isn't a translation service). – Some programmer dude Apr 07 '22 at 08:56
  • I do get that, but it is clear what is really being asked: *what are the C++ equivalents to work with?* An example of a language you're already used to and the other, that carry out the same process, is definitely a good start. I'm not 100% certain that my solution works, but it's definitely of some help to provide at least this. – DanielBell99 Apr 07 '22 at 09:00
  • 2
    Also: `vector>>>`? That's a disgusting type – Caleth Apr 07 '22 at 09:08
  • @Caleth Charming – DanielBell99 Apr 07 '22 at 09:11
  • Yes. A more direct (but still smelly) translation would be `std::vector>`, with `for (auto [x, op, y, Y] : events)` – Caleth Apr 07 '22 at 09:13