Skip to content

Commit af63f31

Browse files
author
kaidi
committed
update 200
1 parent 0eeaa6a commit af63f31

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def numIslands(self, grid: List[List[str]]) -> int:
3+
if not grid:
4+
return 0
5+
count = 0
6+
7+
for i in range(len(grid)):
8+
for j in range(len(grid[0])):
9+
if grid[i][j] == "1":
10+
self.dfs(grid, i, j)
11+
count +=1
12+
return count
13+
14+
def dfs(self,grid, i,j):
15+
if i < 0 or j < 0 or i > len(grid)-1 or j > len(grid[0])-1 or grid[i][j]!="1":
16+
return
17+
grid[i][j] = '5'
18+
self.dfs(grid,i-1,j)
19+
self.dfs(grid,i+1,j)
20+
self.dfs(grid,i,j-1)
21+
self.dfs(grid,i,j+1)
22+
23+
# Iterate all cells, for each cell, do a DFS
24+
# The exit situation of the DFS is when index is out of bound, or the current cell is not an island
25+
# Otherwise, if the current cell is still "1", change it to "5" (used as a flag, meaning this cell has been visited)

Hashing/1512. Number of Good Pairs.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import math
2+
class Solution:
3+
def numIdenticalPairs(self, nums: List[int]) -> int:
4+
groups = collections.Counter(nums).values()
5+
# result = sum([ n * (n-1)/2 for n in groups])
6+
result = sum([math.comb(n,2) for n in groups])
7+
return int(result)
8+
9+
# Build in wheel for calculating the combinations.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ For question details, please go to [Leetcode](https://leetcode.com/problemset/al
1414
+ Medium 515 Find Largest Value in Each Tree Row --`[float('-inf')]`
1515

1616
2. [Depth First Search]()
17+
Iteration VS recursive.
18+
---
1719
+ Pre-order traversal
1820
+ In-order traversal
1921
+ Post-order traversal
22+
+ Medium 200 Number of Islands
2023

2124
3. [Hashing](https://github.com/KaidiGuo/Algorithm-Exercises/tree/master/Hashing)
25+
The use of collections.Counter() and dictionary.
26+
---
2227
+ Easy 884 Uncommon Words from Two Sentences -- `[collections.Counter()]`
2328
+ Easy 387 First Unique Character in a String -- `[collections.Counter()]`--`[float('inf')]`--`[index = string.find('c')]`
29+
+ Easy 1512. Number of Good Pairs `[math.comb(n,k)]`
2430
+ Medium 347 Top K Frequent Elements -- `[counts = sorted(dic.values())]`
2531
+ Easy 1 Two Sum -- `[for i,item in enumerate(nums)]`
2632
+ Medium 3 Longest Substring Without Repeating Characters

0 commit comments

Comments
 (0)