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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count-subarrays-with-median-k | Python short solution | Counter of difference | python-short-solution-counter-of-differe-ghty | \ndef countSubarrays(self, nums: List[int], k: int) -> int:\n\tcount, idx = Counter([0]), nums.index(k)\n\tans = d = 0\n\tfor i in range(len(nums)):\n\t\td -= ( | vincent_great | NORMAL | 2022-11-27T06:24:41.538765+00:00 | 2022-11-27T06:28:03.017606+00:00 | 47 | false | ```\ndef countSubarrays(self, nums: List[int], k: int) -> int:\n\tcount, idx = Counter([0]), nums.index(k)\n\tans = d = 0\n\tfor i in range(len(nums)):\n\t\td -= (nums[i]<k)\n\t\td += (nums[i]>k)\n\t\tif i<idx:\n\t\t\tcount[d] += 1\n\t\telse:\n\t\t\tans += (count[d]+count[d-1])\n\treturn ans\n``` | 1 | 0 | [] | 0 |
count-subarrays-with-median-k | Python3 Solution With Freq Table | python3-solution-with-freq-table-by-mota-48lu | \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. O(n) \n\n | Motaharozzaman1996 | NORMAL | 2022-11-27T04:48:27.058774+00:00 | 2022-11-27T04:48:27.058806+00:00 | 106 | false | \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. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def countSubarrays(self, nums: List[int], k: int) -> int:\n idx,freq,prefix = nums.index(k), Coun... | 1 | 0 | ['Python', 'Python3'] | 0 |
count-subarrays-with-median-k | [Java] Use Map or Array to count the number of smaller/larger items around k | java-use-map-or-array-to-count-the-numbe-325w | Intuition\nThe idea is straightforwar, we just need to find k first, and count the number of smaller/larger items around it.\n\n# Approach\n1. Find the index of | wddd | NORMAL | 2022-11-27T04:21:40.672699+00:00 | 2022-11-27T04:21:40.672739+00:00 | 372 | false | # Intuition\nThe idea is straightforwar, we just need to find k first, and count the number of smaller/larger items around it.\n\n# Approach\n1. Find the index of k\n2. Count the number of smaller/larger items on one side of k\n3. Count the number of smaller/larger items on the other side (and calculate the result at t... | 1 | 0 | ['Java'] | 0 |
count-subarrays-with-median-k | [C++] easy unordered Map | c-easy-unordered-map-by-mble6125-5mzk | Intuition\n1. All desired subarray should include k.\n2. For a desired subarray A:\n Let x be the number of element greater than k.\n Let y be the number of | mble6125 | NORMAL | 2022-11-27T04:01:38.141978+00:00 | 2022-11-27T04:01:38.142018+00:00 | 504 | false | # Intuition\n1. All desired subarray should include k.\n2. For a desired subarray A:\n Let x be the number of element greater than k.\n Let y be the number of element lower than k.\n x - y should be 0 or 1.\n\nThese two observations conclude following algorithm.\n\n# Code\n```\nclass Solution {\npublic:\n int ... | 1 | 0 | ['C++'] | 1 |
count-subarrays-with-median-k | [Java] Prefix Sum + HashMap | O(N) | Detailed Math Explanation | java-prefix-sum-hashmap-on-detailed-math-wslz | Define:\n- int pivot: where nums[pivot] == k\n- Prefix sum array smaller: where smaller[i+1] is the number of m so that nums[m] < k and 0 <= m <= i <= nums.leng | jjeeffff | NORMAL | 2022-11-27T04:01:37.026244+00:00 | 2022-11-27T04:31:52.662050+00:00 | 924 | false | Define:\n- int `pivot`: where `nums[pivot] == k`\n- Prefix sum array `smaller`: where `smaller[i+1]` is the number of `m` so that `nums[m] < k` and `0 <= m <= i <= nums.length`.\n\nGoal: count the number of `(i, j)` pairs where `0 <= i <= pivot <= j <= nums.length` and `smaller[j+1] - smaller[i] == (j - i) / 2`.\n\nLet... | 1 | 0 | ['Math', 'Prefix Sum', 'Java'] | 0 |
count-subarrays-with-median-k | [c++] O(n) | c-on-by-hujc-dzju | \nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n // for the definision of median in this problem,\n // suppose su | hujc | NORMAL | 2022-11-27T04:00:48.047557+00:00 | 2022-11-27T04:02:04.632254+00:00 | 522 | false | ```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n // for the definision of median in this problem,\n // suppose subarray A meets the condition\n \n // there are two cases\n // 1. odd number length: median(A) == k --> {x > k} - {x < k} == 0\n /... | 1 | 0 | [] | 0 |
count-subarrays-with-median-k | c++ using map | c-using-map-by-mss2-oybz | IntuitionApproachComplexity
Time complexity:O(n)
Space complexity:O(n)
Code | MSS2 | NORMAL | 2025-02-11T20:54:40.589374+00:00 | 2025-02-11T20:54:40.589374+00:00 | 7 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:$$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:$$O(n)$$
<!-- Add your space complexity here, e.g. $$O(... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | O(N) Python step-by-step solution with explanation comments | on-python-step-by-step-solution-with-exp-tbsy | Complexity
Time complexity:
O(N)
Space complexity:
O(N)
Code | nikalinov | NORMAL | 2025-02-06T11:16:44.930842+00:00 | 2025-02-06T11:23:41.250972+00:00 | 10 | false | # Complexity
- Time complexity:
O(N)
- Space complexity:
O(N)
# Code
```python3 []
class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
ind_k = nums.index(k)
# subarrays to the left of index with calculated diff between
# bigger and smaller numbers as keys
# eg ... | 0 | 0 | ['Python3'] | 0 |
count-subarrays-with-median-k | Hash table solution [EXPLAINED] | hash-table-solution-explained-by-r9n-och5 | IntuitionFind subarrays where the median is equal to a given value k. Instead of sorting every subarray, we use a balance approach to track the number of elemen | r9n | NORMAL | 2024-12-26T06:01:31.844302+00:00 | 2024-12-26T06:01:31.844302+00:00 | 5 | false | # Intuition
Find subarrays where the median is equal to a given value k. Instead of sorting every subarray, we use a balance approach to track the number of elements greater or smaller than k, which helps us efficiently calculate the median.
# Approach
I've traversed the array, adjust a balance value based on whether ... | 0 | 0 | ['Array', 'Hash Table', 'Prefix Sum', 'C#'] | 0 |
count-subarrays-with-median-k | Count Subarrays With Median K | count-subarrays-with-median-k-by-naeem_a-yszf | null | Naeem_ABD | NORMAL | 2024-12-12T04:50:24.249561+00:00 | 2024-12-12T04:50:24.249561+00:00 | 11 | 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# Keypoint\nAll numbers are distinct. So there is only one k in nums\nbalanced: if an array is balanced(count(moreK) - count(lessK) == 0 or 1) and contains k, it is a ... | 0 | 0 | ['Go'] | 0 |
count-subarrays-with-median-k | C++ | Map + Prefix sum | c-map-prefix-sum-by-kena7-58qv | \nclass Solution {\npublic:\n #define ll long long int\n int countSubarrays(vector<int>& nums, int k) \n {\n int n=nums.size(),s=-1;\n fo | kenA7 | NORMAL | 2024-12-01T10:21:27.830993+00:00 | 2024-12-01T10:21:27.831022+00:00 | 3 | false | ```\nclass Solution {\npublic:\n #define ll long long int\n int countSubarrays(vector<int>& nums, int k) \n {\n int n=nums.size(),s=-1;\n for(int i=0;i<n;i++)\n {\n if(nums[i]==k)\n {\n nums[i]=0;\n s=i;\n }\n else i... | 0 | 0 | [] | 0 |
count-subarrays-with-median-k | C++ Simple Well Commented Code : O(N) | c-simple-well-commented-code-on-by-nites-66wn | Code\ncpp []\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n= nums.size();\n\n unordered_map<int,int> um;\n | niteshkumartiwari | NORMAL | 2024-12-01T04:50:05.953683+00:00 | 2024-12-01T04:50:05.953707+00:00 | 8 | false | # Code\n```cpp []\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n= nums.size();\n\n unordered_map<int,int> um;\n um[0] = 1; // zero sum happended once.\n bool found = false;\n int sum =0;\n int result=0;\n\n for(int num : nums) {\n ... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | Java BEATS 100% | java-beats-100-by-alexander_sergeev-zsk1 | 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 | alexander_sergeev | NORMAL | 2024-10-29T17:08:00.350062+00:00 | 2024-10-29T17:08:00.350095+00:00 | 6 | 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)$$ --... | 0 | 0 | ['Java'] | 0 |
count-subarrays-with-median-k | Counting Subarrays with Median Equal to k Using Balance Map | Beats 100% ✅✅ | counting-subarrays-with-median-equal-to-xpqw4 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Balance Calculation: Traverse the array and maintain a balance variabl | Sahnawas | NORMAL | 2024-10-28T09:52:04.205408+00:00 | 2024-10-28T09:52:04.205437+00:00 | 5 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Balance Calculation: Traverse the array and maintain a balance variable:\n - Increment balance by +1 for elements greater than k.\n - Decrement balance by -1 for elements less than k.\n\n2. Tracking Prefix Balances... | 0 | 0 | ['JavaScript'] | 0 |
count-subarrays-with-median-k | Python (Simple Hashmap) | python-simple-hashmap-by-rnotappl-bc5s | 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 | rnotappl | NORMAL | 2024-10-27T08:22:13.476721+00:00 | 2024-10-27T08:22:13.476752+00:00 | 15 | 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)$$ --... | 0 | 0 | ['Python3'] | 0 |
count-subarrays-with-median-k | C++, O(n) | c-on-by-samaun37-86ty | 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 | samaun37 | NORMAL | 2024-10-20T19:34:04.527415+00:00 | 2024-10-20T19:34:04.527448+00:00 | 5 | 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)$$ --... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | Easy Map Solution | easy-map-solution-by-kvivekcodes-ecxz | 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 | kvivekcodes | NORMAL | 2024-10-09T11:27:22.050339+00:00 | 2024-10-09T11:27:22.050366+00:00 | 6 | 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)$$ --... | 0 | 0 | ['Array', 'Hash Table', 'C++'] | 0 |
count-subarrays-with-median-k | C++ Solution with T.C :- O(N) and S.C :- O(N). | c-solution-with-tc-on-and-sc-on-by-adity-elyb | \nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n=nums.size(),ind_k;\n for(int i=0;i<n;++i){\n if | adityarupda2002 | NORMAL | 2024-10-04T11:05:23.206855+00:00 | 2024-10-04T11:05:23.206880+00:00 | 2 | false | ```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n=nums.size(),ind_k;\n for(int i=0;i<n;++i){\n if(nums[i]==k) ind_k=i;\n }\n vector<int> a(n);\n a[ind_k]=0;\n for(int i=0;i<n;++i){\n if(i!=ind_k){\n i... | 0 | 0 | ['Array', 'Hash Table', 'C', 'Prefix Sum'] | 0 |
count-subarrays-with-median-k | C++ | c-by-tinachien-vb0n | \nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int,int>odd;\n unord | TinaChien | NORMAL | 2024-09-21T04:32:11.373919+00:00 | 2024-09-21T04:32:11.373952+00:00 | 1 | false | ```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int,int>odd;\n unordered_map<int,int>even;\n even[0] = 1;\n int ret = 0;\n int presum = 0;\n for(int i = 0; i < n; i++){\n if(nums[i] == k... | 0 | 0 | [] | 0 |
count-subarrays-with-median-k | Prefix sum && Hash table. O(n) Beats 100%. | prefix-sum-hash-table-on-beats-100-by-15-drfj | Intuition\n Describe your first thoughts on how to solve this problem. \n- Hint1\nFirstly, we need to tanslate the question to an easier one. \nThere are number | 1504658251 | NORMAL | 2024-08-27T00:04:56.148176+00:00 | 2024-08-27T00:41:19.864171+00:00 | 51 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Hint1\nFirstly, we need to tanslate the question to an easier one. \nThere are numbers from 1 to n, but we have a \'k\', so these numbers are in three classes which are strickly grater than k, k and strictly smaller than k.\nChange the ... | 0 | 0 | ['Array', 'Hash Table', 'Prefix Sum', 'C++', 'Go', 'Python3'] | 0 |
count-subarrays-with-median-k | most simple c++ solution just using the hashmap balancing diff on both the sides of median | most-simple-c-solution-just-using-the-ha-knh4 | 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 | Kronos_1066 | NORMAL | 2024-07-28T10:20:34.946222+00:00 | 2024-07-28T10:20:34.946255+00:00 | 14 | 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)$$ --... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | Java PrefixSum soln | java-prefixsum-soln-by-vharshal1994-sqco | 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 | vharshal1994 | NORMAL | 2024-07-26T15:23:08.240883+00:00 | 2024-07-26T15:23:08.240913+00:00 | 6 | 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)$$ --... | 0 | 0 | ['Java'] | 0 |
count-subarrays-with-median-k | Python (Simple Hashmap) | python-simple-hashmap-by-rnotappl-rxlm | 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 | rnotappl | NORMAL | 2024-07-16T10:15:16.294259+00:00 | 2024-07-16T10:15:16.294286+00:00 | 5 | 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)$$ --... | 0 | 0 | ['Python3'] | 0 |
count-subarrays-with-median-k | Optimal way to find the total no. of subarray with a given median | optimal-way-to-find-the-total-no-of-suba-4m5l | Intuition\nIn this implementation, we start by locating the index of the median (k) in the array. Then, we traverse forward and backward from this index, using | tasniatahsin | NORMAL | 2024-06-22T22:50:53.169968+00:00 | 2024-06-22T22:50:53.169987+00:00 | 6 | false | # Intuition\nIn this implementation, we start by locating the index of the median (k) in the array. Then, we traverse forward and backward from this index, using a diff variable to track differences between counts of elements greater than k and less than k. We store these differences in a map.\n# Approach\nBest Approac... | 0 | 0 | ['Array', 'Hash Table', 'JavaScript'] | 0 |
count-subarrays-with-median-k | C++ Simple || Hash Map || Commented for better understanding | c-simple-hash-map-commented-for-better-u-54t4 | 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 | rajatm17 | NORMAL | 2024-05-28T11:20:57.246758+00:00 | 2024-05-28T11:20:57.246789+00:00 | 9 | 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$$O(n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $... | 0 | 0 | ['Hash Table', 'C++'] | 0 |
count-subarrays-with-median-k | Clear Explanation | Clean Code | O(n) | Java | clear-explanation-clean-code-on-java-by-6ohpx | Intuition\n Describe your first thoughts on how to solve this problem. \nCount the elements less than k and greater than k.\nif they are equal or only differ by | f20180991 | NORMAL | 2024-05-01T11:24:37.651952+00:00 | 2024-05-01T11:24:37.651984+00:00 | 12 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCount the elements less than k and greater than k.\nif they are equal or only differ by 1 then we can say that the median is k.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nLoop through the array until you find ... | 0 | 0 | ['Java'] | 0 |
count-subarrays-with-median-k | Simple Solution | Hash Table | Counting | C++ | simple-solution-hash-table-counting-c-by-p5e4 | Intuition\n Describe your first thoughts on how to solve this problem. \nFirst thought that comes to mind is to find the median k, then expand the array to left | 131477097 | NORMAL | 2024-03-05T14:18:59.251955+00:00 | 2024-03-05T14:18:59.251989+00:00 | 11 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst thought that comes to mind is to find the median `k`, then expand the array to left and right from the position of `k` and count the number of subarrays based on the number of less and greater `k` elements. This way is not feasible ... | 0 | 0 | ['Hash Table', 'Counting', 'C++'] | 0 |
count-subarrays-with-median-k | [Java] HashMap | explanation | comment | java-hashmap-explanation-comment-by-hesi-iyf8 | Intuition\nThe crucial observation is that in even-sized arrays, the median is the left middle element, not the average of the middle two. This implies that for | hesicheng20 | NORMAL | 2024-02-13T21:37:34.803750+00:00 | 2024-02-13T21:37:34.803772+00:00 | 14 | false | # Intuition\nThe crucial observation is that in even-sized arrays, the median is the left middle element, not the average of the middle two. This implies that **for a subarray to have a median of k, it must necessarily include k**. Additionally, for k to be the median, the count of elements greater than k should equal ... | 0 | 0 | ['Java'] | 0 |
count-subarrays-with-median-k | C++ | O(N) | EASY | c-on-easy-by-goku_2022-yg67 | 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 | goku_2022 | NORMAL | 2024-01-17T11:03:56.574709+00:00 | 2024-01-17T11:03:56.574731+00:00 | 11 | 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)$$ --... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | Solution | solution-by-penrosecat-ze91 | Intuition\n Describe your first thoughts on how to solve this problem. \nFrom the definition in the problem, the median is always an element of the array. So an | penrosecat | NORMAL | 2024-01-12T13:21:30.281527+00:00 | 2024-01-12T13:21:30.281547+00:00 | 6 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFrom the definition in the problem, the median is always an element of the array. So any array where the median is k must always have the element k in it.\n\nSo we start from the single element k. Now we want to find all the subarrays con... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | Map || Checking sum of Greater and smaller on both sides | map-checking-sum-of-greater-and-smaller-m13i4 | \n\n# Code\n\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n unordered_map<int,int> mp;\n int ind = 0;\n f | szoro | NORMAL | 2023-12-15T07:23:23.956293+00:00 | 2023-12-15T07:23:23.956311+00:00 | 5 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n unordered_map<int,int> mp;\n int ind = 0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==k)ind=i;\n }\n int gs = 0,ans=0;\n for(int i=ind;i<nums.size();i++){\n ... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | Prefix and Suffix Scores to Track Numbers Greater and Smaller than k.Time: O(n), Space: O(n) | prefix-and-suffix-scores-to-track-number-fd1a | class Solution(object):\n def countSubarrays(self, nums, k):\n """\n :type nums: List[int]\n :type k: int\n :rtype: int\n | iitjsagar | NORMAL | 2023-11-10T23:43:43.734391+00:00 | 2023-11-10T23:43:43.734417+00:00 | 2 | false | class Solution(object):\n def countSubarrays(self, nums, k):\n """\n :type nums: List[int]\n :type k: int\n :rtype: int\n """\n \n prefix_dict = {0:1}\n sufix_dict = {0:1}\n n = len(nums)\n \n for i,x in enumerate(nums):\n if x =... | 0 | 0 | [] | 0 |
count-subarrays-with-median-k | Prefix sum Solution , fastest than 95 % of cpp user. | prefix-sum-solution-fastest-than-95-of-c-tc8c | 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 | leminhl1412 | NORMAL | 2023-11-08T05:49:23.788307+00:00 | 2023-11-08T05:49:23.788332+00:00 | 13 | 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)$$ --... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | O(n): Hash Map solution. | on-hash-map-solution-by-thisnameisorigin-tb29 | Intuition\n Describe your first thoughts on how to solve this problem. \nFor the number \'k\' to be median, the number of elements greater than \'k\' and less t | thisNameIsOriginal | NORMAL | 2023-08-24T05:05:55.362040+00:00 | 2023-08-24T05:05:55.362074+00:00 | 12 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFor the number \'k\' to be median, the number of elements greater than \'k\' and less than \'k\' should differ by either 0 or 1.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nPick an index to the left of the numb... | 0 | 0 | ['Hash Table', 'C#'] | 0 |
count-subarrays-with-median-k | Used vector instead of Hashmap. O(n) solution. | used-vector-instead-of-hashmap-on-soluti-h8mk | Intuition\nThe approach is same as used by many people in the solutions section. This is just a modification of that only with better complexity. So please go t | Ishita_ggn | NORMAL | 2023-08-23T17:03:20.604323+00:00 | 2023-08-23T17:03:20.604355+00:00 | 14 | false | # Intuition\nThe approach is same as used by many people in the solutions section. This is just a modification of that only with better complexity. So please go through the hashmap approach before coming here.\n\n# Approach 1 (using hashmap)\nPoint to note: For ```k``` to be the median, ```k``` must be in the subarray.... | 0 | 0 | ['Array', 'C++'] | 0 |
count-subarrays-with-median-k | Unordered Map || C++ | unordered-map-c-by-scrapernerd-qpqv | 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 | ScraperNerd | NORMAL | 2023-07-28T14:45:43.480374+00:00 | 2023-07-28T14:45:43.480400+00:00 | 24 | 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)$$ --... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | prefix sum || variant of subarray with sum k | prefix-sum-variant-of-subarray-with-sum-bsv3y | \nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n int sum=0;\n int pivot;\n int | Arbazmalik | NORMAL | 2023-07-19T12:45:06.038515+00:00 | 2023-07-19T12:45:06.038532+00:00 | 11 | false | ```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n int sum=0;\n int pivot;\n int count=0;\n map<int,int> mpp;\n mpp[0]=1;\n vector<int> arr(n,0);\n for(int i=0;i<n;i++)\n {\n if(nums[i]<k)\n ... | 0 | 0 | [] | 1 |
count-subarrays-with-median-k | C++ solution based on hints | c-solution-based-on-hints-by-vvhack-2tiw | \nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n int kIdx = -1;\n for (int i = 0; i < n; ++i) {\n | vvhack | NORMAL | 2023-07-04T21:49:42.610177+00:00 | 2023-07-04T21:49:42.610194+00:00 | 5 | false | ```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n int kIdx = -1;\n for (int i = 0; i < n; ++i) {\n if (nums[i] == k) kIdx = i;\n nums[i] = (nums[i] == k) ? 0 : ((nums[i] < k) ? -1 : 1);\n }\n vector<int> cumSums;\n vector<int> cumSumFre... | 0 | 0 | [] | 0 |
count-subarrays-with-median-k | 🔥Easy to Understand C++ Code✅ || PrefixSum + HashMap🌟💯 | easy-to-understand-c-code-prefixsum-hash-q8z0 | Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n## Please Upvote if it helps\uD83E\uDD17\n\nclass Solution {\npublic:\n int coun | aDish_21 | NORMAL | 2023-07-04T12:48:58.449310+00:00 | 2023-07-04T13:02:48.447162+00:00 | 45 | false | # Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n## Please Upvote if it helps\uD83E\uDD17\n```\nclass Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) {\n int n = nums.size(),k_ind = -1,prefixSum = 0,count = 0;\n unordered_map<int,int> mp;\n for(in... | 0 | 0 | ['Array', 'Hash Table', 'Prefix Sum', 'C++'] | 0 |
count-subarrays-with-median-k | JS clean solution with logic explained. Easy to understand. | js-clean-solution-with-logic-explained-e-rj2e | Approach\nFor a subarray to have a median of k, there are three cases:\n\n- case 1: [k] itself\n- case 2: there is equal amount of numbers either bigger or smal | bowen0110 | NORMAL | 2023-07-01T22:29:53.725959+00:00 | 2023-07-01T22:29:53.725979+00:00 | 17 | false | # Approach\nFor a subarray to have a median of k, there are three cases:\n```\n- case 1: [k] itself\n- case 2: there is equal amount of numbers either bigger or smaller than k\n ex: [k-2, k-1, k, k+1, k+2]\n- case 3: the number of values bigger than k - number of values smaller than k is 1\n ex: [k-1, k, ... | 0 | 0 | ['JavaScript'] | 0 |
count-subarrays-with-median-k | O(n) one pass easy solution | on-one-pass-easy-solution-by-iworkouther-ym17 | 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 | iworkouthere | NORMAL | 2023-06-23T05:36:06.919570+00:00 | 2023-06-23T05:36:06.919604+00:00 | 34 | 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. $... | 0 | 0 | ['Java'] | 0 |
count-subarrays-with-median-k | Very Easy || 97% || converted into subarray sum with equal K | very-easy-97-converted-into-subarray-sum-09bl | Intuition\nconverted into subarray sum with equal K\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity:\n O(n) \n\ | OMVAGHANI18 | NORMAL | 2023-06-15T05:30:39.777214+00:00 | 2023-06-15T05:30:39.777241+00:00 | 45 | false | # Intuition\nconverted into subarray sum with equal K\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n $$O(n)$$ \n\n- Space complexity:\n $$O(n)$$ \n\n# Code\n```\nclass Solution {\npublic:\nint fun(vector<int>& nums, int k)\n{\n int ans=0;\n int n=nums.si... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | [Rust] O(n) with single hashmap | rust-on-with-single-hashmap-by-tiedyedvo-7t0r | The problem uses an unusual definition of median, where the median of an even-length array is the lower middle number, not the average of the two middle numbers | tiedyedvortex | NORMAL | 2023-06-03T16:06:09.987757+00:00 | 2023-06-03T16:06:09.987781+00:00 | 6 | false | The problem uses an unusual definition of median, where the median of an even-length array is the lower middle number, not the average of the two middle numbers. \n\nAdditionally, because all array elements are distinct, there is exactly one element with value k in the array.\n\nThis means that the median of a subarray... | 0 | 0 | ['Rust'] | 0 |
count-subarrays-with-median-k | Java | Prefix Sum | O(N) | Unique Element so we can use prefix instead of Heap | java-prefix-sum-on-unique-element-so-we-qpvn2 | \n# Code\n\n/**\n *\n[- - - - - - - # - - - - - - - - - - ]\n\nbulk loading: \n > bulk load until #\n > use two heap to find the medium\nHow | evan-dayy | NORMAL | 2023-05-31T18:50:58.490068+00:00 | 2023-05-31T18:50:58.490093+00:00 | 28 | false | \n# Code\n```\n/**\n *\n[- - - - - - - # - - - - - - - - - - ]\n\nbulk loading: \n > bulk load until #\n > use two heap to find the medium\nHowever, it took N2Log(N) time, which is pretty ackward;\nwhat about we do a transformation on the array, to an array with -1, 0, 1\n 0, 1, 2, 3, 4\n [0, 0... | 0 | 0 | ['Java'] | 0 |
count-subarrays-with-median-k | Java Solution | java-solution-by-code-pk-3k5n | 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 | code-pk | NORMAL | 2023-05-23T18:26:51.430325+00:00 | 2023-05-23T18:26:51.430361+00:00 | 44 | 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. $$O... | 0 | 0 | ['Java'] | 0 |
count-subarrays-with-median-k | [JavaScript] Track difference using Hash Table | javascript-track-difference-using-hash-t-rmc4 | Approach\n- https://leetcode.com/problems/count-subarrays-with-median-k/solutions/2851940/balance/\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexit | tmohan | NORMAL | 2023-05-23T14:14:40.411740+00:00 | 2023-05-23T14:15:18.157224+00:00 | 6 | false | # Approach\n- https://leetcode.com/problems/count-subarrays-with-median-k/solutions/2851940/balance/\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nvar countSubarrays = function(nums, k) {\n const count = { 0: 1 }, kIndex = nums.findIndex((a) => a == k);\n\n for (let ... | 0 | 0 | ['Hash Table', 'JavaScript'] | 0 |
count-subarrays-with-median-k | Easy C++ Solution | easy-c-solution-by-klu_2000031799-ivur | Intuition\nIn summary, the code divides the array into two parts based on the index of the first occurrence of k. Then, it uses prefix sums and two unordered ma | klu_2000031799 | NORMAL | 2023-05-17T05:15:57.722576+00:00 | 2023-05-17T05:15:57.722612+00:00 | 35 | false | # Intuition\nIn summary, the code divides the array into two parts based on the index of the first occurrence of k. Then, it uses prefix sums and two unordered maps to efficiently count the subarrays on the left and right sides of the array with the desired sum.\n\n# Approach\nThe calculatePrefixSum function calculates... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | c++ simple prefix array | c-simple-prefix-array-by-vedantnaudiyal-p9pf | 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 | vedantnaudiyal | NORMAL | 2023-05-12T19:22:51.539344+00:00 | 2023-05-12T19:22:51.539402+00:00 | 15 | 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)$$ --... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | C++ - Letting the Standard C++ Library do ALL the work | c-letting-the-standard-c-library-do-all-nlv69 | Inspired by Vlad\'s solution, https://leetcode.com/problems/count-subarrays-with-median-k/solutions/2851940/balance/?orderBy=most_votes.\nSee his description fo | carstenh | NORMAL | 2023-05-10T05:14:34.752253+00:00 | 2023-05-10T05:14:34.752297+00:00 | 40 | false | Inspired by Vlad\'s solution, https://leetcode.com/problems/count-subarrays-with-median-k/solutions/2851940/balance/?orderBy=most_votes.\nSee his description for the theory behind.\n\nNotice **every** statement is a call to the Standard Library.\n```\nclass Solution {\npublic:\n\tint countSubarrays(std::vector<int>& nu... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | Golang : Prefix | Map | golang-prefix-map-by-harshawasthi90-joms | \npackage main\n\n\nfunc countSubarrays(nums []int, k int) int {\n\tdict := make(map[int]int, len(nums)/2)\n\tdict[0] = 1\n\n\tresult, sum, valid := 0, 0, false | harshawasthi90 | NORMAL | 2023-04-18T20:22:03.301704+00:00 | 2023-04-18T20:29:21.374985+00:00 | 31 | false | ```\npackage main\n\n\nfunc countSubarrays(nums []int, k int) int {\n\tdict := make(map[int]int, len(nums)/2)\n\tdict[0] = 1\n\n\tresult, sum, valid := 0, 0, false\n\n\tfor i := 0; i < len(nums); i++ {\n\t\tif nums[i] < k {\n\t\t\tsum--\n\t\t} else if nums[i] > k {\n\t\t\tsum++\n\t\t} else {\n\t\t\tvalid = true\n\t\t}\... | 0 | 0 | ['Go'] | 0 |
count-subarrays-with-median-k | CPP | EASY | cpp-easy-by-mahipalpatel11111-m3kt | class Solution {\npublic:\n int countSubarrays(vector& nums, int k) \n {\n unordered_mapl,r;\n int i=0;\n for(;i-1)\n {\n | mahipalpatel11111 | NORMAL | 2023-03-21T08:20:05.884978+00:00 | 2023-03-21T08:20:05.885014+00:00 | 10 | false | class Solution {\npublic:\n int countSubarrays(vector<int>& nums, int k) \n {\n unordered_map<int,int>l,r;\n int i=0;\n for(;i<nums.size();++i)\n {\n if(nums[i]==k)\n break;\n }\n if(i==nums.size())\n return 0;\n int j=i-1;\n ... | 0 | 0 | [] | 0 |
count-subarrays-with-median-k | O(n) Solution || C++ || easy explanation | on-solution-c-easy-explanation-by-sashi_-bwqh | Intuition\n Describe your first thoughts on how to solve this problem. \nIt is clear that we need to consider only those sub-array\'s which contain the given me | Sashi_sharma | NORMAL | 2023-03-18T18:01:38.169077+00:00 | 2023-03-18T18:01:38.169110+00:00 | 43 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt is clear that we need to consider only those sub-array\'s which contain the given median.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe use the Hash-map and PrifixSum technique to solve this problem.\nAt fi... | 0 | 0 | ['C++'] | 0 |
count-subarrays-with-median-k | Easy solution | easy-solution-by-seeki-pjz6 | 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 | Seeki | NORMAL | 2023-03-18T14:07:36.642872+00:00 | 2023-03-18T14:07:36.642911+00:00 | 67 | 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)$$ --... | 0 | 0 | ['Python3'] | 0 |
count-subarrays-with-median-k | Go Using Map | go-using-map-by-janardhankrishnasai-g34f | 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 | JanardhanKrishnaSai | NORMAL | 2023-03-18T07:43:25.673518+00:00 | 2023-03-18T07:43:25.673565+00:00 | 26 | 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)$$ --... | 0 | 0 | ['Go'] | 0 |
count-subarrays-with-median-k | Python, solution with explanation | python-solution-with-explanation-by-shun-h5qj | python\n\'\'\'\ntransform the numbers > k to 1, the numbers < k to -1, the numbers == k to 0,\nuse prefix sum to track the balance between the numbers > k and n | shun6096tw | NORMAL | 2023-03-14T12:59:46.448962+00:00 | 2023-03-14T12:59:46.449005+00:00 | 25 | false | ```python\n\'\'\'\ntransform the numbers > k to 1, the numbers < k to -1, the numbers == k to 0,\nuse prefix sum to track the balance between the numbers > k and numbers < k\nif we have found k, and we can count the number of subarray\nif subarray\'s length is odd, and #(number > k) == #(number < k) in subarray (the su... | 0 | 0 | ['Prefix Sum', 'Python'] | 0 |
count-subarrays-with-median-k | C++ count left and right | c-count-left-and-right-by-sanzenin_aria-y6tj | Intuition\nfor a subarray, let cnt = num of values > k - num of value < k\nto make k at medium, cnt must be 0 for odd size or 1 for even size.\nuse two map to s | sanzenin_aria | NORMAL | 2023-03-13T14:14:44.838351+00:00 | 2023-03-13T14:14:44.838395+00:00 | 58 | false | # Intuition\nfor a subarray, let cnt = num of values > k - num of value < k\nto make k at medium, cnt must be 0 for odd size or 1 for even size.\nuse two map to store the cnt of leftside and rightride subarray.\nto make k at medium, left cnt + right cnt must be 0 or 1. \n\n# Code\n```\nclass Solution {\npublic:\n in... | 0 | 0 | ['C++'] | 0 |
number-of-closed-islands | Java/C++ with picture, Number of Enclaves | javac-with-picture-number-of-enclaves-by-ysxi | Intuition\nThis is similar to 1020. Number of Enclaves. \n\n#### Approach 1: Flood Fill\n\nFirst, we need to remove all land connected to the edges using flood | votrubac | NORMAL | 2019-11-10T04:08:41.301974+00:00 | 2019-11-10T18:51:13.651433+00:00 | 41,307 | false | #### Intuition\nThis is similar to [1020. Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/discuss/265555/C%2B%2B-with-picture-DFS-and-BFS). \n\n#### Approach 1: Flood Fill\n\nFirst, we need to remove all land connected to the edges using flood fill.\n\nThen, we can count and flood-fill the remainin... | 381 | 12 | [] | 49 |
number-of-closed-islands | C++ easy DFS (faster than 99.81%, memory less than 100%) | c-easy-dfs-faster-than-9981-memory-less-akxnv | c++\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int res = 0;\n for (int i = 0; i < grid.size(); i++){\n | tatextry | NORMAL | 2020-03-21T22:06:19.535860+00:00 | 2020-03-21T22:06:19.535907+00:00 | 15,591 | false | ```c++\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int res = 0;\n for (int i = 0; i < grid.size(); i++){\n for (int j = 0; j < grid[0].size(); j++){\n if (grid[i][j] == 0){\n res += dfs(grid, i, j) ? 1 : 0;\n }... | 184 | 5 | [] | 24 |
number-of-closed-islands | ✅[Python3//C++//Java]✅ Easy and understand DFS solution(Beats 100%) | python3cjava-easy-and-understand-dfs-sol-a1ts | \n\nThe DFS function takes a position (i, j) and recursively checks its neighboring cells. If the cell is out of bounds or has a value of 1, it returns True. Ot | abdullayev_akbar | NORMAL | 2023-04-06T01:47:56.791135+00:00 | 2023-04-06T02:12:45.053507+00:00 | 16,019 | false | \n\nThe DFS function takes a position (i, j) and recursively checks its neighboring cells. If the cell is out of bounds or has a value of 1, it returns True. Otherwise, it marks the cell as visited and cont... | 154 | 2 | ['Depth-First Search', 'C++', 'Java', 'Python3'] | 4 |
number-of-closed-islands | [Java] Very Simple DFS Solution | java-very-simple-dfs-solution-by-edwin_z-0lji | \nclass Solution {\n\n int[][] dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n \n public int closedIsland(int[][] grid) {\n int res = 0;\ | edwin_z | NORMAL | 2019-11-10T04:06:22.299878+00:00 | 2019-11-10T04:10:45.965430+00:00 | 17,039 | false | ```\nclass Solution {\n\n int[][] dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n \n public int closedIsland(int[][] grid) {\n int res = 0;\n for(int i = 0; i < grid.length; i++){\n for(int j = 0; j < grid[0].length; j++){\n if(grid[i][j] == 0){\n ... | 99 | 6 | ['Depth-First Search', 'Java'] | 17 |
number-of-closed-islands | Python easy understand DFS solution | python-easy-understand-dfs-solution-by-t-7267 | \nclass Solution(object):\n def closedIsland(self, grid):\n """\n :type grid: List[List[int]]\n :rtype: int\n """\n if not | too-young-too-naive | NORMAL | 2019-11-10T04:03:47.123566+00:00 | 2019-11-10T04:04:25.822362+00:00 | 14,901 | false | ```\nclass Solution(object):\n def closedIsland(self, grid):\n """\n :type grid: List[List[int]]\n :rtype: int\n """\n if not grid or not grid[0]:\n return 0\n \n m, n = len(grid), len(grid[0])\n \n def dfs(i, j, val):\n if 0<=i<m a... | 97 | 6 | [] | 17 |
number-of-closed-islands | [Java/Python 3] DFS, BFS and Union Find codes w/ brief explanation and analysis. | javapython-3-dfs-bfs-and-union-find-code-zc6q | DFS\n1. Traverse grid, for each 0, do DFS to check if it is a closed island;\n2. Within each DFS, if the current cell is out of the boundary of grid, return 0; | rock | NORMAL | 2019-11-11T07:20:26.186510+00:00 | 2019-11-17T17:04:09.169154+00:00 | 8,662 | false | **DFS**\n1. Traverse `grid`, for each `0`, do DFS to check if it is a closed island;\n2. Within each DFS, if the current cell is out of the boundary of `grid`, return `0`; if the current cell value is positive, return `1`; otherwise, it is `0`, change it to `2` then recurse to its 4 neighors and return the **multificat... | 75 | 3 | [] | 13 |
number-of-closed-islands | Image Explanation🏆- [Clean - "Generalized" Code] - C++/Java/Python | image-explanation-clean-generalized-code-1h5s | Video Solution (Aryan Mittal) - Link in LeetCode Profile\nNumber of Closed Islands by Aryan Mittal\n\n\n\n# Approach & Intution\n\n\n\n\n\n\n\n\n# Generalized C | aryan_0077 | NORMAL | 2023-04-06T01:16:37.174543+00:00 | 2023-04-06T05:23:21.398096+00:00 | 14,810 | false | # Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`Number of Closed Islands` by `Aryan Mittal`\n\n\n\n# Approach & Intution\n`, we can perform a depth-first search to check if it forms a closed island, i.e., all adjacent land is surrounded by ... | 56 | 1 | ['Array', 'Depth-First Search', 'Python', 'C++', 'Java'] | 2 |
number-of-closed-islands | [Python3] Easy DFS one pass SHORT solution | python3-easy-dfs-one-pass-short-solution-uros | If you traverse and hit the border, this is not an island so don\'t add 1.\n\n\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n | localhostghost | NORMAL | 2019-11-14T15:41:48.838772+00:00 | 2019-11-14T15:51:17.634321+00:00 | 3,963 | false | If you traverse and hit the border, this is not an island so don\'t add 1.\n\n```\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n m, n = len(grid), len(grid[0])\n res = 0\n \n def dfs(x, y):\n if x in (0, m-1) or y in (0, n-1):\n self.i... | 40 | 0 | [] | 3 |
number-of-closed-islands | JAVA | Simple DFS solution | 100% Runtime | java-simple-dfs-solution-100-runtime-by-jpg2n | The goal is to count the number of time the dfs function is called, this will be the number of islands that exits.\nSimilar to this problem: https://leetcode.co | suraj008 | NORMAL | 2020-06-15T17:41:40.730664+00:00 | 2020-06-15T17:48:18.745188+00:00 | 3,711 | false | The goal is to count the number of time the dfs function is called, this will be the number of islands that exits.\nSimilar to this problem: https://leetcode.com/problems/number-of-islands/.\n\nThe catch here is that if the island of 0\'s is connected to a boundary 0. i.e i =0 || j=0 || i=length-1 || j=length-1 that i... | 34 | 0 | ['Depth-First Search', 'Java'] | 7 |
number-of-closed-islands | clean C++ solution using DFS | clean-c-solution-using-dfs-by-sky97-ips7 | We are not considering edge connected zeroes for solution so first do DFS for those zeroes and assign the value -1 so at time of counting it would not be consid | sky97 | NORMAL | 2020-07-29T08:32:23.468143+00:00 | 2020-07-29T08:32:23.468189+00:00 | 2,733 | false | We are not considering edge connected zeroes for solution so first do DFS for those zeroes and assign the value -1 so at time of counting it would not be consider for solution.\n```\nclass Solution {\npublic:\n int r , c;\n \n void dfs(vector<vector<int>>& grid , int i , int j){\n if(i < 0 || j < 0 || j... | 33 | 1 | [] | 3 |
number-of-closed-islands | Detailed Explanation using Terminal Nodes | detailed-explanation-using-terminal-node-7gw9 | Intuition\nThe idea is simple. We want to perform a DFS on the matrix to find the connected component of 0s. Call a connnected component valid if none of its e | just__a__visitor | NORMAL | 2019-11-10T04:03:35.247598+00:00 | 2019-11-11T08:20:19.424440+00:00 | 2,468 | false | # Intuition\nThe idea is simple. We want to perform a DFS on the matrix to find the connected component of `0s`. Call a connnected component valid if none of its element lies on the border of the matrix. It is clear that we need to find all the valid connected component.\n\n* We consider a cell with value `1` as a bl... | 29 | 2 | [] | 2 |
number-of-closed-islands | ✅ [C++] Easy Intuitive Solution using DFS | Explained | c-easy-intuitive-solution-using-dfs-expl-3lak | This problem is easy. You just need to figure out the fact that any island will be "surrounded" if it can\'t reach the boundary. Let me explain. \nSo we know th | biT_Legion | NORMAL | 2022-05-18T09:16:12.625558+00:00 | 2022-05-18T09:16:12.625595+00:00 | 2,574 | false | This problem is easy. You just need to figure out the fact that any island will be "surrounded" if it can\'t reach the boundary. Let me explain. \nSo we know that once we start DFS from any cell, it will move in all four directions provided they are `unvisited` and have a `0`, and it will stop if it meets an `unvisited... | 25 | 0 | ['Depth-First Search', 'C', 'C++'] | 4 |
number-of-closed-islands | ✅✅Python🔥Java🔥C++🔥Simple Solution🔥Easy to Understand🔥 | pythonjavacsimple-solutioneasy-to-unders-8ll7 | Please UPVOTE \uD83D\uDC4D\n\n!! BIG ANNOUNCEMENT !!\nI am currently Giving away my premium content well-structured assignments and study materials to clear int | cohesk | NORMAL | 2023-04-06T00:06:24.179812+00:00 | 2023-04-06T00:06:24.179855+00:00 | 4,239 | false | # Please UPVOTE \uD83D\uDC4D\n\n**!! BIG ANNOUNCEMENT !!**\nI am currently Giving away my premium content well-structured assignments and study materials to clear interviews at top companies related to computer science and data science to my current Subscribers this week. I planned to give for next 10,000 Subscribers a... | 24 | 1 | ['Array', 'Depth-First Search', 'C++', 'Java', 'Python3'] | 1 |
number-of-closed-islands | [Python] 2 solutions: DFS, BFS | python-2-solutions-dfs-bfs-by-hiepit-801u | Idea\n- The closed island is the island which doesn\'t touch the bound of grid.\n\n\u2714\uFE0F Solution 1: DFS\npython\nclass Solution:\n def closedIsland(s | hiepit | NORMAL | 2019-11-10T04:57:54.722313+00:00 | 2022-07-02T18:52:34.852022+00:00 | 1,676 | false | **Idea**\n- The closed island is the island which doesn\'t touch the bound of grid.\n\n**\u2714\uFE0F Solution 1: DFS**\n```python\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n m, n = len(grid), len(grid[0])\n DIR = [0, 1, 0, -1, 0]\n \n def dfs(r, c):\n ... | 21 | 2 | [] | 2 |
number-of-closed-islands | ✅ Fastest C++ | Python | Java Solution -Runtime: 3 ms,faster than 100% | fastest-c-python-java-solution-runtime-3-sc9i | SOLUTION\n\n\n\nEXPLANATION\n\n The function takes a 2D vector of integers as input, representing the grid.\n It initializes a counter variable c to 0.\n For ea | codermal7 | NORMAL | 2023-04-06T06:10:37.418837+00:00 | 2023-04-06T06:15:26.207890+00:00 | 1,156 | false | **SOLUTION**\n\n<iframe src="https://leetcode.com/playground/E6GNYD2g/shared" frameBorder="0" width="800" height="700"></iframe>\n\n**EXPLANATION**\n\n* The function takes a 2D vector of integers as input, representing the grid.\n* It initializes a counter variable c to 0.\n* For each element in the grid, it checks if ... | 20 | 1 | ['Depth-First Search', 'Recursion', 'C', 'Matrix', 'Python', 'C++', 'Java'] | 3 |
number-of-closed-islands | 🐍 DFS || Well-explained || 93% faster || | dfs-well-explained-93-faster-by-abhi9rai-emb7 | IDEA :\n Till we are founding "0" we can go through dfs and check if that island is surrounded by water or not.\n Up, down, left and right if all comes True the | abhi9Rai | NORMAL | 2021-06-04T05:51:12.975676+00:00 | 2021-06-04T05:52:54.772604+00:00 | 1,979 | false | ## IDEA :\n* Till we are founding "0" we can go through dfs and check if that island is surrounded by water or not.\n* Up, down, left and right if all comes True then increment the counter by 1.\n\n\'\'\'\n\n\tclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n \n def dfs(i,j):\n ... | 20 | 0 | ['Python', 'Python3'] | 4 |
number-of-closed-islands | ✅ [C++]Easy solution with explanation (With and Without Visited Array)✅ | ceasy-solution-with-explanation-with-and-yewn | Intuition\nThe boundaries that have 0 will never be an answer as they can\'t be surrounded. Hence 0\'s connected to these 0 will also not be answer as there wil | upadhyayabhi0107 | NORMAL | 2023-04-06T02:57:33.543803+00:00 | 2023-04-06T02:57:33.543851+00:00 | 2,751 | false | # Intuition\nThe boundaries that have 0 will never be an answer as they can\'t be surrounded. Hence 0\'s connected to these 0 will also not be answer as there will be an oppening.\n\n# Approach\nWe simply neglect the outter 0\'s and their connected parts.\n\n1. Traverse over the boundaries , if you find any 0\'s just d... | 15 | 0 | ['Depth-First Search', 'Recursion', 'C++'] | 0 |
number-of-closed-islands | [C++] Fill boundaries then count islands - Beats 100% | c-fill-boundaries-then-count-islands-bea-56q6 | \nclass Solution {\n void util(vector<vector<int>>& grid, int i, int j) {\n if(i<0 || j<0 || i>=grid.size() || j>=grid[0].size() || grid[i][j])\n | rexagod | NORMAL | 2020-11-13T03:25:28.338274+00:00 | 2020-11-13T03:25:28.338315+00:00 | 2,031 | false | ```\nclass Solution {\n void util(vector<vector<int>>& grid, int i, int j) {\n if(i<0 || j<0 || i>=grid.size() || j>=grid[0].size() || grid[i][j])\n return;\n grid[i][j] = 1;\n util(grid,i-1,j);\n util(grid,i,j-1);\n util(grid,i+1,j);\n util(grid,i,j+1);\n }\np... | 15 | 0 | ['Depth-First Search', 'C'] | 4 |
number-of-closed-islands | ✅C++ || EASY BFS | c-easy-bfs-by-chiikuu-ibcg | Code\n\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& g) {\n queue<pair<int,int>>q;\n int n=g.size(),m=g[0].size();\n | CHIIKUU | NORMAL | 2023-04-06T08:29:44.955826+00:00 | 2023-04-06T08:30:43.757708+00:00 | 2,193 | false | # Code\n```\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& g) {\n queue<pair<int,int>>q;\n int n=g.size(),m=g[0].size();\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(i==0 || i==n-1 || j==0 || j==m-1){\n if(g[i][j]==0){\n ... | 12 | 1 | ['Breadth-First Search', 'Matrix', 'C++'] | 0 |
number-of-closed-islands | JAVA || Easy Solution || 100% Faster Code || DFS || Detailed Explanation | java-easy-solution-100-faster-code-dfs-d-skgd | \n\n\n# Code\nPLEASE UPVOTE IF YOU LIKE.\n\nclass Solution {\n \n public int closedIsland(int[][] grid) {\n\n /*\n * Base Condition :\n | shivrastogi | NORMAL | 2023-04-06T01:03:28.349259+00:00 | 2023-04-06T01:03:28.349294+00:00 | 2,097 | false | \n\n\n# Code\nPLEASE UPVOTE IF YOU LIKE.\n```\nclass Solution {\n \n public int closedIsland(int[][] grid) {\n\n /*\n * Base Condition :\n * If row or column length is less than 3 all the values will be somehow connected to perimeter.\n * So there can\'t be a island.\n */\n ... | 11 | 0 | ['Java'] | 4 |
number-of-closed-islands | Easy Approach 🔥 Simple DFS with edge cases🔥 Recursion | easy-approach-simple-dfs-with-edge-cases-afo7 | PLEASE UPVOTE \uD83D\uDC4D\n\n# Approach\n### Simple dfs algorithm is used which determines the number of island surrounded by water but we have to take care of | ribhav_32 | NORMAL | 2023-04-06T00:21:16.256612+00:00 | 2023-04-06T00:21:16.256647+00:00 | 785 | false | # **PLEASE UPVOTE \uD83D\uDC4D**\n\n# Approach\n### *Simple dfs algorithm is used which determines the number of island surrounded by water but we have to take care of island at the edges*\n### *that\'s why we have to first use dfs over all the island at the edges*\n# Code\n```\nclass Solution {\npublic:\n int n , m... | 11 | 1 | ['Array', 'Depth-First Search', 'Graph', 'Recursion', 'C++'] | 1 |
number-of-closed-islands | 💯C++ ✅ DFS 💥 easy solution with comment | c-dfs-easy-solution-with-comment-by-sona-gxy1 | \n\n# Code\n\nclass Solution {\npublic: int dx[4]={0,0,-1,1};\n int dy[4]={1,-1,0,0};\n/* boundry check kar rhe hai isvalid function ke through ,new x ,n | sonal91022 | NORMAL | 2023-01-23T17:23:19.411243+00:00 | 2023-02-02T17:05:34.561150+00:00 | 751 | false | \n\n# Code\n```\nclass Solution {\npublic: int dx[4]={0,0,-1,1};\n int dy[4]={1,-1,0,0};\n/* boundry check kar rhe hai isvalid function ke through ,new x ,new y grid ke andr hona chiye aur grid[newx][newy] 0 hona chiye \n bool isvalid(int i,int j,int r,int c,vector<vector<int>>& grid){\n if(i>... | 11 | 0 | ['C++'] | 1 |
number-of-closed-islands | Very Simple Solution with FLOOD FILL | very-simple-solution-with-flood-fill-by-b2bke | \nvar closedIsland = function(grid) {\n let count = 0;\n let row = grid.length;\n let column = grid[0].length;\n for(let i = 0; i<grid.length; i++ | deleted_user | NORMAL | 2022-04-23T12:08:38.260689+00:00 | 2022-04-23T12:08:38.260733+00:00 | 771 | false | ```\nvar closedIsland = function(grid) {\n let count = 0;\n let row = grid.length;\n let column = grid[0].length;\n for(let i = 0; i<grid.length; i++){\n\n for(let j=0; j<grid[0].length; j++){\n if (grid[i][j] == 0){\n if (helper(i,j))count++;\n }\n }\n ... | 11 | 0 | ['Depth-First Search', 'Graph', 'JavaScript'] | 1 |
number-of-closed-islands | [python] Logic Explained, DFS solution | python-logic-explained-dfs-solution-by-1-g2cv | \nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n ## RC ##\n ## APPROACH : DFS ##\n ## LOGIC ##\n ## 1. | 101leetcode | NORMAL | 2020-06-01T22:42:49.823854+00:00 | 2020-06-01T22:42:49.823902+00:00 | 1,845 | false | ```\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n ## RC ##\n ## APPROACH : DFS ##\n ## LOGIC ##\n ## 1. Find, Islands just like normal Leetcode 200. Number of Islands problem.\n ## 2. While doing so, check if every land block, is not in the border.\n ... | 11 | 1 | ['Depth-First Search', 'Python', 'Python3'] | 3 |
number-of-closed-islands | Python DFS | python-dfs-by-rooch-t1fd | python\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n if len(grid) == 0: return 0\n def help(grid,i,j):\n | rooch | NORMAL | 2020-02-01T13:54:03.982098+00:00 | 2020-02-01T13:54:03.982131+00:00 | 1,188 | false | ```python\nclass Solution:\n def closedIsland(self, grid: List[List[int]]) -> int:\n if len(grid) == 0: return 0\n def help(grid,i,j):\n if i < 0 or j < 0 or i > len(grid)-1 or j > len(grid[0]) -1: return False\n if grid[i][j] == 1: return True\n grid[i][j] = 1\n ... | 11 | 0 | [] | 4 |
number-of-closed-islands | C++ , DFS , No of islands | c-dfs-no-of-islands-by-baman9451-ofci | 12 ms\n9.5 MB\n```\nclass Solution {\n public:\n \n void dfs(vector>& grid, int i, int j) { // making everthing water , every node connected will be | baman9451 | NORMAL | 2021-05-22T09:37:45.795284+00:00 | 2021-05-22T09:37:45.795330+00:00 | 1,076 | false | 12 ms\n9.5 MB\n```\nclass Solution {\n public:\n \n void dfs(vector<vector<int>>& grid, int i, int j) { // making everthing water , every node connected will become water\n \n if(i<0 || j<0 || i>=grid.size() || j>=grid[0].size() || grid[i][j])\n return;\n \n grid[i][j]... | 10 | 1 | ['Depth-First Search', 'C'] | 2 |
number-of-closed-islands | ✔💯 DAY 371 | [JAVA/C++/PYTHON] | EXPLAINED |INTUITION | Approach | PROOF | day-371-javacpython-explained-intuition-pgcyh | \n\n\n# Please Upvote as it really motivates me\n# Intuition \n##### \u2022\tAn island is formed by connecting all of the \'0s\' in all four directions (left, t | ManojKumarPatnaik | NORMAL | 2023-04-06T04:40:19.087664+00:00 | 2023-04-06T13:39:44.178798+00:00 | 426 | false | \n\n\n# Please Upvote as it really motivates me\n# Intuition \n##### \u2022\tAn island is formed by connecting all of the \'0s\' in all four directions (left, top, right, and bottom), which leads us to mode... | 9 | 0 | ['Union Find', 'Python', 'C++', 'Java', 'Python3'] | 0 |
number-of-closed-islands | ✅ C++ || DFS || Easy to understand | c-dfs-easy-to-understand-by-indresh149-lo0a | \nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int res = 0;\n for (int i = 0; i < grid.size(); i++){\n | indresh149 | NORMAL | 2022-09-02T11:54:29.967982+00:00 | 2022-09-02T11:54:29.968013+00:00 | 661 | false | ```\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int res = 0;\n for (int i = 0; i < grid.size(); i++){\n for (int j = 0; j < grid[0].size(); j++){\n if (grid[i][j] == 0){\n res += dfs(grid, i, j) ? 1 : 0;\n }\n ... | 9 | 0 | ['Depth-First Search', 'Graph', 'C'] | 2 |
number-of-closed-islands | [C++] 2 approaches | DFS | BFS | Easy to understand | c-2-approaches-dfs-bfs-easy-to-understan-hbbz | The idea is at first we traverse through the boundary of the grid and on seeing any 0 (land), we apply BFS or DFS, and visit all the unvisited 0s which we can r | sk__22 | NORMAL | 2022-01-20T09:13:56.838142+00:00 | 2022-01-20T09:32:11.595793+00:00 | 731 | false | The idea is at first we traverse through the boundary of the grid and on seeing any 0 (land), we apply BFS or DFS, and visit all the unvisited 0s which we can reach. These 0s will never be a part of any closed island. \n\nThen we traverse the cells which are not on boundary, if any cell is 0 (land) and not visited, we ... | 9 | 0 | ['Depth-First Search', 'Breadth-First Search', 'Graph', 'Recursion', 'C'] | 3 |
number-of-closed-islands | [Java] DFS | 100% Faster | Clean Code | java-dfs-100-faster-clean-code-by-akbero-lrgv | \nclass Solution {\n public int closedIsland(int[][] grid) {\n \n for(int i = 0; i < grid.length; ++i) {\n if(grid[i][0] == 0) dfs(g | akberovr | NORMAL | 2021-01-21T11:13:58.752688+00:00 | 2021-01-21T11:13:58.752716+00:00 | 1,013 | false | ```\nclass Solution {\n public int closedIsland(int[][] grid) {\n \n for(int i = 0; i < grid.length; ++i) {\n if(grid[i][0] == 0) dfs(grid,i,0);\n if(grid[i][grid[0].length - 1] == 0) dfs(grid,i,grid[0].length - 1);\n }\n ... | 9 | 0 | ['Java'] | 2 |
number-of-closed-islands | Java || DFS solution with comments | java-dfs-solution-with-comments-by-kashu-zxld | class Solution {\n \n public int closedIsland(int[][] grid) {\n int num = 0;\n\t\t\n/ \nDFS functions is used twice\n\t1. To label nodes which can r | kashup12 | NORMAL | 2020-10-05T04:57:24.557649+00:00 | 2020-10-05T04:59:18.205500+00:00 | 825 | false | class Solution {\n \n public int closedIsland(int[][] grid) {\n int num = 0;\n\t\t\n/* \nDFS functions is used twice\n*\t1. To label nodes which can reach to boundary\n*\t2. To count all closed islands.\n\t\n*/ \n\t// Calling DFS to label all other nodes which are connected by boundaries\n\t\n for(i... | 9 | 0 | [] | 1 |
number-of-closed-islands | Easy method | easy-method-by-ashish_49-egbs | \n# Approach\nmake all boundary lands to water and after that traversal a simle dfs \n\n\n\n# Code\n\nclass Solution {\npublic:\n void dfs(int rw,int cl,vect | Ashish_49 | NORMAL | 2023-04-06T18:00:01.452901+00:00 | 2023-04-06T18:00:01.452946+00:00 | 37 | false | \n# Approach\nmake all boundary lands to water and after that traversal a simle dfs \n\n\n\n# Code\n```\nclass Solution {\npublic:\n void dfs(int rw,int cl,vector<vector<int>>&vis,vector<vector<int>>& gd,int ar[],int ac[]){\n vis[rw][cl]=1;\n int n=gd.size();\n int m=gd[0].size();\n for(int i=0;i<4;i++){\n ... | 8 | 0 | ['C++'] | 0 |
number-of-closed-islands | ✅ Java | Easy DFS Solution Explained 📝 | Beginner friendly | Beats 100% | java-easy-dfs-solution-explained-beginne-fy5e | Intuition\nSince we need to find an island is close or not(that is surrounded by water), this would involve exploring all 4 directions so DFS comes to mind\n\nH | nikeMafia | NORMAL | 2023-04-06T03:08:53.628402+00:00 | 2023-05-01T06:32:50.357407+00:00 | 898 | false | # Intuition\nSince we need to find an island is close or not(that is surrounded by water), this would involve exploring all 4 directions so DFS comes to mind\n\nHighly suggest solving this question before looking at the solution if you haven\'t already\nLC 200 : [Number of Islands](https://leetcode.com/problems/number-... | 8 | 2 | ['Depth-First Search', 'Java'] | 0 |
number-of-closed-islands | Clean & simple code || C++ || Explained | clean-simple-code-c-explained-by-amankat-x4uw | \n# Approach\nfirst of we will try to visit all the land part (zeros) which are at the edges or connected to edegs of our square , because these island could no | amankatiyar783597 | NORMAL | 2023-04-06T02:25:54.751124+00:00 | 2023-04-06T02:25:54.751157+00:00 | 2,238 | false | \n# Approach\nfirst of we will try to visit all the land part (zeros) which are at the edges or connected to edegs of our square , because these island could not be part of answer and , we will mark them as visted so that we dot not need to visit when we will be collecting answer.\n\n`This piece of code will mark all e... | 8 | 1 | ['C++'] | 0 |
number-of-closed-islands | C++ | easy explanation | DFS | Striver style | | c-easy-explanation-dfs-striver-style-by-wvynp | This probelm is similar to number of enclaves sum (no.1020). We will be performing the DFS across the boundaries as we know the boundary lands cannot form a clo | avinashyerra | NORMAL | 2023-04-06T09:09:50.145496+00:00 | 2023-04-06T09:09:50.145540+00:00 | 921 | false | This probelm is similar to number of enclaves sum (no.1020). We will be performing the DFS across the boundaries as we know the boundary lands cannot form a closed island, we will maintain a visited vector and mark the boundary zeroes as visited , after marking them as visited the problem reduces to count the number of... | 7 | 0 | ['Depth-First Search', 'Recursion', 'C'] | 1 |
number-of-closed-islands | ✅✅ Easy C++ Solution | Beginner friendly | DFS | Recursion | Striver | Clean Codes | easy-c-solution-beginner-friendly-dfs-re-si05 | Intuition\nThe Islands which by any means are NOT connected to boundaries(first row, last row, first coloumn, last coloumn) are our answer.\n Describe your firs | warrior0331 | NORMAL | 2023-04-06T09:02:33.300699+00:00 | 2023-04-06T10:43:41.802819+00:00 | 1,179 | false | # Intuition\nThe Islands which by any means are **NOT** connected to boundaries(first row, last row, first coloumn, last coloumn) are our answer.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nBasically we have to find the number of connected components(land) which are not connected... | 7 | 1 | ['C++'] | 0 |
number-of-closed-islands | Easy to understand solution with comments | easy-to-understand-solution-with-comment-tsnu | JavaScript Solution\n\n// Set constants for the different types of cells in the grid\nconst land = 0;\nconst water = 1;\nconst visitedLand = 2;\n\n// Set up the | emmakuen | NORMAL | 2023-04-06T04:19:07.656676+00:00 | 2023-04-06T04:19:07.656724+00:00 | 973 | false | ## JavaScript Solution\n```\n// Set constants for the different types of cells in the grid\nconst land = 0;\nconst water = 1;\nconst visitedLand = 2;\n\n// Set up the four possible directions to move from a given cell\nconst directions = [\n [1, 0], // Down\n [0, 1], // Right\n [-1, 0], // Up\n [0, -1], // Left\n];... | 7 | 1 | ['Array', 'Breadth-First Search', 'Queue', 'JavaScript'] | 1 |
number-of-closed-islands | C++ HIGHLY READABLE & COMMENTED CODE | c-highly-readable-commented-code-by-sm32-pnqa | PLZ UPVOTE IF YOU LIKEF IT\n\n```\nclass Solution {\npublic:\n \n void dfs(vector>& grid,int i,int j){\n \n // Just the normal limits check | sm3210 | NORMAL | 2021-07-27T10:49:58.208959+00:00 | 2021-07-27T10:49:58.208990+00:00 | 585 | false | **PLZ UPVOTE IF YOU LIKEF IT**\n\n```\nclass Solution {\npublic:\n \n void dfs(vector<vector<int>>& grid,int i,int j){\n \n // Just the normal limits check and condition check \n if(i<0 || j<0 || i>=grid.size() || j>=grid[0].size() || grid[i][j]==1)\n return;\n \n //... | 7 | 1 | ['C', 'C++'] | 0 |
number-of-closed-islands | Java DFS 100% | java-dfs-100-by-rchanana-98ez | \n\tclass Solution {\n\t\tpublic int closedIsland(int[][] grid) {\n\t\t\tif (grid.length == 0) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tint numIslands = 0;\n\t\t\tf | rchanana | NORMAL | 2020-01-14T22:12:22.777354+00:00 | 2020-01-14T22:12:46.035428+00:00 | 803 | false | \n\tclass Solution {\n\t\tpublic int closedIsland(int[][] grid) {\n\t\t\tif (grid.length == 0) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tint numIslands = 0;\n\t\t\tfor (int i = 0 ; i < grid.length; i++) {\n\t\t\t\tfor (int j = 0; j < grid[0].length; j++) {\n\t\t\t\t\tif(grid[i][j] == 0 && isClosedIsland(grid, i,j)) {\n\t\t\... | 7 | 0 | [] | 2 |
number-of-closed-islands | EASY DFS CODE... ✅ beginner friendly, C++ | easy-dfs-code-beginner-friendly-c-by-xia-vgla | Approach\nhere we apply dfs to each element of grid[i][j].\nif grid is land then only apply dfs\nand cover it with any unique number as i chosed 2313+((200 * j) | XIAO-summer | NORMAL | 2023-04-06T19:26:48.020742+00:00 | 2023-04-06T19:26:48.020776+00:00 | 76 | false | # Approach\nhere we apply dfs to each element of grid[i][j].\nif grid is land then only apply dfs\nand cover it with any unique number as i chosed 2313+((200 * j) + (i))).\nand continue our approach of applying dfs.\neverytime we approach a new entity we updated our count, as taken by flag.\nAnd fiinally we return our ... | 6 | 0 | ['C++'] | 4 |
number-of-closed-islands | Java solution using BFS and DFS with explanation | java-solution-using-bfs-and-dfs-with-exp-f0ip | \nclass Solution {\n public int closedIsland(int[][] grid) {\n int totalRows= grid.length;\n int totalCols= grid[0].length;\n int ans=0; | cherry-singla | NORMAL | 2022-08-25T13:55:52.999420+00:00 | 2022-08-25T13:55:52.999461+00:00 | 649 | false | ```\nclass Solution {\n public int closedIsland(int[][] grid) {\n int totalRows= grid.length;\n int totalCols= grid[0].length;\n int ans=0;\n \n /* \n 1. Iterate over the borders, and see if any island starts from the border itself.\n 2. If starts from border(i.e. cel... | 6 | 0 | ['Depth-First Search', 'Breadth-First Search', 'Recursion', 'Java'] | 0 |
number-of-closed-islands | Easy Javascript solution using DFS | easy-javascript-solution-using-dfs-by-vi-nuz8 | \nvar closedIsland = function(grid) {\n let m=grid.length;\n let n=grid[0].length;\n \n let count=0;\n \n function dfs(x,y,grid){\n if( | vishwanath1 | NORMAL | 2022-02-27T15:16:49.027855+00:00 | 2022-02-27T15:16:49.027888+00:00 | 760 | false | ```\nvar closedIsland = function(grid) {\n let m=grid.length;\n let n=grid[0].length;\n \n let count=0;\n \n function dfs(x,y,grid){\n if(x<0 || x>=grid.length || y<0 || y>=grid[0].length || grid[x][y]==1){\n return;\n }\n \n grid[x][y]=1;\n dfs(x+1,y,grid... | 6 | 0 | ['Depth-First Search', 'JavaScript'] | 1 |
number-of-closed-islands | c++ BFS | c-bfs-by-zhuxiongwei24-hvwj | \n\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int result=0;\n if(grid.empty() || grid[0].empty())\n | zhuxiongwei24 | NORMAL | 2019-11-23T09:21:44.637294+00:00 | 2019-11-23T09:21:44.637336+00:00 | 404 | false | ```\n\nclass Solution {\npublic:\n int closedIsland(vector<vector<int>>& grid) {\n int result=0;\n if(grid.empty() || grid[0].empty())\n return result;\n vector<vector<bool>> visited(grid.size(),vector<bool>(grid[0].size(),false));\n vector<pair<int,int>> directions={{-1,0},{1,... | 6 | 0 | [] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.