{"id":22513947,"url":"https://github.com/emahtab/container-with-most-water","last_synced_at":"2026-01-06T17:36:40.918Z","repository":{"id":79525371,"uuid":"230047281","full_name":"eMahtab/container-with-most-water","owner":"eMahtab","description":"Container with most water problem","archived":false,"fork":false,"pushed_at":"2020-04-01T07:14:27.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:26:11.345Z","etag":null,"topics":["container-with-most-water","leetcode","leetcode-java","problem-solving","two-pointers"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-25T05:36:11.000Z","updated_at":"2020-04-01T07:14:29.000Z","dependencies_parsed_at":"2023-05-10T17:15:46.520Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/container-with-most-water","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcontainer-with-most-water","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcontainer-with-most-water/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcontainer-with-most-water/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcontainer-with-most-water/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/container-with-most-water/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952261,"owners_count":20699457,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["container-with-most-water","leetcode","leetcode-java","problem-solving","two-pointers"],"created_at":"2024-12-07T03:15:13.442Z","updated_at":"2026-01-06T17:36:40.869Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Container with most water\n## https://leetcode.com/problems/container-with-most-water\n\nGiven n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.\n\nNote: You may not slant the container and n is at least 2.\n\n![Area with most water](example.jpg?raw=true \"Title\")\n\nThe above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.\n\n**This a beautiful question showing, how to use the two pointer approach for better runtime**\n\n### Implementation 1 : O(n^2)\n\n```java\nclass Solution {\n    public int maxArea(int[] height) {\n        if(height == null || height.length == 0)\n            return 0;\n        int mostWater = Integer.MIN_VALUE;\n        for(int i = 0; i \u003c height.length - 1; i++) {\n            for(int j = i + 1; j \u003c height.length; j++) {\n                mostWater = Math.max(mostWater, Math.min(height[i], height[j]) * (j-i));\n            }\n        }\n        return mostWater;\n    }\n}\n```\nAbove implementation have runtime complexity of O(n^2) and space complexity of O(1)\n\n```\nRuntime Complexity = O(n^2)\nSpace Complexity   = O(1)\n```\n\n### Implementation 2 : O(n)\n\n```java\nclass Solution {\n    public int maxArea(int[] height) {\n        if(height == null || height.length == 0)\n            return 0;\n        int left = 0;\n        int right = height.length - 1;\n        int mostWater = Integer.MIN_VALUE;\n        while(left \u003c right) {\n            int distance = right - left;\n            int waterStored = Math.min(height[left], height[right]) * distance;\n            mostWater = Math.max(mostWater, waterStored);\n            if(height[left] \u003c= height[right]) {\n                left++;\n            } else {\n                right--;\n            }\n        }\n        return mostWater;\n    }\n}\n```\n\nAbove implementation have runtime complexity of O(n) and space complexity of O(1)\n\n```\nRuntime Complexity = O(n)\nSpace Complexity   = O(1)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcontainer-with-most-water","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fcontainer-with-most-water","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcontainer-with-most-water/lists"}