Trapping Rain Water

July 29, 2020

Trapping rain water ilk bakışta görmek zor ancak şu şekilde düşünülebilir.

Engel uzunluklarını B dizisi ile ifade edelim. Herhangi bir i noktasındaki biriken su miktarına X(i) dersek

$$ X(i) = Min( max( B[k] k > i) , max(B[k] k < i ))) - B[i] $$

Bir noktanın sağındaki ve solundaki en uzun engelleri önceden hesaplarsak biz O(n) zamanlı çözümü verir.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
int trap(vector<int>& arr) {
    int fw = 0, bw = 0, sum = 0, len = arr.size();
    int forward[len], backward[len];

    for (int i = 0; i < len; i++){
        // noktanın solundaki en uzun engel uzunluğu
        fw = max(arr[i], fw), forward[i] = fw;
        // noktanın sağındaki en uzun engel uzunluğu
        bw = max(arr[len - i - 1], bw), backward[len - i - 1] = bw;
    }

    for (int i = 0; i < len; i++) 
        sum += min(forward[i], backward[i]) - arr[i];

    return sum;
}

hidden

AI: Where in the Loop Should Humans Go?

This is a re-publishing of a blog post I originally wrote for work, but wanted on my own blog as well.AI is everywhere, and its impressive claims are leading to rapid adoption. At this stage, I’d qualify it as charismatic technology—someth…

via Ferd.ca

More Good Programming Quotes, Part 6

Here are more good programming quotes I have found since my last post. Programming “Configuration is coding in a poorly designed programming language without tests, version control, or documentation.”Gregor Hohpe “It’s the developers misunderstanding, not…

via Henrik Warne's blog

HOWTO: Change your behavior

In theory, behavior change should be easy. At first glance, it seems like you control your behavior. So, if you desire different behavior, why doesn’t your behavior change as instantly as your desire to change it? In short, lasting change of habitual behavio…

via Matt Might's blog