Leetcode Day 4
November, 20 2021

Two Sum

Attempt to recall: Question is “Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.”

Intersection of Two Arrays

vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ans;
        vector<int>::iterator it;
        for(int i = 0; i < nums1.size(); i++){
            it = find(nums2.begin(),nums2.end(),nums1[i]);
            if( it != nums2.end() ){
                ans.push_back(nums1[i]);
                *it = -1;
            }
        }
        return ans;
    }
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
    map<int,int> m;
            vector<int> R;
            for(auto n:nums1) m[n]++;
            // for(const auto& elem : m){
            //     std::cout << elem.first<< " " 
            //       << elem.second << "\n";
            // }
            for(auto n:nums2){
                if( --m[n] >= 0 ){
                    R.push_back(n);
                    
                }
            }
            return R;
}

Best Time to Buy and Sell Stock

int maxProfit(vector<int>& prices) {
        int index = 0;
        int smallest = INT_MAX;
        int maxprofit = INT_MIN;
        if( prices.size() == 1){
            return 0;
        }
        for(int i = 0; i < prices.size(); i++){
            if(prices[i] < smallest){
                smallest = prices[i];
            }
            if( prices[i] - smallest > maxprofit ) {
                maxprofit = prices[i] - smallest;
            }
        }
        return maxprofit;
    }
Melton Zheng
November 20 2021
Load comments 

My Calender III Question states to create a class called my calendar III which has a function which adds bookings to the calendar. When you add a booking with a start and end time, which are integers, you have to return the max number of intersections between the bookings for the calender. Ex. Let’s say the bookings are as follows: [10, 20] [15, 30] [40, 50] [3, 18] The first...
read more

Oct 2022
next

Okay, so I tried to take notes but it was so boring. I’d rather do questions. There’s this thing called study plan on leetcode which I’m prolly just gonna follow. I clicked on all three study plans: algorithms and dynamic programming (DP). And lemme just say I kinda struggled with the two already for data structures. I started at around 12:30 PM and now it’s 2:06 PM. Contains Duplicate The...
read more

Nov 2021
prev
Popular
Years