question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
number-of-good-pairs | Easy Solution | easy-solution-by-deepakumar-developer-pn1o | Dart []\nclass Solution {\n int numIdenticalPairs(List<int> nums) {\n int output=0;\n for(var i=0;i<nums.length-1;i++){\n for(var j=i+1;j<nums.len | deepakumar-developer | NORMAL | 2023-12-29T18:02:49.841477+00:00 | 2023-12-29T18:22:07.325894+00:00 | 427 | false | ```Dart []\nclass Solution {\n int numIdenticalPairs(List<int> nums) {\n int output=0;\n for(var i=0;i<nums.length-1;i++){\n for(var j=i+1;j<nums.length;j++){\n if(nums[i] == nums[j] && i < j){\n output++;\n }\n }\n }\n return output;\n }\n}\n```\n```C++ ... | 9 | 0 | ['Python', 'C++', 'Java', 'Python3', 'Dart'] | 1 |
number-of-good-pairs | ✅88.44%🔥Easy Solution🔥Array & HashTable | 8844easy-solutionarray-hashtable-by-pych-m31y | C++ []\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& A) {\n int ans = 0;\n unordered_map<int, int> cnt;\n for (int | pycharm82 | NORMAL | 2023-10-04T10:44:10.617765+00:00 | 2023-10-04T10:44:10.617786+00:00 | 285 | false | ```C++ []\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& A) {\n int ans = 0;\n unordered_map<int, int> cnt;\n for (int x: A) {\n ans += cnt[x]++;\n }\n return ans;\n }\n};\n```\n```Java []\nclass Solution {\n public int numIdenticalPairs(int[] ... | 9 | 0 | ['C', 'PHP', 'C++', 'Java', 'Go', 'Python3', 'C#'] | 2 |
number-of-good-pairs | CPP 2 Approaches | Brute ->Better | Unordered Map | cpp-2-approaches-brute-better-unordered-c2273 | 1.Brute Force Method\n# Approach\n->Count Each Pair using nested loops\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity:O(n2 | saurav_1928 | NORMAL | 2023-10-03T01:39:49.133084+00:00 | 2023-10-03T01:42:29.386487+00:00 | 735 | false | # 1.Brute Force Method\n# Approach\n->Count Each Pair using nested loops\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# 2.Usin... | 9 | 0 | ['Array', 'Hash Table', 'Math', 'Counting', 'C++'] | 3 |
number-of-good-pairs | [C++] O(n) Easy Solution using unordered map. | c-on-easy-solution-using-unordered-map-b-s896 | We store the count of each number in unordered map, if the count of a number is greater than 1 then we increase the variable res and store the number of possib | shubhamsth | NORMAL | 2022-02-04T18:14:30.741648+00:00 | 2022-02-04T18:14:30.741686+00:00 | 757 | false | We store the count of each number in unordered map, if the count of a number is greater than 1 then we increase the variable res and store the number of possible combinations which satisfy the given condition given in the question (nums[i] == nums[j] and i<j)\n```\nclass Solution {\npublic:\n \n int numIdentical... | 9 | 0 | ['Hash Table', 'C', 'C++'] | 3 |
number-of-good-pairs | Simple O(n) Java Solution || HashMap | simple-on-java-solution-hashmap-by-himan-scgd | Upvote if you LIKE\uD83D\uDE42 \nclass Solution {\n\n public int numIdenticalPairs(int[] nums) {\n HashMap hm = new HashMap<>();\n int count = | himanshuramranjan | NORMAL | 2021-11-29T02:19:15.187627+00:00 | 2021-11-30T09:52:02.191809+00:00 | 604 | false | **Upvote if you LIKE**\uD83D\uDE42 \nclass Solution {\n\n public int numIdenticalPairs(int[] nums) {\n HashMap<Integer,Integer> hm = new HashMap<>();\n int count = 0;\n for(int i=0;i<nums.length;i++){\n if(hm.containsKey(nums[i])){\n count += hm.get(nums[i]);\n ... | 9 | 1 | ['Java'] | 1 |
number-of-good-pairs | C++ 0 ms, faster than 100.00% O(n) solution | c-0-ms-faster-than-10000-on-solution-by-hlrjq | \nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n \n //Using map to store frequencies of each element\n map<int | sj_codebreaker | NORMAL | 2020-09-17T16:52:33.017777+00:00 | 2020-09-17T16:53:32.055486+00:00 | 938 | false | ```\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n \n //Using map to store frequencies of each element\n map<int,int> count;\n \n int noOfPairs = 0;\n \n for(int i=0;i<nums.size();i++){\n count[nums[i]]+=1;\n }\n \n ... | 9 | 0 | ['C', 'C++'] | 1 |
number-of-good-pairs | Optimal and suboptimal completely Explained with 2 Solutions | optimal-and-suboptimal-completely-explai-nkh9 | \nThis is the bruteforce approach.\n\nTime:O(n^2)\nSpace:O(1)\npython\nclass Solution:\n def numIdenticalPairs(self, nums: List[int]) -> int:\n n = le | akhil_ak | NORMAL | 2020-07-12T04:08:14.708171+00:00 | 2020-07-12T04:08:14.708223+00:00 | 854 | false | \nThis is the bruteforce approach.\n\nTime:O(n^2)\nSpace:O(1)\n```python\nclass Solution:\n def numIdenticalPairs(self, nums: List[int]) -> int:\n n = len(nums)\n res = 0 \n for i in range(n):\n for j in range(i+1,n):\n if nums[i] == nums[j]:\n res+=1... | 9 | 0 | ['Python', 'Python3'] | 2 |
number-of-good-pairs | C program | c-program-by-vibishraj-mxgg | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Vibishraj | NORMAL | 2024-06-13T04:33:18.462305+00:00 | 2024-06-13T04:33:18.462336+00:00 | 110 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 8 | 0 | ['C'] | 3 |
number-of-good-pairs | ✅Simple Java Solutions || ✅Runtime 1ms | simple-java-solutions-runtime-1ms-by-ahm-haj3 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | ahmedna126 | NORMAL | 2023-10-03T13:10:41.666103+00:00 | 2023-11-07T11:34:47.130660+00:00 | 162 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 8 | 0 | ['Java'] | 0 |
number-of-good-pairs | Java | O(N) | 1 Line Logic | Easy | java-on-1-line-logic-easy-by-tejkiran_1-sddy | \nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int ans = 0;\n int[] temp = new int[101];\n \n for (int i = 0; | tejkiran_1 | NORMAL | 2022-10-08T07:03:14.384765+00:00 | 2022-10-08T07:03:14.384808+00:00 | 1,907 | false | ```\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int ans = 0;\n int[] temp = new int[101];\n \n for (int i = 0; i < nums.length; i++) {\n ans += temp[nums[i]]++;\n }\n return ans;\n }\n}\n``` | 8 | 0 | ['Array', 'Java'] | 5 |
number-of-good-pairs | Java || Easiest || 100% faster | java-easiest-100-faster-by-siddhantraii-npkl | \nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n\tint answer = 0;\n\tint[] freq = new int[102];\n \n\tfor (int i : nums) {\n\t\tif (f | siddhantraii | NORMAL | 2020-12-26T11:43:45.847886+00:00 | 2020-12-26T11:43:45.847923+00:00 | 977 | false | ```\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n\tint answer = 0;\n\tint[] freq = new int[102];\n \n\tfor (int i : nums) {\n\t\tif (freq[i] == 0) freq[i]++;\n\t\telse{\n\t\t\tanswer += freq[i];\n\t\t\tfreq[i]++;\n\t\t}\n\t}\n\treturn answer;\n}\n}\n``` | 8 | 3 | ['Java'] | 3 |
number-of-good-pairs | [Python] 98% with O(n) | python-98-with-on-by-pakilamak-umvy | Solution1: Brute force\nTC= O(n*2)\nSC O(n)\n\nclass Solution(object):\n def numIdenticalPairs(self, nums):\n """\n :type nums: List[int]\n | pakilamak | NORMAL | 2020-10-13T17:56:54.724393+00:00 | 2020-10-20T17:24:48.909680+00:00 | 600 | false | Solution1: Brute force\nTC= O(n*2)\nSC O(n)\n```\nclass Solution(object):\n def numIdenticalPairs(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n count =0\n for i in range(len(nums)):\n for j in range(len(nums)):\n if nums[i]==nums... | 8 | 1 | ['Python'] | 1 |
number-of-good-pairs | EASIEST C++ CODE (O(N)) SOLUTION | easiest-c-code-on-solution-by-baibhavsin-mt0l | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nLoop through the input vector:\n\nThe for loop iterates over each element | baibhavsingh07 | NORMAL | 2023-10-03T05:48:01.888263+00:00 | 2023-10-03T05:48:01.888281+00:00 | 653 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nLoop through the input vector:\n\nThe for loop iterates over each element in the input vector a using the index i.\nCheck if the element exists in the map:\n\nInside the loop, there is an if statement that checks if the curr... | 7 | 0 | ['Array', 'Hash Table', 'Math', 'C++'] | 2 |
number-of-good-pairs | Simple Solution || Number of Good Pairs | simple-solution-number-of-good-pairs-by-9ixel | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | himshrivas | NORMAL | 2023-01-28T15:08:06.168696+00:00 | 2023-01-28T15:08:06.168734+00:00 | 2,110 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 7 | 0 | ['Python3'] | 0 |
number-of-good-pairs | ✅ Javascript Solution: Faster than 97.43 % of other submissions || Map || Array Reduce | javascript-solution-faster-than-9743-of-48vkr | Feel free to ask Q\'s...\n#happytohelpu\n\nDo upvote if you find this solution useful. Happy Coding!\n\nRuntime: 58 ms\nMemory Usage: 41.7 MB\n\n\n\n/**\n * @pa | lakshmikant4u | NORMAL | 2022-12-03T18:03:28.080967+00:00 | 2023-04-10T19:33:10.825297+00:00 | 1,882 | false | **Feel free to ask Q\'s...**\n*#happytohelpu*\n\n***Do upvote if you find this solution useful. Happy Coding!***\n\nRuntime: 58 ms\nMemory Usage: 41.7 MB\n\n```\n\n/**\n * @param {number[]} nums\n * @param {Map} map\n * @return {number}\n */\nconst numIdenticalPairs = (nums, map = new Map(), res = 0) => {\n for (let... | 7 | 0 | ['JavaScript'] | 1 |
number-of-good-pairs | C++ Hashmap Solution EXPLAINED | c-hashmap-solution-explained-by-hugsfork-108j | Hello Everyone!\nI hope you find this solution helpful, feel free to comment suggestions and critiques!\n\n\nclass Solution {\npublic:\n int numIdenticalPair | hugsforknife | NORMAL | 2022-10-02T14:48:10.586134+00:00 | 2022-10-02T14:48:10.586186+00:00 | 889 | false | **Hello Everyone!**\nI hope you find this solution helpful, feel free to comment suggestions and critiques!\n\n```\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n unordered_map<int, int>mp;\n int ans = 0;\n for (int i = 0; i < nums.size(); ++i){\n //Go throug... | 7 | 0 | ['C'] | 2 |
number-of-good-pairs | Python 1 liner Without using Counter | python-1-liner-without-using-counter-by-riihn | \ndef numIdenticalPairs(self, nums: List[int]) -> int:\n return (sum(i==j for j in nums for i in nums)-len(nums))//2\n | danielwang5 | NORMAL | 2021-03-09T08:50:06.406560+00:00 | 2021-03-09T08:51:32.875616+00:00 | 229 | false | ```\ndef numIdenticalPairs(self, nums: List[int]) -> int:\n return (sum(i==j for j in nums for i in nums)-len(nums))//2\n``` | 7 | 0 | [] | 1 |
number-of-good-pairs | Python 3, faster than 99.48% | python-3-faster-than-9948-by-narasimhag-f9a1 | The solution to this problem uses a small math formula and a hash table. The following is the approach I used,\n\n1. Build a dictionary to store the array items | narasimhag | NORMAL | 2020-12-28T07:08:18.027087+00:00 | 2020-12-28T07:08:52.466875+00:00 | 1,034 | false | The solution to this problem uses a small math formula and a hash table. The following is the approach I used,\n\n1. Build a dictionary to store the array items as keys and the value is a list of indices where this element appears. Ex: nums = [1,2,1,1]. d = { 1: [0,2,3], 2: [1]}. As I am iterating through the array lin... | 7 | 0 | ['Python3'] | 1 |
number-of-good-pairs | C++ 100% faster | c-100-faster-by-cmshih-tar1 | \nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n unordered_map<int,int> map;\n int ans=0;\n for(auto n:nums){\ | cmshih | NORMAL | 2020-10-05T04:23:22.380144+00:00 | 2020-10-05T04:23:33.990851+00:00 | 1,151 | false | ```\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n unordered_map<int,int> map;\n int ans=0;\n for(auto n:nums){\n map[n]++;\n ans+=(map[n]-1);\n }\n return ans;\n }\n};\n``` | 7 | 0 | ['C'] | 3 |
number-of-good-pairs | 🚀Beats 99.17% || 📈Easy solution with O(n) Approach ✔ | beats-9917-easy-solution-with-on-approac-ls5w | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this code is to count the number of good pairs in the given array | anshuP_cs24 | NORMAL | 2023-10-03T02:43:00.921858+00:00 | 2023-10-03T02:58:43.347310+00:00 | 1,519 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this code is to count the number of good pairs in the given array nums. A good pair consists of two elements with the same value, and the goal is to efficiently find and count these pairs.\n\n---\n\n# Approach\n<!-- D... | 6 | 0 | ['Array', 'Hash Table', 'Math', 'Union Find', 'C', 'Counting', 'Python', 'C++', 'Java', 'Python3'] | 4 |
number-of-good-pairs | Very Simple and Easy to Understand ||c++ || python || java | very-simple-and-easy-to-understand-c-pyt-r5ww | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | me_avi | NORMAL | 2023-10-03T02:04:29.452875+00:00 | 2023-10-03T02:04:29.452896+00:00 | 572 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 6 | 0 | ['Counting', 'Python', 'C++', 'Java'] | 5 |
number-of-good-pairs | C# easy solution. | c-easy-solution-by-aloneguy-fosu | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Complexity\n- Time complexity: 82 ms.Beats 76.37% of other solutions.\n Add your ti | aloneguy | NORMAL | 2023-03-10T13:40:55.915542+00:00 | 2023-03-10T13:40:55.915583+00:00 | 476 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Complexity\n- Time complexity: 82 ms.Beats 76.37% of other solutions.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:38.2 MB.Beats 15.19% of other solutions.\n<!-- Add your space complexity here, e.g. $$O... | 6 | 0 | ['Array', 'C#'] | 0 |
number-of-good-pairs | Easy || JAVA || HashMap || Explained | easy-java-hashmap-explained-by-hashcoder-b766 | \n# Approach\n Describe your approach to solving the problem. \nThis Java code defines a method named numIdenticalPairs that takes an array of integers nums and | hashcoderz | NORMAL | 2023-02-14T16:15:43.140394+00:00 | 2023-02-14T16:15:43.140426+00:00 | 1,448 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nThis Java code defines a method named **numIdenticalPairs** that takes an array of integers nums and returns the number of good pairs of indices **(i, j)** such that **nums[i] == nums[j]** and **i < j**. A good pair is a pair of indices that satisfi... | 6 | 0 | ['Array', 'Hash Table', 'Math', 'Counting', 'Java'] | 0 |
number-of-good-pairs | Java Simple Explanation | O(N) time | 0 ms faster than 100% | java-simple-explanation-on-time-0-ms-fas-f6sa | We declared an empty array of size 101 since 101 is the max number given in constraints.\nThe temp array looks like [0, 0, 0, 0, 0, ... , 0] now.\n\nWe are now | akashdeepghosh | NORMAL | 2022-10-26T13:03:33.890946+00:00 | 2022-10-26T13:03:33.890980+00:00 | 441 | false | We declared an empty array of size 101 since 101 is the max number given in constraints.\nThe temp array looks like [0, 0, 0, 0, 0, ... , 0] now.\n\nWe are now looping in the given nums array and going to each element and adding it\'s temp count.\n\nLet\'s understand this by an example.\nGiven `nums = [1,2,3,1,1,3]`\nw... | 6 | 0 | ['Java'] | 0 |
number-of-good-pairs | ✅Easy Java solution||Simple||Beginner Friendly🔥 | easy-java-solutionsimplebeginner-friendl-252o | If you really found my solution helpful please upvote it, as it motivates me to post such kind of codes and help the coding community, if you have some queries | deepVashisth | NORMAL | 2022-09-07T11:34:42.819129+00:00 | 2022-09-07T11:34:42.819165+00:00 | 410 | false | **If you really found my solution helpful please upvote it, as it motivates me to post such kind of codes and help the coding community, if you have some queries or some improvements please feel free to comment and share your views.**\n```\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int c... | 6 | 0 | ['Java'] | 0 |
number-of-good-pairs | Python3 | Hash Table | O(n) | 93.3% Faster, 98.04% memory efficient. | python3-hash-table-on-933-faster-9804-me-f4uv | \n\ndef numIdenticalPairs(self, nums: List[int]) -> int:\n d={}\n c=0\n for i in range(len(nums)):\n if nums[i] in d:\n | atifnawaz13 | NORMAL | 2021-08-04T20:15:16.744660+00:00 | 2021-08-04T20:15:16.744702+00:00 | 513 | false | \n```\ndef numIdenticalPairs(self, nums: List[int]) -> int:\n d={}\n c=0\n for i in range(len(nums)):\n if nums[i] in d:\n d[nums[i]]+=1\n c+=d[nums[i]]\n else:\n d[nums[i]]=0\n return c\n```\nPlease Upvote\nHappy Coding!! | 6 | 0 | ['Python', 'Python3'] | 0 |
number-of-good-pairs | Kotlin 1 line | kotlin-1-line-by-georgcantor-xs9u | \nfun numIdenticalPairs(a: IntArray) = a.mapIndexed { i, n -> a.slice(i + 1..a.lastIndex).count { it == n } }.sum()\n\nApproach 2:\n\nfun numIdenticalPairs(a: I | GeorgCantor | NORMAL | 2021-02-15T08:49:38.808611+00:00 | 2021-07-18T09:58:39.926250+00:00 | 423 | false | ```\nfun numIdenticalPairs(a: IntArray) = a.mapIndexed { i, n -> a.slice(i + 1..a.lastIndex).count { it == n } }.sum()\n```\n**Approach 2:**\n```\nfun numIdenticalPairs(a: IntArray): Int {\n var c = 0\n a.forEachIndexed { i, n -> for (j in i + 1 until a.size) if (n == a[j]) c++ }\n return c\n}\n```\n**Approach... | 6 | 0 | ['Kotlin'] | 0 |
number-of-good-pairs | JavaScript Solution > 93% Speed using map and counter | javascript-solution-93-speed-using-map-a-zcia | ```/*\n * @param {number[]} nums\n * @return {number}\n /\nvar numIdenticalPairs = function(nums) {\n const map = new Map();\n let pairs = 0;\n\n for ( | gideonbabu | NORMAL | 2021-01-23T18:46:23.542973+00:00 | 2021-01-23T18:46:23.543023+00:00 | 918 | false | ```/**\n * @param {number[]} nums\n * @return {number}\n */\nvar numIdenticalPairs = function(nums) {\n const map = new Map();\n let pairs = 0;\n\n for (let i = 0; i < nums.length; i++) {\n if (map.has(nums[i])) {\n pairs += map.get(nums[i]);\n map.set(nums[i], map.get(nums[i]) + 1... | 6 | 0 | ['JavaScript'] | 0 |
number-of-good-pairs | Simple JavaScript solution | simple-javascript-solution-by-medhatisaa-rdil | \nvar numIdenticalPairs = function(nums) {\n let obj = {};\n let counter = 0;\n\n for (val of nums) {\n if (obj[val]) {\n counter | medhatisaac | NORMAL | 2020-11-14T16:19:44.517274+00:00 | 2020-11-14T16:19:44.517303+00:00 | 864 | false | ```\nvar numIdenticalPairs = function(nums) {\n let obj = {};\n let counter = 0;\n\n for (val of nums) {\n if (obj[val]) {\n counter += obj[val];\n obj[val]++;\n } else {\n obj[val] = 1;\n }\n }\n console.log(obj);\n return counter;\n};\n``` | 6 | 0 | ['JavaScript'] | 0 |
number-of-good-pairs | Python3 solution with a single pass and no if-else statements | python3-solution-with-a-single-pass-and-i367q | This solution makes use of a single pass and no if-else statements.\n\n@navinmittal29 Thanks for your suggestion on how to avoid using the if-else statements in | ecampana | NORMAL | 2020-09-09T04:51:14.456237+00:00 | 2020-09-09T04:51:14.456295+00:00 | 534 | false | This solution makes use of a single pass and no if-else statements.\n\n[@navinmittal29](https://leetcode.com/navinmittal29) Thanks for your suggestion on how to avoid using the if-else statements in my previous solution.\n\n\n```\nclass Solution:\n def numIdenticalPairs(self, nums: List[int]) -> int:\n my_cou... | 6 | 0 | ['Python', 'Python3'] | 0 |
number-of-good-pairs | Simplest Solution with Python 3 | simplest-solution-with-python-3-by-thepy-xag3 | \n def numIdenticalPairs(self, nums: List[int]) -> int:\n set_nums=set(nums)\n good=0\n for x in set_nums:\n n=nums.count(x)\ | thepylama | NORMAL | 2020-07-20T15:10:04.231479+00:00 | 2020-07-20T15:10:04.231516+00:00 | 718 | false | ```\n def numIdenticalPairs(self, nums: List[int]) -> int:\n set_nums=set(nums)\n good=0\n for x in set_nums:\n n=nums.count(x)\n good+=(n*(n-1))/2 \n\t\t\t#finding number of occurences of element and using nC2 to find good pairs \n return int(good)\n``` | 6 | 1 | [] | 4 |
number-of-good-pairs | [Java/Python 3] 4/1 liners O(n) counting codes w/ brief explanation and analysis. | javapython-3-41-liners-on-counting-codes-f1xi | For any given i items, we have C(i, 2) = (i - 1) * i / 2 combination options if choosing pairs from the i elements.\njava\n public int numIdenticalPairs(int[ | rock | NORMAL | 2020-07-12T04:23:12.027843+00:00 | 2020-07-17T17:50:22.200435+00:00 | 824 | false | For any given `i` items, we have `C(i, 2) = (i - 1) * i / 2` combination options if choosing pairs from the `i` elements.\n```java\n public int numIdenticalPairs(int[] nums) {\n int[] cnt = new int[101];\n for (int n : nums)\n ++cnt[n];\n return Arrays.stream(cnt).map(i -> (i - 1) * i... | 6 | 0 | [] | 3 |
number-of-good-pairs | Easy and Simple CPP code!💯✅ | easy-and-simple-cpp-code-by-siddharth_si-fqex | Intuition\nThe problem asks us to count the number of "good pairs" in the array, where a pair (i, j) is considered "good" if nums[i] == nums[j] and i < j. Essen | siddharth_sid_k | NORMAL | 2024-08-23T14:21:14.642283+00:00 | 2024-08-23T14:21:14.642312+00:00 | 264 | false | # Intuition\nThe problem asks us to count the number of "good pairs" in the array, where a pair (i, j) is considered "good" if nums[i] == nums[j] and i < j. Essentially, we are looking for identical values at different positions in the array. This problem can be solved using a brute-force approach by comparing each ele... | 5 | 0 | ['C++'] | 0 |
number-of-good-pairs | ✅Very Easy To Understand✅| 3 Approaches | C++ |✅ Optimal- Beats 100% and Brute Force | very-easy-to-understand-3-approaches-c-o-1823 | Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to find all pairs where nums[i]==nums[j] where i < j basically which indicates | Ritik7 | NORMAL | 2023-10-03T18:49:30.818479+00:00 | 2023-10-03T18:49:30.818509+00:00 | 68 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to find all pairs where ```nums[i]==nums[j]``` where ```i < j``` basically which indicates ```{i,j}``` and ```{j,i}``` should be counted once.\n\n- To do this there can be multiple approaches- \n - Bruteforce, Starting from the... | 5 | 0 | ['C++'] | 1 |
number-of-good-pairs | Simple and Easiest Solution for this Question in C++ | simple-and-easiest-solution-for-this-que-7ft9 | Intuition\nThe intuition behind the given code is to find and count the number of identical pairs of elements in a given vector nums. Here\'s the step-by-step i | RaviRanjan940 | NORMAL | 2023-10-03T17:11:40.819891+00:00 | 2023-10-03T17:11:40.819920+00:00 | 51 | false | # Intuition\nThe intuition behind the given code is to find and count the number of identical pairs of elements in a given vector `nums`. Here\'s the step-by-step intuition:\n\n1. **Initialization**: Initialize variables `n` to store the size of the input vector `nums` and `cnt` to keep track of the count of identical ... | 5 | 0 | ['C++'] | 0 |
number-of-good-pairs | C/C++ array 0ms Beats 100% | cc-array-0ms-beats-100-by-anwendeng-iit9 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nLet N[x] store the numb | anwendeng | NORMAL | 2023-10-03T05:00:46.778262+00:00 | 2023-10-03T10:38:17.477776+00:00 | 340 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nLet N[x] store the number of occurrencies of x.\nThere are N[x]*(N[x]-1)/2 good pairs for x.\nSumming up\n# Complexity\n- Time complexity:\n<!-- Add your time complexi... | 5 | 0 | ['Array', 'C++'] | 1 |
number-of-good-pairs | Find number of good pairs in O(n) | find-number-of-good-pairs-in-on-by-sanke-xodg | Intuition\nTo solve this problem here combinational formula(nCr) is used. \n\n# Approach\nIf we get the count for all the elements from array separately and the | sanketpatil7467 | NORMAL | 2023-10-03T03:05:07.933741+00:00 | 2023-10-03T03:05:07.933767+00:00 | 727 | false | # Intuition\nTo solve this problem here combinational formula(nCr) is used. \n\n# Approach\nIf we get the count for all the elements from array separately and then perform combinational formula on each of them then we get number of good pairs. So to get count of all elements Hashmap is used.\nAt the end we are returnin... | 5 | 0 | ['Hash Table', 'Java'] | 3 |
number-of-good-pairs | ✔🚀96.90% Beats O(n) Solution ✨📈|| Detailed Explanation || Easy to Understand🚀 | 9690-beats-on-solution-detailed-explanat-j044 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this code is to count the number of good pairs in the given array | saket_1 | NORMAL | 2023-10-03T02:34:25.695757+00:00 | 2023-10-03T05:30:27.803401+00:00 | 904 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this code is to count the number of good pairs in the given array nums. A good pair consists of two elements with the same value, and the goal is to efficiently find and count these pairs.\n\n---\n\n# Approach\n<!-- D... | 5 | 0 | ['Array', 'Hash Table', 'Linked List', 'Math', 'C', 'Counting', 'Python', 'C++', 'Java', 'Python3'] | 3 |
number-of-good-pairs | 🚀✔99.78% Beats O(n) Solution 📈|| Easy and detailed explanation ever ❣ | 9978-beats-on-solution-easy-and-detailed-6qm1 | Intuition\uD83D\uDE80\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this solution is to count the occurrences of each numb | nikkipriya_78 | NORMAL | 2023-10-03T02:28:31.912708+00:00 | 2023-10-07T13:51:55.866156+00:00 | 956 | false | # Intuition\uD83D\uDE80\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this solution is to count the occurrences of each number in the input array using an unordered map (countMap). Then, for each unique number in the map, we calculate the number of good pairs using a formula... | 5 | 0 | ['Array', 'Hash Table', 'Math', 'C', 'Sliding Window', 'Counting', 'Python', 'C++', 'Java', 'Python3'] | 3 |
number-of-good-pairs | C# Simple Solution Beats 100% | c-simple-solution-beats-100-by-apakg-jjm8 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Apakg | NORMAL | 2023-05-07T18:57:29.218581+00:00 | 2023-05-07T18:57:29.218615+00:00 | 696 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 5 | 0 | ['C#'] | 1 |
number-of-good-pairs | JAVA || O (n log n) || Hashmap used || For beginners || Explained !!! 💡 | java-o-n-log-n-hashmap-used-for-beginner-pwfl | Approach\n- Create a hashmap\n- O(n log n) nested loop setup for finding out pairs\n- If a key == nums[i] does not exist upon finding a pair, push a pair (nums[ | AbirDey | NORMAL | 2023-04-27T13:58:07.259413+00:00 | 2023-04-27T13:58:07.259462+00:00 | 1,920 | false | # Approach\n- Create a hashmap\n- O(n log n) nested loop setup for finding out pairs\n- If a key == nums[i] does not exist upon finding a pair, push a pair (nums[i] , 1)\n- If key exist, increment the value corresponding to the key in the hashmap.\n- for all the key values in the map, return the sum of all the values ... | 5 | 0 | ['Hash Table', 'Java'] | 2 |
number-of-good-pairs | Find the number of identical pairs in an array of integers | find-the-number-of-identical-pairs-in-an-polj | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is asking to find the number of identical pairs in an array of integers. We | RinatMambetov | NORMAL | 2023-04-04T15:05:21.567412+00:00 | 2023-04-04T15:05:21.567450+00:00 | 2,272 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking to find the number of identical pairs in an array of integers. We can keep track of the frequency of each integer using a hash map, where the keys are the integers and the values are the frequency of occurrence. Then... | 5 | 0 | ['JavaScript'] | 1 |
number-of-good-pairs | C++ easy solution. | c-easy-solution-by-aloneguy-sclu | \n\n# Code\n\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n int count=0;\n for(int i=0;i<nums.size()-1;i++){\n | aloneguy | NORMAL | 2023-03-12T20:25:37.110806+00:00 | 2023-03-12T20:25:37.110835+00:00 | 444 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n int count=0;\n for(int i=0;i<nums.size()-1;i++){\n for(int j=i+1;j<nums.size();j++){\n if(nums[i]==nums[j])count++;\n }\n }\n return count;\n }\n};\n``` | 5 | 0 | ['C++'] | 1 |
number-of-good-pairs | Beats 100% Java Solution | beats-100-java-solution-by-jaiyadav-aodz | \n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n\n Has | jaiyadav | NORMAL | 2023-01-01T05:26:55.730708+00:00 | 2023-01-01T05:26:55.730758+00:00 | 1,560 | false | \n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n\n HashMap<Integer,Integer>mp=new HashMap<>();\n\n int count=0;\n\n for(int i=0;i<nums.length;i++){\n\n if(!mp.containsKey(nums[i])){\n mp.put(nums... | 5 | 0 | ['Java'] | 1 |
number-of-good-pairs | Using basic combination with bucket array | using-basic-combination-with-bucket-arra-e5ty | Intuition\n(Read the note below)\nSince we have to count the number of pairs using nCr which is n!/(n-r)! * r!\nTaking r = 2, since we are to count the number o | himanshubanerji | NORMAL | 2022-12-29T19:20:32.493199+00:00 | 2022-12-29T19:20:32.493244+00:00 | 855 | false | # Intuition\n(Read the note below)\nSince we have to count the number of pairs using `nCr` which is `n!/(n-r)! * r!`\nTaking `r = 2`, since we are to count the number of pairs\n\n`nC2 = n!/(n-2)! * (2)!`\n\n# Approach\nNow next step was to minimize the value of `nC2` else taking the worst case scenario 100 (max value o... | 5 | 0 | ['Math', 'C++'] | 2 |
number-of-good-pairs | Single loop, O(n) speed, hash table, fast | single-loop-on-speed-hash-table-fast-by-jcsyo | Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe good pair quantites are same as how many identical number already accululated eac | micronew | NORMAL | 2022-12-11T20:59:00.777261+00:00 | 2022-12-11T21:05:46.413718+00:00 | 619 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe good pair quantites are same as how many identical number already accululated each time when you encounter the same number. For example, if number 1 appears 4 times already, then the next time you encounter number 1, that will contr... | 5 | 0 | ['Hash Table', 'C', 'Hash Function'] | 0 |
number-of-good-pairs | [C++] MAP + MATH. O(n) | c-map-math-on-by-rahug-643m | Explanation\ncount the occurrence of the same elements.\nFor each new element a,\nthere will be more count[a] pairs.\nwith A[i] == A[j] and i < j\n\nExplaining | Rahug | NORMAL | 2022-11-02T17:14:51.263447+00:00 | 2022-11-02T17:14:51.263553+00:00 | 1,198 | false | ***Explanation***\ncount the occurrence of the same elements.\nFor each new element a,\nthere will be more count[a] pairs.\nwith *A[i] == A[j]* and *i < j*\n\n***Explaining the logic:***\n1) We can store the ints like a key in map, and the indexes save in the vector of ints like a value in a map. \nFor example 1, it w... | 5 | 0 | ['Math', 'C'] | 3 |
number-of-good-pairs | ✔️C++|| Explained|| Easy|| Using Permutation | c-explained-easy-using-permutation-by-sr-xr46 | \nThe approach is very simple. Count number of duplicates for each element in a given array and calculate nC2 which is used to select two elements at a time.\n\ | srikrishna0874 | NORMAL | 2022-10-07T08:07:48.431682+00:00 | 2022-10-07T08:07:48.431720+00:00 | 203 | false | ```\nThe approach is very simple. Count number of duplicates for each element in a given array and calculate nC2 which is used to select two elements at a time.\n\nNote: nC2=n*(n-1)/2\n```\n```\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n map<int,int> m;\n for(int i=0;i<num... | 5 | 0 | ['C', 'Probability and Statistics'] | 0 |
number-of-good-pairs | Java Solution using HashMap | java-solution-using-hashmap-by-komal_sha-g63a | Time Complexity : O(N)\n\n```\n\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int count = 0;\n HashMap map = new HashMap<>() | Komal_Sharma15 | NORMAL | 2022-09-01T14:43:10.833147+00:00 | 2022-09-01T14:44:11.658345+00:00 | 422 | false | Time Complexity : O(N)\n\n```\n\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int count = 0;\n HashMap<Integer, Integer> map = new HashMap<>();\n for(int i=0; i<nums.length; i++){\n if(map.containsKey(nums[i])){\n count += map.get(nums[i]);\n ... | 5 | 0 | ['Java'] | 2 |
number-of-good-pairs | C++ | O(n) | Best Explanation | Easy Understanding | c-on-best-explanation-easy-understanding-tqcy | Use new version of leetcode to see latex expressions properly\n\n\n// Time Complexity = O(n)\n// Space Complexity = O(n)\n// Runtime : 0ms, faster than 100.00%\ | meteahmetyakar | NORMAL | 2022-08-19T15:42:00.424750+00:00 | 2023-05-17T20:41:47.945379+00:00 | 765 | false | ### ***Use new version of leetcode to see latex expressions properly***\n***\n```\n// Time Complexity = O(n)\n// Space Complexity = O(n)\n// Runtime : 0ms, faster than 100.00%\n// Memory Usage : 7.2 MB, less than 60.74%\n```\n\n```\n int numIdenticalPairs(vector<int>& nums) {\n \n unordered_map<int,int... | 5 | 0 | ['Hash Table', 'Math', 'C++'] | 0 |
number-of-good-pairs | Python oneliner solution | python-oneliner-solution-by-trpaslik-qqfk | Here is my python one-liner:\n\npython\n return sum(c * (c - 1) // 2 for c in Counter(nums).values())\n | trpaslik | NORMAL | 2022-05-31T11:09:51.878939+00:00 | 2022-05-31T11:09:51.878969+00:00 | 759 | false | Here is my python one-liner:\n\n```python\n return sum(c * (c - 1) // 2 for c in Counter(nums).values())\n``` | 5 | 0 | ['Python'] | 0 |
number-of-good-pairs | Good 100% faster solution | good-100-faster-solution-by-sourav_17-xxfy | \nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n int n = nums.size();\n map<int,int> smap;\n int count =0;\n | sourav_17 | NORMAL | 2021-04-10T06:18:58.386590+00:00 | 2021-04-10T06:18:58.386633+00:00 | 617 | false | ```\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n int n = nums.size();\n map<int,int> smap;\n int count =0;\n for(int i =0;i<n;i++)\n {\n count+=smap[nums[i]];\n smap[nums[i]]++;\n }\n return count;\n \n }\n... | 5 | 0 | ['C', 'C++'] | 0 |
number-of-good-pairs | Java | Hashing | 2 methods | java-hashing-2-methods-by-prashant404-2aki | Method 1: \n>T/S: O(n\xB2)/O(1), where n = size(nums)\n\npublic int numIdenticalPairs(int[] nums) {\n\tvar count = 0;\n\n\tfor (var i = 0; i < nums.length; i++) | prashant404 | NORMAL | 2020-12-22T04:54:04.075167+00:00 | 2023-10-03T07:25:04.331316+00:00 | 798 | false | **Method 1**: \n>T/S: O(n\xB2)/O(1), where n = size(nums)\n```\npublic int numIdenticalPairs(int[] nums) {\n\tvar count = 0;\n\n\tfor (var i = 0; i < nums.length; i++)\n\t\tfor (var j = i + 1; j < nums.length; j++)\n\t\t\tif (nums[i] == nums[j])\n\t\t\t\tcount++;\n\n\treturn count;\n}\n```\n\n**Method 2**: \n>T/S: O(n)... | 5 | 0 | ['Java'] | 1 |
number-of-good-pairs | Python 3 -> 98.52% faster. 3 techniques but only 1 optimal solution | python-3-9852-faster-3-techniques-but-on-v46w | Suggestions to make it better are always welcomed.\n\nThere are 3 ways of doing this:\n1. Use 2 for loops. For every ith element, compare with the remaining i+1 | mybuddy29 | NORMAL | 2020-08-29T23:20:46.713391+00:00 | 2020-08-29T23:20:46.713437+00:00 | 573 | false | **Suggestions to make it better are always welcomed.**\n\nThere are 3 ways of doing this:\n1. Use 2 for loops. For every ith element, compare with the remaining i+1 to n elements. If match is found, increase the count. This is T=O(n2) and S=O(1)\n\n2. Sort the list before processing it. This way all the matching number... | 5 | 1 | ['Python', 'Python3'] | 2 |
number-of-good-pairs | Python 3 Solution With defaultdict | python-3-solution-with-defaultdict-by-hs-5owk | \nclass Solution:\n def numIdenticalPairs(self, nums) -> int:\n """\n Given an array of numbers, this program determines the\n number of | hsweiner54 | NORMAL | 2020-07-12T14:44:12.923048+00:00 | 2020-07-12T14:44:12.923085+00:00 | 379 | false | ```\nclass Solution:\n def numIdenticalPairs(self, nums) -> int:\n """\n Given an array of numbers, this program determines the\n number of good pairs [nums[j], nums[k]]. In a good pair,\n j < k and nums[j] = nums[k].\n \n If there are 2 or more ocurrences of a value within ... | 5 | 1 | ['Python3'] | 0 |
number-of-good-pairs | Python - Faster than 100% of Submission | python-faster-than-100-of-submission-by-mif79 | \n freq={}\n output=0\n for num in nums:\n if not num in freq:\n freq[num]=0\n freq[num]+=1\n \ | azrap | NORMAL | 2020-07-12T09:36:34.731282+00:00 | 2020-07-12T09:36:34.731316+00:00 | 756 | false | ```\n freq={}\n output=0\n for num in nums:\n if not num in freq:\n freq[num]=0\n freq[num]+=1\n \n for num in freq:\n n=freq[num]\n output+=(n*(n-1)//2)\n \n return output\n```\n\n | 5 | 0 | [] | 1 |
number-of-good-pairs | Simple Java Code ☠️ | simple-java-code-by-abhinandannaik1717-vedq | Code\njava []\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int n = nums.length;\n HashMap<Integer,Integer> map = new HashMa | abhinandannaik1717 | NORMAL | 2024-08-23T16:57:34.041905+00:00 | 2024-08-23T16:57:34.041935+00:00 | 512 | false | # Code\n```java []\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int n = nums.length;\n HashMap<Integer,Integer> map = new HashMap<>();\n int c=0;\n for(int i=0;i<n;i++){\n if(!map.isEmpty() && map.containsKey(nums[i])){\n c+=map.get(nums[i]);\... | 4 | 0 | ['Java'] | 0 |
number-of-good-pairs | 43 ms -----users with Python3 | 43-ms-users-with-python3-by-kawinp-znn6 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | KawinP | NORMAL | 2024-05-27T17:26:19.966891+00:00 | 2024-05-27T17:26:19.966913+00:00 | 1,007 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $... | 4 | 0 | ['Python3'] | 0 |
number-of-good-pairs | ⭐ Let's solve in easy way| Brute force Approach | C++, C, JavaScript ✅ | lets-solve-in-easy-way-brute-force-appro-farj | Approach\nIn the quesiton, Same number creates a good pair.\n\nFor easy to understand,\nGood pair = Two values that are the same form a pair, but their index va | Captain2000 | NORMAL | 2024-04-02T13:35:24.216591+00:00 | 2024-04-06T11:09:48.815979+00:00 | 245 | false | # Approach\nIn the quesiton, Same number creates a good pair.\n\nFor easy to understand,\nGood pair = Two values that are the same form a pair, but their index values are different. \n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space ... | 4 | 0 | ['C', 'C++', 'JavaScript'] | 0 |
surrounded-regions | C++ Beginner Friendly | Boundary DFS | inPlace | c-beginner-friendly-boundary-dfs-inplace-ldk2 | \nclass Solution {\npublic:\n void DFS(vector<vector<char>>& board, int i, int j, int m, int n) {\n if(i<0 or j<0 or i>=m or j>=n or board[i][j] != \' | chronoviser | NORMAL | 2020-06-17T08:04:22.754075+00:00 | 2020-06-17T08:04:22.754108+00:00 | 56,589 | false | ```\nclass Solution {\npublic:\n void DFS(vector<vector<char>>& board, int i, int j, int m, int n) {\n if(i<0 or j<0 or i>=m or j>=n or board[i][j] != \'O\') return;\n board[i][j] = \'#\';\n DFS(board, i-1, j, m, n);\n DFS(board, i+1, j, m, n);\n DFS(board, i, j-1, m, n);\n ... | 1,588 | 6 | [] | 84 |
surrounded-regions | A really simple and readable C++ solution\uff0conly cost 12ms | a-really-simple-and-readable-c-solutionu-kpel | First, check the four border of the matrix. If there is a element is\n 'O', alter it and all its neighbor 'O' elements to '1'.\n \n \n - Then ,alter all t | sugeladi | NORMAL | 2015-06-27T02:50:31+00:00 | 2018-10-25T23:27:01.864376+00:00 | 97,448 | false | - First, check the four border of the matrix. If there is a element is\n 'O', alter it and all its neighbor 'O' elements to '1'.\n \n \n - Then ,alter all the 'O' to 'X'\n - At last,alter all the '1' to 'O'\n\nFor example:\n\n X X X X X X X X X X X X\n X X O X -> ... | 533 | 15 | ['C++'] | 68 |
surrounded-regions | 9 lines, Python 148 ms | 9-lines-python-148-ms-by-stefanpochmann-i0ps | Phase 1: "Save" every O-region touching the border, changing its cells to 'S'. \nPhase 2: Change every 'S' on the board to 'O' and everything else to 'X'.\n\n | stefanpochmann | NORMAL | 2015-07-14T11:40:54+00:00 | 2018-10-22T07:22:20.456625+00:00 | 50,348 | false | Phase 1: "Save" every O-region touching the border, changing its cells to 'S'. \nPhase 2: Change every 'S' on the board to 'O' and everything else to 'X'.\n\n def solve(self, board):\n if not any(board): return\n \n m, n = len(board), len(board[0])\n save = [ij for k in range(m+n) for ij in ... | 292 | 13 | ['Python'] | 41 |
surrounded-regions | Java DFS + boundary cell turning solution, simple and clean code, commented. | java-dfs-boundary-cell-turning-solution-4sf3p | \tpublic void solve(char[][] board) {\n\t\tif (board.length == 0 || board[0].length == 0)\n\t\t\treturn;\n\t\tif (board.length < 2 || board[0].length < 2)\n\t\t | jinwu | NORMAL | 2015-09-22T03:42:59+00:00 | 2018-10-16T01:09:16.140119+00:00 | 76,532 | false | \tpublic void solve(char[][] board) {\n\t\tif (board.length == 0 || board[0].length == 0)\n\t\t\treturn;\n\t\tif (board.length < 2 || board[0].length < 2)\n\t\t\treturn;\n\t\tint m = board.length, n = board[0].length;\n\t\t//Any 'O' connected to a boundary can't be turned to 'X', so ...\n\t\t//Start from first and l... | 261 | 6 | ['Depth-First Search', 'Java'] | 49 |
surrounded-regions | Easy Explained Solution With 🏞 (Images) | easy-explained-solution-with-images-by-y-n6r6 | \n\n\n\n\n\n\n\n\ncode\npython\nfrom collections import deque\n\nclass Solution:\n def solve(self, board: List[List[str]]) -> None:\n """\n Do | yasir991925 | NORMAL | 2021-11-01T07:51:57.859028+00:00 | 2021-11-01T07:52:49.130693+00:00 | 19,458 | false | \n\n\n \tint count; // number of components\n public:\n \tUF(int N)\n \t{\n \t\tcount = N;\n \t\tid = new int[N];\n \t\trank = new int... | 213 | 1 | [] | 36 |
surrounded-regions | Python easy to understand DFS and BFS solutions | python-easy-to-understand-dfs-and-bfs-so-zh4y | \nclass Solution(object):\n # DFS\n def solve1(self, board):\n if not board or not board[0]:\n return \n for i in [0, len(board)- | oldcodingfarmer | NORMAL | 2015-09-10T10:07:56+00:00 | 2020-09-17T16:11:51.620288+00:00 | 27,121 | false | ```\nclass Solution(object):\n # DFS\n def solve1(self, board):\n if not board or not board[0]:\n return \n for i in [0, len(board)-1]:\n for j in range(len(board[0])):\n self.dfs(board, i, j) \n for i in range(len(board)):\n for j in [0, len(... | 201 | 5 | ['Depth-First Search', 'Breadth-First Search', 'Python'] | 19 |
surrounded-regions | [Python] O(mn), 3 colors dfs, explained | python-omn-3-colors-dfs-explained-by-dba-eenh | In this problem we need to understand, what exactly surrouned by \'X\' means. It actually means that if we start from \'O\' at the border, and we traverse only | dbabichev | NORMAL | 2020-06-17T07:45:28.910212+00:00 | 2020-06-17T10:38:28.590138+00:00 | 7,071 | false | In this problem we need to understand, what exactly surrouned by `\'X\'` means. It actually means that if we start from `\'O\'` at the border, and we traverse only `\'O\'`, only those `\'O\'` are **not surrouned** by `\'X\'`. So the plan is the following:\n\n1. Start dfs or bfs from all `\'O\'`, which are on the border... | 140 | 2 | ['Depth-First Search'] | 9 |
surrounded-regions | [Java] Simple and Readable Solution | BFS | java-simple-and-readable-solution-bfs-by-avh5 | Problem statement at a glance: So, a m x n matrix is given with \'O\'s and \'X\'s and we to make all \'O\'s to \'X\'s which are on 4 directions of a \'X\' and t | poojitha_792 | NORMAL | 2021-11-01T12:13:36.930508+00:00 | 2021-11-01T13:02:59.615442+00:00 | 7,654 | false | **Problem statement at a glance**: So, a m x n matrix is given with \'O\'s and \'X\'s and we to make all \'O\'s to \'X\'s which are on 4 directions of a \'X\' and they should not be connected with an \'O\' which is on border.\n**Approach:**\n1) Traverse all borders of the grid and if you find an \'O\', then mark all th... | 103 | 6 | ['Breadth-First Search'] | 15 |
surrounded-regions | My BFS solution (C++ 28ms) | my-bfs-solution-c-28ms-by-eaglesky-w8rf | The algorithm is quite simple: Use BFS starting from 'O's on the boundary and mark them as 'B', then iterate over the whole board and mark 'O' as 'X' and 'B' a | eaglesky | NORMAL | 2014-08-27T08:01:57+00:00 | 2014-08-27T08:01:57+00:00 | 36,823 | false | The algorithm is quite simple: Use BFS starting from 'O's on the boundary and mark them as 'B', then iterate over the whole board and mark 'O' as 'X' and 'B' as 'O'. \n\n\n\n void bfsBoundary(vector<vector<char> >& board, int w, int l)\n {\n int width = board.size();\n int length = board[0].size();... | 97 | 1 | ['Breadth-First Search'] | 31 |
surrounded-regions | Simple BFS solution - easy to understand | simple-bfs-solution-easy-to-understand-b-socy | The idea is to first find all 'O's on the edge, and do BFS from these 'O's. Keep adding 'O's into the queue in the BFS, and mark it as '+'. Since these 'O's are | husqing | NORMAL | 2015-06-05T18:51:08+00:00 | 2018-10-20T02:53:50.736488+00:00 | 19,014 | false | The idea is to first find all 'O's on the edge, and do BFS from these 'O's. Keep adding 'O's into the queue in the BFS, and mark it as '+'. Since these 'O's are found by doing BFS from the 'O's on the edge, it means they are connected to the edge 'O's. so they are the 'O's that will remain as 'O' in the result. \n\nAft... | 88 | 3 | ['Breadth-First Search', 'Java'] | 10 |
surrounded-regions | Java Union-Find with Explanations | java-union-find-with-explanations-by-gra-jhmm | Click to see DFS Solution\n\nLogical Thought\nWe aim to find all \'O\'s such that it is on the border or it is connected to an \'O\' on the border. If we regard | gracemeng | NORMAL | 2018-09-05T21:36:37.481111+00:00 | 2018-10-09T06:25:15.012099+00:00 | 4,441 | false | [Click to see DFS Solution](https://leetcode.com/problems/surrounded-regions/discuss/163363/Logical-Thinking-with-Code)\n\n**Logical Thought**\nWe aim to find all \'O\'s such that it is on the border or it is connected to an \'O\' on the border. If we regard \'O\' mentioned above as a `node` (or an `element`), the prob... | 83 | 1 | [] | 13 |
surrounded-regions | Fast and very easy to understand | DFS from borders | O(N) time | O(N) Space | fast-and-very-easy-to-understand-dfs-fro-ym69 | This problem could be solved two ways:\n1. Identify all isolated \'O\' cells and mark them as \'X\' then.\n2. Mark all accessible from boarders \'O\' and then t | dartkron | NORMAL | 2021-11-01T00:55:05.473285+00:00 | 2021-11-01T14:06:48.057423+00:00 | 10,969 | false | This problem could be solved two ways:\n1. Identify all isolated `\'O\'` cells and mark them as `\'X\'` then.\n2. Mark all accessible from boarders `\'O\'` and then turn all inaccessible ones into `\'X\'`.\n\nThis solution is about the **second way**.\n\nTo mark cells as "reachable", algorithm changing their values to ... | 62 | 2 | ['C', 'Python', 'Go', 'Python3'] | 6 |
surrounded-regions | [ c++ | JAVA ]Clean and simple [ DFS ] [ Explain ] | c-java-clean-and-simple-dfs-explain-by-s-rx3u | The Idea is first to Mark those grid which having value \' O \' are deeply adjacent to the First Row , First Column , Last Row and Last Column !!! \nso we are | suman_cp | NORMAL | 2020-06-17T13:11:19.054396+00:00 | 2020-06-18T05:59:18.927442+00:00 | 4,494 | false | The Idea is first to Mark those grid which having value \' O \' are deeply adjacent to the First Row , First Column , Last Row and Last Column !!! \nso we are going to update those grid by changeing it\'s value \' O\' - > \' P \' [ I am use \' P \' here ,but You can chose any Value except \' X \' and \' O \' ]\nTo ma... | 62 | 2 | ['C', 'Java'] | 3 |
surrounded-regions | JAVA-----------Easy Version To UnderStand!!!!!!!!!!!! | java-easy-version-to-understand-by-hello-t2pw | ----------\n## Use BFS.This problem is similar to Number of Islands. In this problem, only the cells on the boarders can not be surrounded. So we can first merg | helloworld123456 | NORMAL | 2016-01-24T07:46:52+00:00 | 2018-09-30T19:13:14.673320+00:00 | 12,798 | false | ----------\n## Use BFS.This problem is similar to Number of Islands. In this problem, only the cells on the boarders can not be surrounded. So we can first merge those O's on the boarders like in Number of Islands and replace O's with 'B', and then scan the board and replace all O's left (if any). ##\n\n class Point... | 54 | 2 | [] | 10 |
surrounded-regions | [C++] DFS Easy and clean solution [T: 90% | M: 80%] | c-dfs-easy-and-clean-solution-t-90-m-80-n864e | Please like the post if you found it helpful. And please don\'t downvote. It really demotivates and cancels the purpose of sharing knowledge in the community\n\ | harshit0311 | NORMAL | 2020-06-17T11:28:18.749985+00:00 | 2020-06-17T11:28:18.750015+00:00 | 3,878 | false | ***Please like the post if you found it helpful. And please don\'t downvote. It really demotivates and cancels the purpose of sharing knowledge in the community***\n\nThe idea is to iterate through the edges of the array and check if there is any zero. And if there is, we will check for zeroes in the neighbour. And kee... | 47 | 3 | ['Depth-First Search', 'C', 'C++'] | 0 |
surrounded-regions | Java Very Easy Solution || Hard to forgot this | java-very-easy-solution-hard-to-forgot-t-57xf | Hey Programmers, i try to solve this question using DFS approach.\n\n What I did is, firstly we traverse it\'s top n bottom column\n Then it\'s left n right row | hi-malik | NORMAL | 2021-11-01T05:59:02.909632+00:00 | 2021-11-01T05:59:02.909684+00:00 | 3,050 | false | Hey Programmers, i try to solve this question using DFS approach.\n\n* What I did is, firstly we traverse it\'s top n bottom column\n* Then it\'s left n right row\n* And finally traverse its all then node\nHere\'s... | 45 | 0 | ['Java'] | 7 |
surrounded-regions | [Python] Very simple idea; DFS | python-very-simple-idea-dfs-by-jianpingb-5519 | The idea is simple:\n- Start from the boundary, and use DFS (or BFS) to flip the \'O\'s that are connected to the edge to a third symbol (e.g., "Z")\n- Scan the | jianpingbadao | NORMAL | 2021-11-01T01:31:22.214229+00:00 | 2021-11-01T01:31:54.018997+00:00 | 5,319 | false | The idea is simple:\n- Start from the boundary, and use DFS (or BFS) to flip the \'O\'s that are connected to the edge to a third symbol (e.g., "Z")\n- Scan the matrix again to flip the remaining \'O\' to \'X\', and the third symbol back to \'O\'\n\n```python\nclass Solution:\n def solve(self, board: List[List[str]]... | 39 | 2 | ['Depth-First Search', 'Python'] | 7 |
surrounded-regions | My Java O(n^2) accepted solution | my-java-on2-accepted-solution-by-frances-b0te | The idea is pretty simple: a 'O' marked cell cannot be captured whether:\n\n1. It is in contact with the border of the board or\n2. It is adjacent to an unflipp | francesco | NORMAL | 2014-12-29T16:57:54+00:00 | 2018-10-16T16:42:15.589427+00:00 | 9,778 | false | The idea is pretty simple: a 'O' marked cell cannot be captured whether:\n\n1. It is in contact with the border of the board or\n2. It is adjacent to an unflippable cell.\n\nSo the algorithm is straightforward:\n\n 1. Go around the border of the board\n 2. When a 'O' cell is found mark it with 'U' and perform a DFS on ... | 39 | 0 | [] | 6 |
surrounded-regions | Java dfs solution | java-dfs-solution-by-lencha-7uba | public void solve(char[][] board) {\n if(board == null || board.length == 0) return;\n for(int i = 0; i < board.length; i++){\n for(int | lencha | NORMAL | 2016-05-14T09:22:36+00:00 | 2016-05-14T09:22:36+00:00 | 5,275 | false | public void solve(char[][] board) {\n if(board == null || board.length == 0) return;\n for(int i = 0; i < board.length; i++){\n for(int j = 0; j < board[0].length; j++){\n if(i == 0 || i == board.length-1 || j == 0 || j == board[0].length-1){\n if(board[i][... | 36 | 2 | ['Depth-First Search'] | 6 |
surrounded-regions | python3 BFS and DFS | python3-bfs-and-dfs-by-nightybear-y6uf | below is my python3 BFS and DFS solutions\n\n\nBFS:\npython\nclass Solution:\n \'\'\'\n Time complexity : O(MXN)\n Space complexity : O(MXN) in worse c | nightybear | NORMAL | 2020-01-09T06:45:42.806154+00:00 | 2021-08-01T16:05:23.243129+00:00 | 4,295 | false | below is my python3 BFS and DFS solutions\n\n\nBFS:\n```python\nclass Solution:\n \'\'\'\n Time complexity : O(MXN)\n Space complexity : O(MXN) in worse case\n\n First, check the four border of the matrix. If there is a element is\n \'O\', alter it and all its neighbor \'O\' elements to \'N\'.\n\n The... | 32 | 0 | ['Depth-First Search', 'Breadth-First Search', 'Python', 'Python3'] | 2 |
surrounded-regions | C++ Simple and Easy-to-Understand Clean DFS Solution, Explained | c-simple-and-easy-to-understand-clean-df-mwsa | Idea:\nFirst, we want to save the Os that are not 4-directionally surrounded by Xs. So we go through all edges, DFS each O and change all its connected Os to !. | yehudisk | NORMAL | 2021-11-01T08:42:39.291888+00:00 | 2021-11-01T10:28:27.829324+00:00 | 1,614 | false | **Idea:**\nFirst, we want to save the `O`s that are not 4-directionally surrounded by `X`s. So we go through all edges, DFS each `O` and change all its connected `O`s to `!`.\nAll the remaining `O`s are 4-directionally surrounded by `X`s, so we change all of them to `X`.\nNow, we change back all `!`s to `O`s.\n\n```\nc... | 31 | 1 | ['C'] | 6 |
surrounded-regions | Simplest C++ solution possible (with comments) [Beats 99.5% solution] | simplest-c-solution-possible-with-commen-lir3 | My BFS Solution\n\nclass Solution {\npublic:\n\t// To check whether the indices are present in the matrix. [Like (-1,0) is not present]\n bool isSafe(int i, | not_a_cp_coder | NORMAL | 2020-08-13T13:24:04.913559+00:00 | 2020-08-13T13:24:04.913601+00:00 | 3,152 | false | ## **My BFS Solution**\n```\nclass Solution {\npublic:\n\t// To check whether the indices are present in the matrix. [Like (-1,0) is not present]\n bool isSafe(int i, int j, int m, int n){\n return (i>=0 && i<m && j>=0 && j<n);\n }\n\t// Checks whether the indices are present at the border of the matrix.\n... | 30 | 0 | ['Depth-First Search', 'Breadth-First Search', 'C', 'C++'] | 4 |
surrounded-regions | Concise 12ms C++ DFS solution | concise-12ms-c-dfs-solution-by-kenigma-8wit | First check all surrounding rows columns find those 'O' and their neighbors that are also 'O', make them to some other character like '1'. then traverse the who | kenigma | NORMAL | 2016-05-11T21:45:44+00:00 | 2016-05-11T21:45:44+00:00 | 5,303 | false | First check all surrounding rows columns find those 'O' and their neighbors that are also 'O', make them to some other character like '1'. then traverse the whole board, now the 'O' left need to be turned to 'X', and those marked '1' turned back to 'O'\n\nlike this:\n\n X X X X X X X X X X X X\n X ... | 29 | 0 | ['C++'] | 9 |
surrounded-regions | [Java] TC: O(M*N) | SC: O(min(M,N) | Linear Space BFS & simple DFS solutions | java-tc-omn-sc-ominmn-linear-space-bfs-s-z2ne | BFS - Iterative Solution (This is a space optimized solution as compared to DFS solution)\n\njava\n/**\n * BFS - Iterative Solution (This is a space optimized s | NarutoBaryonMode | NORMAL | 2021-11-01T10:21:27.442597+00:00 | 2021-11-02T22:41:33.434281+00:00 | 1,301 | false | **BFS - Iterative Solution (This is a space optimized solution as compared to DFS solution)**\n\n```java\n/**\n * BFS - Iterative Solution (This is a space optimized solution as compared to DFS solution)\n *\n * Start from edges and then mark all \'O\' cells that connect to \'O\' cells at edge.\n *\n * Time Complexity:... | 27 | 12 | ['Depth-First Search', 'Breadth-First Search', 'Java'] | 1 |
surrounded-regions | Java DFS with Explanations | java-dfs-with-explanations-by-gracemeng-tdmd | Click to see Union-Find Solution\n\nLogical Thinking\nWe aim to set all O\'s which doesn\'t locate at borders or connect to O at borders to X.\nWe mark all O\' | gracemeng | NORMAL | 2018-08-26T00:27:16.186549+00:00 | 2020-04-13T16:21:37.389517+00:00 | 1,851 | false | [Click to see Union-Find Solution](https://leetcode.com/problems/surrounded-regions/discuss/167165/Java-Union-Find-with-Explanations)\n\n**Logical Thinking**\nWe aim to set all O\'s which doesn\'t locate at borders or connect to O at borders to X.\nWe mark all O\'s at borders and apply DFS at each O at boarders to mar... | 27 | 0 | [] | 3 |
surrounded-regions | JavaScript Runtime: faster than 88.14% Memory Usage: less than 100.00% | javascript-runtime-faster-than-8814-memo-0zcu | \nvar solve = function(board) {\n if(board.length ==0) return null \n \n for(var i=0;i<board.length;i++){\n for(var j=0;j<board[0].length;j++){\ | miguelto | NORMAL | 2019-09-24T01:17:10.196271+00:00 | 2019-09-24T01:17:10.196325+00:00 | 3,125 | false | ```\nvar solve = function(board) {\n if(board.length ==0) return null \n \n for(var i=0;i<board.length;i++){\n for(var j=0;j<board[0].length;j++){\n if(board[i][j] == \'O\' && (i==0 || i==board.length-1 || j==0 || j==board[0].length-1)){\n dfs(board,i,j)\n }\n ... | 24 | 0 | ['Depth-First Search', 'JavaScript'] | 3 |
surrounded-regions | DFS | BFS | Easy to understand C++ | Clean code😊 | dfs-bfs-easy-to-understand-c-clean-code-y2jly | Intuition\nStore all the boundary elements which are \'O\' in a data structure like queue or stack mark all the O\'s which are connected to these O\'s and conve | Comrade-in-code | NORMAL | 2023-03-20T05:46:26.888870+00:00 | 2023-03-20T05:47:05.184175+00:00 | 3,807 | false | # Intuition\nStore all the boundary elements which are \'O\' in a data structure like queue or stack mark all the O\'s which are connected to these O\'s and convert all other O\'s to X.\n\n# Approach\nWe can use DFS as well as BFS. I am using BFS int this solution:\n1. First store position of all the O\'s that are pres... | 22 | 0 | ['Array', 'Depth-First Search', 'Breadth-First Search', 'Graph', 'C++'] | 1 |
surrounded-regions | Share my clean Java Code | share-my-clean-java-code-by-rainforestin-0frk | public class Solution {\n public void solve(char[][] board) {\n int rown = board.length;\n if (rown==0) return;\n int co | rainforestin | NORMAL | 2015-02-27T18:57:34+00:00 | 2015-02-27T18:57:34+00:00 | 5,853 | false | public class Solution {\n public void solve(char[][] board) {\n int rown = board.length;\n if (rown==0) return;\n int coln = board[0].length;\n for (int row=0; row<rown; ++row) {\n for (int col=0; col<coln; ++col) {\n if (row==0 ||... | 22 | 1 | ['Breadth-First Search', 'Queue'] | 2 |
surrounded-regions | [C++] DFS, BFS, and Union Find Solutions. | c-dfs-bfs-and-union-find-solutions-by-ar-ejmi | For DFS and BFS, we have the same idea in solving this problem.\n1. We first go through boarders to check if there is any \'O\'. \n2. If found \'O\' on boarders | arthur960304 | NORMAL | 2019-08-20T10:06:43.149173+00:00 | 2019-08-20T10:09:02.619501+00:00 | 1,588 | false | For **DFS** and **BFS**, we have the same idea in solving this problem.\n1. We first go through boarders to check if there is any `\'O\'`. \n2. If found `\'O\'` on boarders, perform DFS or BFS and make all connected `\'O\'` becomes `\'D\'`.\n3. After that, we go through whole board and mark remaining `\'O\'` as `\'X\'`... | 21 | 0 | ['Depth-First Search', 'Breadth-First Search', 'Union Find', 'C'] | 1 |
surrounded-regions | C++ | DFS | Approach explained | c-dfs-approach-explained-by-nidhi_ranjan-hmdx | Approach- We apply dfs on all Os that are on boundary and mark them as #,then we just need to traverse the matrix and flip remaining Os to X as these are the on | nidhi_ranjan | NORMAL | 2021-10-01T15:50:47.942036+00:00 | 2021-10-01T15:50:47.942079+00:00 | 1,534 | false | Approach- We apply dfs on all Os that are on boundary and mark them as #,then we just need to traverse the matrix and flip remaining Os to X as these are the ones that are completely surrounded by X. Also,we flip back the #s to Os.\n\n```\nclass Solution {\npublic:\n void dfs(int i,int j,vector<vector<char>>& board)... | 20 | 0 | ['Depth-First Search', 'Graph', 'Recursion', 'C', 'C++'] | 1 |
surrounded-regions | Simply Simple Python approach - detailed explanation | simply-simple-python-approach-detailed-e-1d7s | \tclass Solution:\n def solve(self, board: List[List[str]]) -> None:\n """\n Do not return anything, modify board in-place instead.\n "" | limitless_ | NORMAL | 2019-10-29T01:53:33.585288+00:00 | 2019-10-29T01:53:33.585323+00:00 | 2,176 | false | \tclass Solution:\n def solve(self, board: List[List[str]]) -> None:\n """\n Do not return anything, modify board in-place instead.\n """\n \n q = []\n \n r = len(board)\n if r == 0: return\n c = len(board[0])\n if c == 0: return\n \n\t\t# ... | 19 | 0 | ['Breadth-First Search', 'Python', 'Python3'] | 5 |
surrounded-regions | Python DFS solution beats 96%, 108 ms | python-dfs-solution-beats-96-108-ms-by-m-9ksg | \nclass Solution:\n def solve(self, board: List[List[str]]) -> None:\n\t\n m , n = len(board) , len(board[0]) if m>0 else 0 \n \n d | mike840609 | NORMAL | 2019-05-18T18:09:56.804529+00:00 | 2019-05-18T18:09:56.804609+00:00 | 2,110 | false | ```\nclass Solution:\n def solve(self, board: List[List[str]]) -> None:\n\t\n m , n = len(board) , len(board[0]) if m>0 else 0 \n \n def dfs(i,j): \n if board[i][j] == "O":\n board[i][j] = \'D\'\n for x , y in [(i+1,j), (... | 19 | 0 | ['Depth-First Search', 'Python'] | 4 |
surrounded-regions | Clean JavaScript solution | clean-javascript-solution-by-hongbo-miao-ugw2 | Idea\n1) Check four borders. If it is O, change it and all its neighbor to temporary #\n2) Change all O to X\n3) Change all # to O\n\nExample\n\nX X X X X | hongbo-miao | NORMAL | 2018-06-17T06:28:13.359753+00:00 | 2020-06-17T23:29:15.702151+00:00 | 1,130 | false | Idea\n1) Check four borders. If it is O, change it and all its neighbor to temporary #\n2) Change all O to X\n3) Change all # to O\n\nExample\n```\nX X X X X X X X X X X X\nX X O X -> X X O X -> X X X X\nX O X X X # X X X O X X\nX O X X X # X X X O X X\n```\n\n```js\nfunction solve(boa... | 19 | 0 | [] | 3 |
surrounded-regions | Python different simple solutions | python-different-simple-solutions-by-gyh-n2v3 | DFS or BFS:\nPhase 1: Changing every O-region cells to \'D\' which touching the border .\nPhase 2: Change every \'D\' on the board to \'O\' and everything else | gyh75520 | NORMAL | 2019-08-26T07:31:43.184127+00:00 | 2019-08-26T12:44:31.937994+00:00 | 1,524 | false | # DFS or BFS:\nPhase 1: Changing every O-region cells to \'D\' which touching the border .\nPhase 2: Change every \'D\' on the board to \'O\' and everything else to \'X\'.\n```python\n def solve(self, board: List[List[str]]) -> None:\n for i in range(len(board)):\n for j in range(len(board[0])):\n... | 16 | 0 | ['Depth-First Search', 'Breadth-First Search', 'Union Find', 'Python3'] | 1 |
surrounded-regions | Python DFS Easy solution with comments | python-dfs-easy-solution-with-comments-b-7b35 | \nclass Solution:\n \n def dfs(self,board,i,j):\n \n if i<0 or j<0 or i>=len(board) or j>=len(board[0]) or board[i][j]!=\'O\':\n | JoyRafatAshraf | NORMAL | 2020-04-01T06:58:41.977788+00:00 | 2020-06-17T08:28:51.874847+00:00 | 1,008 | false | ```\nclass Solution:\n \n def dfs(self,board,i,j):\n \n if i<0 or j<0 or i>=len(board) or j>=len(board[0]) or board[i][j]!=\'O\':\n return\n board[i][j]=\'$\' # converting to a dollar sign \n \n self.dfs(board,i+1,j)\n self.dfs(board,i-1,j)\n s... | 14 | 0 | ['Depth-First Search', 'Python', 'Python3'] | 5 |
surrounded-regions | Java 1ms Simple | java-1ms-simple-by-vikrant_pc-tewt | \nclass Solution {\n \n public void solve(char[][] board) {\n if(board.length == 0) return;\n boolean[][] visited;\n visited = new bo | vikrant_pc | NORMAL | 2020-06-17T07:19:18.972026+00:00 | 2020-06-18T06:55:33.738611+00:00 | 1,088 | false | ```\nclass Solution {\n \n public void solve(char[][] board) {\n if(board.length == 0) return;\n boolean[][] visited;\n visited = new boolean[board.length][board[0].length];\n \n for(int i=0;i<board.length;i++) { // Run dfs on left and right borders and change \'0\'s to \'#\'\... | 12 | 3 | [] | 1 |
surrounded-regions | C++ solution inspired from the game of I-go(20ms) | c-solution-inspired-from-the-game-of-i-g-2psa | I guess this question is inspired from Chinese traditional sport\u2014I-go. Here the task is to flip all the dead pieces. \n\nMy solution is common: perform BFS | thinkhygmailcom | NORMAL | 2015-01-09T14:31:06+00:00 | 2015-01-09T14:31:06+00:00 | 2,543 | false | I guess this question is inspired from Chinese traditional sport\u2014I-go. Here the task is to flip all the dead pieces. \n\nMy solution is common: perform BFS from live pieces('O') on the edges and mark the live pieces with temporary state '+'. After BFS done, scan the whole board to flip remaining 'O' (dead) cells a... | 12 | 0 | ['Breadth-First Search', 'Queue'] | 2 |
surrounded-regions | Surrounded Region | ~99% faster | surrounded-region-99-faster-by-gouravnat-2pj1 | 130. Surrounded Regions \n\n\nHi coders \uD83D\uDC4B,\n\n\uD83C\uDFC6 Runtime: 8 ms, faster than 98.69%\n Memory Usage: 10 MB, less than 79.51% \n\uD83E\u | gouravNath | NORMAL | 2021-11-01T07:20:49.954488+00:00 | 2021-11-01T13:09:58.040137+00:00 | 602 | false | <h1><b>130. Surrounded Regions</b></h1><hr>\n\n\nHi coders \uD83D\uDC4B,<br>\n\n\uD83C\uDFC6 Runtime: 8 ms, faster than **98.69%**\n Memory Usage: 10 MB, less than **79.51%** <br>\n\uD83E\uDDE0 APPROACH :\n\t\t\u25B6 Traverse the given board and replace all \u2018O\u201... | 11 | 0 | ['Depth-First Search', 'Graph', 'C++'] | 0 |
surrounded-regions | JAVA 100 % FASTER🚀🚀 || STEP BY STEP EXPLAINED😉😉 | java-100-faster-step-by-step-explained-b-cngt | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | abhiyadav05 | NORMAL | 2023-07-23T14:01:09.471549+00:00 | 2023-07-23T14:01:09.471567+00:00 | 1,212 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(n * m)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O... | 10 | 0 | ['Array', 'Depth-First Search', 'Matrix', 'Java'] | 1 |
surrounded-regions | ✔️ 100% Fastest Swift Solution | 100-fastest-swift-solution-by-sergeylesc-a2zq | \nclass Solution {\n func solve(_ board: inout [[Character]]) {\n let row = board.count\n let col = board[0].count\n var map = Array(rep | sergeyleschev | NORMAL | 2022-04-11T06:39:03.737116+00:00 | 2022-04-11T06:40:45.439586+00:00 | 426 | false | ```\nclass Solution {\n func solve(_ board: inout [[Character]]) {\n let row = board.count\n let col = board[0].count\n var map = Array(repeating: Array(repeating: 0, count: col) , count: row)\n // 1...n\n var index = 1\n var indexs: [Int] = []\n \n for i in 0... | 10 | 0 | ['Swift'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.