✅Beats 100% | Video | Explained Step by Step | List Most Common Array Interview Problems✅
https://www.youtube.com/watch?v=1pkOgXD63yU\n\n\n\n## Aproach 1: Dynamic Programming (All Beats 100%)\npython3 []\nclass Solution:\n    def maxProfit(self, pric
https://www.youtube.com/watch?v=1pkOgXD63yU\n\n\n\n## Aproach 1: Dynamic Programming (All Beats 100%)\n```python3 []\nclass Solution:\n    def maxProfit(self, prices: List[int]) -> int:\n        min_price = float(\'inf\')\n        max_profit = 0\n        for current_price in prices:\n            min_price = min(current_price, min_price)\n            max_profit = max(max_profit, current_price - min_price)   \n        return max_profit\n```\n```cpp []\nclass Solution {\npublic:\n    int maxProfit(vector<int>& prices) {\n        int minPrice = INT_MAX;\n        int maxProfit = 0;\n        \n        for (int currentPrice : prices) {\n            minPrice = min(currentPrice, minPrice);\n            maxProfit = max(maxProfit, currentPrice - minPrice);\n        }\n        \n        return maxProfit;\n    }\n};\n\n```\n```java []\nclass Solution {\n    public int maxProfit(int[] prices) {\n        int minPrice = Integer.MAX_VALUE;\n        int maxProfit = 0;\n        \n        for (int currentPrice : prices) {\n            minPrice = Math.min(currentPrice, minPrice);\n            maxProfit = Math.max(maxProfit, currentPrice - minPrice);\n        }\n        \n        return maxProfit;\n    }\n}\n```\n```csharp []\npublic class Solution {\n    public int MaxProfit(int[] prices) {\n        int minPrice = int.MaxValue;\n        int maxProfit = 0;\n        \n        foreach (int currentPrice in prices) {\n            minPrice = Math.Min(currentPrice, minPrice);\n            maxProfit = Math.Max(maxProfit, currentPrice - minPrice);\n        }\n        \n        return maxProfit;\n    }\n}\n```\n```golang []\nfunc maxProfit(prices []int) int {\n    minPrice := math.MaxInt32\n    maxProfit := 0\n    \n    for _, currentPrice := range prices {\n        minPrice = min(currentPrice, minPrice)\n        maxProfit = max(maxProfit, currentPrice - minPrice)\n    }\n    \n    return maxProfit\n}\n```\n```swift []\nclass Solution {\n    func maxProfit(_ prices: [Int]) -> Int {\n        var minPrice = Int.max\n        var maxProfit = 0\n        \n        for currentPrice in prices {\n            minPrice = min(currentPrice, minPrice)\n            maxProfit = max(maxProfit, currentPrice - minPrice)\n        }\n        \n        return maxProfit\n    }\n}\n```\n```javascript [JS]\n// JavaScript\n\nvar maxProfit = function(prices) {\n    let minPrice = Number.MAX_VALUE;\n    let maxProfit = 0;\n    \n    for (let currentPrice of prices) {\n        minPrice = Math.min(currentPrice, minPrice);\n        maxProfit = Math.max(maxProfit, currentPrice - minPrice);\n    }\n    \n    return maxProfit;\n};\n```\n```typescript [TS]\n// TypeScript\n\nfunction maxProfit(prices: number[]): number {\n    let minPrice = Number.MAX_VALUE;\n    let maxProfit = 0;\n    \n    for (const currentPrice of prices) {\n        minPrice = Math.min(currentPrice, minPrice);\n        maxProfit = Math.max(maxProfit, currentPrice - minPrice);\n    }\n    \n    return maxProfit;\n}\n```\n```rust []\nimpl Solution {\n    pub fn max_profit(prices: Vec<i32>) -> i32 {\n        let mut min_price = i32::MAX;\n        let mut max_profit = 0;\n        \n        for current_price in prices {\n            min_price = min_price.min(current_price);\n            max_profit = max_profit.max(current_price - min_price);\n        }\n        \n        return max_profit;\n    }\n}\n```\n```ruby []\ndef max_profit(prices)\n    min_price = Float::INFINITY\n    max_profit = 0\n    \n    prices.each do |current_price|\n        min_price = [current_price, min_price].min\n        max_profit = [max_profit, current_price - min_price].max\n    end\n    \n    max_profit\nend\n```\n- Complexity: O(n) & Space O(1)\n\n\n---\n\n\n## Aproach 2: Two Pointers\n\n\n```python3 []\nclass Solution:\n    def maxProfit(self, prices: List[int]) -> int:\n\n        if not prices or len(prices) < 2:\n            return 0\n            \n        max_profit = 0\n        left_buy = 0\n        right_sell = 1 #you can\'t buy later and sell earlier, unless you\'re doctor who\n        \n        while right_sell < len(prices):\n            current_price = prices[right_sell]\n            buy_price = prices[left_buy]\n            \n            if buy_price < current_price:\n                current_profit = current_price - buy_price\n                max_profit = max(max_profit, current_profit)\n            else:\n                left_buy = right_sell\n                \n            right_sell += 1\n            \n        return max_profit\n```\n```cpp []\nclass Solution {\npublic:\n    int maxProfit(vector<int>& prices) {\n        if (prices.empty() || prices.size() < 2) {\n            return 0;\n        }\n        \n        int maxProfit = 0;\n        int leftBuy = 0;\n        int rightSell = 1;\n        \n        while (rightSell < prices.size()) {\n            int currentPrice = prices[rightSell];\n            int buyPrice = prices[leftBuy];\n            \n            if (buyPrice < currentPrice) {\n                int currentProfit = currentPrice - buyPrice;\n                maxProfit = max(maxProfit, currentProfit);\n            } else {\n                leftBuy = rightSell;\n            }\n            \n            rightSell++;\n        }\n        \n        return maxProfit;\n    }\n};\n```\n```java []\nclass Solution {\n    public int maxProfit(int[] prices) {\n        if (prices == null || prices.length < 2) {\n            return 0;\n        }\n        \n        int maxProfit = 0;\n        int leftBuy = 0;\n        int rightSell = 1;\n        \n        while (rightSell < prices.length) {\n            int currentPrice = prices[rightSell];\n            int buyPrice = prices[leftBuy];\n            \n            if (buyPrice < currentPrice) {\n                int currentProfit = currentPrice - buyPrice;\n                maxProfit = Math.max(maxProfit, currentProfit);\n            } else {\n                leftBuy = rightSell;\n            }\n            \n            rightSell++;\n        }\n        \n        return maxProfit;\n    }\n}\n```\n```csharp []\npublic class Solution {\n    public int MaxProfit(int[] prices) {\n        if (prices == null || prices.Length < 2) {\n            return 0;\n        }\n        \n        int maxProfit = 0;\n        int leftBuy = 0;\n        int rightSell = 1;\n        \n        while (rightSell < prices.Length) {\n            int currentPrice = prices[rightSell];\n            int buyPrice = prices[leftBuy];\n            \n            if (buyPrice < currentPrice) {\n                int currentProfit = currentPrice - buyPrice;\n                maxProfit = Math.Max(maxProfit, currentProfit);\n            } else {\n                leftBuy = rightSell;\n            }\n            \n            rightSell++;\n        }\n        \n        return maxProfit;\n    }\n}\n```\n```golang []\nfunc maxProfit(prices []int) int {\n    if len(prices) < 2 {\n        return 0\n    }\n    \n    maxProfit := 0\n    leftBuy := 0\n    rightSell := 1\n    \n    for rightSell < len(prices) {\n        currentPrice := prices[rightSell]\n        buyPrice := prices[leftBuy]\n        \n        if buyPrice < currentPrice {\n            currentProfit := currentPrice - buyPrice\n            maxProfit = max(maxProfit, currentProfit)\n        } else {\n            leftBuy = rightSell\n        }\n        \n        rightSell++\n    }\n    \n    return maxProfit\n}\n\nfunc max(a, b int) int {\n    if a > b {\n        return a\n    }\n    return b\n}\n```\n```swift []\nclass Solution {\n    func maxProfit(_ prices: [Int]) -> Int {\n        guard prices.count >= 2 else {\n            return 0\n        }\n        \n        var maxProfit = 0\n        var leftBuy = 0\n        var rightSell = 1\n        \n        while rightSell < prices.count {\n            let currentPrice = prices[rightSell]\n            let buyPrice = prices[leftBuy]\n            \n            if buyPrice < currentPrice {\n                let currentProfit = currentPrice - buyPrice\n                maxProfit = max(maxProfit, currentProfit)\n            } else {\n                leftBuy = rightSell\n            }\n            \n            rightSell += 1\n        }\n        \n        return maxProfit\n    }\n}\n```\n```javascript [JS]\n// JavaScript\n\nvar maxProfit = function(prices) {\n    if (!prices || prices.length < 2) {\n        return 0;\n    }\n    \n    let maxProfit = 0;\n    let leftBuy = 0;\n    let rightSell = 1;\n    \n    while (rightSell < prices.length) {\n        const currentPrice = prices[rightSell];\n        const buyPrice = prices[leftBuy];\n        \n        if (buyPrice < currentPrice) {\n            const currentProfit = currentPrice - buyPrice;\n            maxProfit = Math.max(maxProfit, currentProfit);\n        } else {\n            leftBuy = rightSell;\n        }\n        \n        rightSell++;\n    }\n    \n    return maxProfit;\n};\n```\n```typescript [TS]\n// TypeScript\n\nfunction maxProfit(prices: number[]): number {\n    if (!prices || prices.length < 2) {\n        return 0;\n    }\n    \n    let maxProfit = 0;\n    let leftBuy = 0;\n    let rightSell = 1;\n    \n    while (rightSell < prices.length) {\n        const currentPrice = prices[rightSell];\n        const buyPrice = prices[leftBuy];\n        \n        if (buyPrice < currentPrice) {\n            const currentProfit = currentPrice - buyPrice;\n            maxProfit = Math.max(maxProfit, currentProfit);\n        } else {\n            leftBuy = rightSell;\n        }\n        \n        rightSell++;\n    }\n    \n    return maxProfit;\n}\n```\n```rust []\nimpl Solution {\n    pub fn max_profit(prices: Vec<i32>) -> i32 {\n        if prices.len() < 2 {\n            return 0;\n        }\n        \n        let mut max_profit = 0;\n        let mut left_buy = 0;\n        let mut right_sell = 1;\n        \n        while right_sell < prices.len() {\n            let current_price = prices[right_sell];\n            let buy_price = prices[left_buy];\n            \n            if buy_price < current_price {\n                let current_profit = current_price - buy_price;\n                max_profit = max_profit.max(current_profit);\n            } else {\n                left_buy = right_sell;\n            }\n            \n            right_sell += 1;\n        }\n        \n        max_profit\n    }\n}\n```\n```ruby []\ndef max_profit(prices)\n    return 0 if prices.nil? || prices.length < 2\n    \n    max_profit = 0\n    left_buy = 0\n    right_sell = 1\n    \n    while right_sell < prices.length\n        current_price = prices[right_sell]\n        buy_price = prices[left_buy]\n        \n        if buy_price < current_price\n            current_profit = current_price - buy_price\n            max_profit = [max_profit, current_profit].max\n        else\n            left_buy = right_sell\n        end\n        \n        right_sell += 1\n    end\n    \n    max_profit\nend\n```\n\n\n\n- Complexity: O(n) & Space O(1)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n# Explanation\n\n\n \n\n\n---\n\n- The video explains basically everything.\nbut I can explain the differences in the Dynamic Programming approach and the details of the code. \n\n## Aproach 1: Dynamic Programming\n\n1. We need two variables:\n\n- `min_price`: Tracks the lowest price seen so far\n- `max_profit`: Tracks the maximum profit possible\n2. Plan is simple:\n\n- Initialize `min_price` to infinity to ensure the first price will become the minimum\n- Initialize `max_profit` to **0** (in case no profit is possible)\n3. For each price in the array:\n\n- Update `min_price` if we find a lower price\n- Calculate potential profit by subtracting `min_price` from current price\n- Update `max_profit` if this potential profit is higher than previous `max_profit` \n\n## Probably not understand code\n\n## #1 - `min_price = min(current_price, min_price)`\n\nKeeps track of the lowest stock price seen so far by:\n\n1. Comparing two values:\n\n`current_price`: The current stock price being examined\n`min_price`: The lowest price we\'ve seen up to this point\n2. The `min()` function returns the smaller of these two values\n\n### Example\n\n```\n# If current price is $5 and previous min_price was $7\nmin_price = min(5, 7)  # Returns 5\n\n# If current price is $8 and previous min_price was $5\nmin_price = min(8, 5)  # Keeps 5\n```\n## #2 - `max_profit = max(max_profit, current_price - min_price)`\n\n1. Computing potential profit at current position:\n\n- `current_price` - `min_price`: Represents profit if we sell at current price\n- Example: If `current_price` is $10 and `min_price` is $4, potential profit is $6\n2. Comparing two values using max():\n\n- `max_profit`: Best profit found so far\n- `current_price` - `min_price`: New potential profit\n\n### Example\n\n```\n# Scenario 1: Current profit calculation is better\ncurrent_price = 10\nmin_price = 4\nmax_profit = 5\nmax_profit = max(5, 10 - 4)  # max(5, 6) = 6\n\n# Scenario 2: Previous profit was better\ncurrent_price = 8\nmin_price = 4\nmax_profit = 6\nmax_profit = max(6, 8 - 4)  # max(6, 4) = 6\n```\n\n\n## Pseudo Code\n```java [Dynamic Programming]\nAlgorithm MaxProfit(prices)\n    // Initialize variables\n    min_price = INFINITY\n    max_profit = 0\n\n    // Iterate through each price\n    for each current_price in prices do\n        // Update minimum price seen so far\n        min_price = MINIMUM(current_price, min_price)\n        \n        // Calculate potential profit and update max profit if higher\n        current_profit = current_price - min_price\n        max_profit = MAXIMUM(max_profit, current_profit)\n    end for\n\n    return max_profit\nEnd Algorithm\n// Complexy: Time O(n) Space O(1)\n```\n```java [Two Pointers]\nAlgorithm MaxProfit(prices)\n    if prices is empty OR length of prices < 2 then\n        return 0\n    end if\n\n    // Initialize variables\n    max_profit = 0\n    left_buy = 0          // Buy pointer\n    right_sell = 1        // Sell pointer\n\n    // Continue while we have prices to compare\n    while right_sell < length of prices do\n        current_price = prices[right_sell]\n        buy_price = prices[left_buy]\n\n        // If profitable trade found\n        if buy_price < current_price then\n            current_profit = current_price - buy_price\n            max_profit = MAXIMUM(max_profit, current_profit)\n        else\n            // Update buy pointer if we find a lower price\n            left_buy = right_sell\n        end if\n\n        // Move sell pointer to next day\n        right_sell = right_sell + 1\n    end while\n\n    return max_profit\nEnd Algorithm\n// Complexy: Time O(n) Space O(1)\n```\n```java [Brute Force]\nAlgorithm MaxProfit(prices)\n    // Initialize maximum profit\n    max_profit = 0\n\n    // Outer loop - try each buying position\n    for i = 0 to length of prices - 1 do\n        buy_price = prices[i]\n        \n        // Inner loop - try each selling position after buy\n        for j = i + 1 to length of prices - 1 do\n            sell_price = prices[j]\n            \n            // Update maximum profit if better profit found\n            current_profit = sell_price - buy_price\n            max_profit = MAXIMUM(max_profit, current_profit)\n        end for\n    end for\n\n    return max_profit\nEnd Algorithm\n// Complexy: Time O(n^2)I know.. right? Space O(1)\n```\n\n\n\n\nDo you like my translation style? I have more solutions, most common problems. List below.\n\nIf you want you can give UpVote, then more people will see this solution and help them too. \n\nThe button is in the bottom left corner of the screen, I didn\'t see it at first either :)\n![image.png](https://assets.leetcode.com/users/images/2022260e-1830-4056-b086-e8fc852e7593_1731243099.7756999.png)\n\n---\n\n\n\n![array2.png](https://assets.leetcode.com/users/images/7a0a6937-9d07-4cf6-ad93-d27ffcaf69a0_1730577425.4474297.png)\n\n# Most common Array Interview Problems\nList based on my research (interviews, reddit, github) Probably not 100% accurate but very close to my recruitments. Leetcode Premium is probably more accurate [I don\'t have]\n\n\n**Easy:** [[1. Two Sum]](https://leetcode.com/problems/two-sum/solutions/5999466/beats-100-explained-step-by-step-list-most-common-array-inverview) **(common warm-up)** [[121. Best Time to Buy and Sell Stock]](https://leetcode.com/discuss/topic/6027720/solution/) [[88. Merge Sorted Array]](https://leetcode.com/problems/merge-sorted-array/solutions/6031394/beats-100-explained-step-by-step-list-most-common-string-interview-problems) [[27. Remove Element]](https://leetcode.com/problems/remove-element/solutions/6031528/beats-100-explained-step-by-step-list-most-common-array-interview-problems) [[929. Unique Email Addresses]](https://leetcode.com/problems/unique-email-addresses/solutions/6031687/explained-step-by-step-video-list-most-common-array-interview-problems) \n\n\n**Medium/Hard:** [[15. 3Sum]](https://leetcode.com/problems/3sum/description/) [[42. Trapping Rain Water]](https://leetcode.com/problems/trapping-rain-water/?envType=problem-list-v2&envId=atbyk0mj) [[56. Merge Intervals]](https://leetcode.com/problems/merge-intervals/description/) [[57. Insert Interval]](https://leetcode.com/problems/insert-interval/description/) [[46. Permutations]](https://leetcode.com/problems/permutations/description/) [[53. Maximum Subarray]](https://leetcode.com/problems/maximum-subarray/description/) [[73. Set Matrix Zeroes]](https://leetcode.com/problems/set-matrix-zeroes/description/) [[284. Peeking Iterator]](https://leetcode.com/problems/peeking-iterator/description/)\n\n\n\n\n\n\n\n\n\n\n\n\n\n####  [Interview Questions and Answers Repository](https://github.com/RooTinfinite/Interview-questions-and-answers)\n\n\n![image.png](https://assets.leetcode.com/users/images/9dc1b265-b175-4bf4-bc6c-4b188cb79220_1728176037.4402142.png)\n![image.png](https://assets.leetcode.com/users/images/e968c2a2-ff7e-48ec-8f73-7bb1e27d9d2a_1731246003.4436166.png)\n\n
['Swift', 'C++', 'Java', 'Go', 'TypeScript', 'Python3', 'Rust', 'Ruby', 'JavaScript', 'C#']