Skip to content

Commit d1d0526

Browse files
committed
New IB Sol
1 parent 6bc23f0 commit d1d0526

7 files changed

+139
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def generateParenthesis(n):
2+
ans = []
3+
def backtrack(S = '', left = 0, right = 0):
4+
if len(S) == 2*n:
5+
ans.append(S)
6+
return
7+
if left < n:
8+
backtrack(S + '(',left+1, right)
9+
if right < left:
10+
backtrack(S + ')',left,right+1)
11+
backtrack()
12+
return ans
13+
print generateParenthesis(3)
File renamed without changes.

Graph/UndirectedDFS.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from collections import defaultdict
2+
3+
# This class represents a directed graph using
4+
# adjacency list representation
5+
class Graph:
6+
7+
# Constructor
8+
def __init__(self):
9+
10+
# default dictionary to store graph
11+
self.graph = defaultdict(list)
12+
13+
# function to add an edge to graph
14+
def addEdge(self,u,v):
15+
self.graph[u].append(v)
16+
17+
# A function used by DFS
18+
def DFSUtil(self,v,visited):
19+
20+
# Mark the current node as visited and print it
21+
visited[v]= True
22+
print v,
23+
24+
# Recur for all the vertices adjacent to this vertex
25+
for i in self.graph[v]:
26+
if visited[i] == False:
27+
self.DFSUtil(i, visited)
28+
29+
30+
# The function to do DFS traversal. It uses
31+
# recursive DFSUtil()
32+
def DFS(self,v):
33+
34+
# Mark all the vertices as not visited
35+
visited = [False]*(len(self.graph))
36+
37+
# Call the recursive helper function to print
38+
# DFS traversal starting from all vertices one by one
39+
for i in range(len(self.graph)):
40+
if visited[i] == False:
41+
self.DFSUtil(i,visited)
42+
43+
44+
# Driver code
45+
# Create a graph given in the above diagram
46+
g = Graph()
47+
g.addEdge(0, 1)
48+
g.addEdge(0, 2)
49+
g.addEdge(1, 2)
50+
g.addEdge(2, 0)
51+
g.addEdge(2, 3)
52+
g.addEdge(3, 3)
53+
54+
print "Following is DFS from (starting from vertex 2)"
55+
g.DFS(2)

Graph/number-of-islands.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.'''
2+
def numIslands(self,grid):
3+
if not grid:
4+
return 0
5+
count = 0
6+
for i in range(len(grid)):
7+
for j in range(len(grid[0])):
8+
if grid[i][j] == '1':
9+
self.dfs(grid,i,j)
10+
count += 1
11+
return count
12+
def dfs(self,grid,i,j):
13+
if i < 0 or j<0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != '1':
14+
return
15+
grid[i][j] = '#'
16+
self.dfs(grid,i+1,j)
17+
self.dfs(grid,i-1,j)
18+
self.dfs(grid,i,j+1)
19+
self.dfs(grid,i,j-1)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def lengthOfLongestSubstring(arr):
2+
max_length = 0
3+
i = 0
4+
j = i+1
5+
n = len(arr)
6+
ans = arr[0]
7+
while j < n:
8+
if arr[j] not in arr[i:j]:
9+
len_ = len(arr[i:j+1])
10+
if len_ > max_length:
11+
max_length = len_
12+
ans = arr[i:j+1]
13+
j += 1
14+
else:
15+
i += 1
16+
j = i+1
17+
return ans
18+
19+
20+
s = raw_input()
21+
print lengthOfLongestSubstring(s)
22+
23+
24+
#Approach 2
25+
class Solution:
26+
# @param A : string
27+
# @return an integer
28+
def lengthOfLongestSubstring(self, A):
29+
prev = {c:-1 for c in A}
30+
best, prev_best = 1, 1
31+
for i in range(1, len(A)):
32+
prev[A[i-1]] = i-1
33+
prev_best = min(i-prev[A[i]], prev_best+1)
34+
best = max(best, prev_best)
35+
return best
36+
Close

LinkedList/Add-LinkedList-as-no.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ def SumLinkedList(first,second):
2424
if second:
2525
second = second.next
2626

27-
if carry > 0
28-
temp.next = Node(carry)
27+
if carry > 0:
28+
temp.next = Node(carry)
2929
return head
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def maxArea(height):
2+
i = 0
3+
j = len(height)-1
4+
max_water = 0
5+
while i < j:
6+
max_water = max(max_water,(j-i)*min(height[j],height[i]))
7+
if height[i] < height[j]:
8+
i += 1
9+
else:
10+
j -= 1
11+
return max_water
12+
13+
arr = [1,1]
14+
print maxArea(arr)

0 commit comments

Comments
 (0)