Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8514e3c

Browse files
7 - Stock Buy and Sell (With No Limit)
1 parent 887befd commit 8514e3c

File tree

7 files changed

+103
-72
lines changed

7 files changed

+103
-72
lines changed

‎geek_for_geeks/challenge/Secondlargest.py‎

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,43 @@ def getSecondLargest(arr):
2222

2323

2424
# Using Two iteration
25-
def getSecondLargest(arr):
26-
n = len(arr)
27-
first = second = float("-inf")
28-
if n < 2:
29-
return -1
30-
for i in range(len(arr)):
31-
if arr[i] > first:
32-
first = arr[i]
33-
for i in range(len(arr)):
34-
if arr[i] != first and second < first and arr[i] > second:
35-
second = arr[i]
25+
# def getSecondLargest(arr):
26+
# n = len(arr)
27+
# first = second = float("-inf")
28+
# if n < 2:
29+
# return -1
30+
# for i in range(len(arr)):
31+
# if arr[i] > first:
32+
# first = arr[i]
33+
# for i in range(len(arr)):
34+
# if arr[i] != first and second < first and arr[i] > second:
35+
# second = arr[i]
3636

37-
if second == float("-inf"):
38-
return -1
39-
return second
37+
# if second == float("-inf"):
38+
# return -1
39+
# return second
4040

4141

4242
# T.C = O(2n)
4343
# S.C = O(1)
4444

4545

4646
# Using sorted()
47-
def getSecondLargest(arr):
48-
n = len(arr)
49-
data = sorted(arr)
50-
first = second = float("-inf")
51-
if n < 2:
52-
return -1
53-
else:
54-
first = max(arr)
55-
for i in range(len(data) - 1, 0, -1):
56-
if data[i] != first:
57-
second = data[i]
58-
break
59-
if second == float("-inf"):
60-
return -1
61-
return second
47+
# def getSecondLargest(arr):
48+
# n = len(arr)
49+
# data = sorted(arr)
50+
# first = second = float("-inf")
51+
# if n < 2:
52+
# return -1
53+
# else:
54+
# first = max(arr)
55+
# for i in range(len(data) - 1, 0, -1):
56+
# if data[i] != first:
57+
# second = data[i]
58+
# break
59+
# if second == float("-inf"):
60+
# return -1
61+
# return second
6262

6363

6464
# T.C = O(n log n)

‎geek_for_geeks/challenge/majority_element.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Majority Element - More Than n/3
1+
# 6.Majority Element - More Than n/3
22
def findMajority(arr):
33
data = {}
44
n = len(arr)

‎geek_for_geeks/challenge/move_zero.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ def pushZerosToEnd(arr):
1313
# S.C = O(1)
1414

1515

16-
def pushZerosToEnd(arr):
17-
count = 0
18-
n = len(arr)
19-
for i in range(n):
20-
if arr[i] != 0:
21-
arr[count] = arr[i]
22-
count += 1
23-
while count < n:
24-
arr[count] = 0
25-
count += 1
26-
return arr
16+
# def pushZerosToEnd(arr):
17+
# count = 0
18+
# n = len(arr)
19+
# for i in range(n):
20+
# if arr[i] != 0:
21+
# arr[count] = arr[i]
22+
# count += 1
23+
# while count < n:
24+
# arr[count] = 0
25+
# count += 1
26+
# return arr
2727

2828

2929
# T.C = O(2n)

‎geek_for_geeks/challenge/next_permutation.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def nextPermutation(arr):
2020
return arr
2121

2222

23-
print(nextPermutation([1, 2, 4, 5, 6, 0]))
23+
# print(nextPermutation([1, 2, 4, 5, 6, 0]))
2424

2525
# T.C = O(n)
2626
# S.C = O(1)

‎geek_for_geeks/challenge/reverse_an_array.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ def reverseArray(arr):
1212

1313

1414
# Naive method
15-
def reverseArray(arr):
16-
temp = []
17-
n = len(arr)
18-
for i in range(n):
19-
temp.append(arr[n - 1 - i])
20-
# for i in range(len(arr) - 1, -1, -1):
21-
# temp.append(arr[i])
22-
return temp
15+
# def reverseArray(arr):
16+
# temp = []
17+
# n = len(arr)
18+
# for i in range(n):
19+
# temp.append(arr[n - 1 - i])
20+
# # for i in range(len(arr) - 1, -1, -1):
21+
# # temp.append(arr[i])
22+
# return temp
2323

2424

2525
# T.C = O(n)

‎geek_for_geeks/challenge/rotate_an_array.py‎

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,41 @@ def reversal(arr, start, end):
2121
# S.C = O(1)
2222

2323

24-
def rotateArr(arr, d):
25-
n = len(arr)
26-
d = d % n
27-
return arr[d:] + arr[:d]
24+
# def rotateArr(arr, d):
25+
# n = len(arr)
26+
# d = d % n
27+
# return arr[d:] + arr[:d]
2828

2929

3030
# T.C = O(n)
3131
# S.C = O(n)
3232

3333

34-
def rotateArr(arr, d):
35-
n = len(arr)
36-
temp = []
37-
for i in range(d, n):
38-
temp.append(arr[i])
39-
for j in range(0, d):
40-
temp.append(arr[j])
41-
return temp
34+
# def rotateArr(arr, d):
35+
# n = len(arr)
36+
# temp = []
37+
# for i in range(d, n):
38+
# temp.append(arr[i])
39+
# for j in range(0, d):
40+
# temp.append(arr[j])
41+
# return temp
4242

4343

4444
# T.C = O(n)
4545
# S.C = O(n)
4646

4747

48-
def rotateArr(arr, d):
49-
n = len(arr)
50-
first = 0
51-
for i in range(d):
52-
first = arr[0]
53-
for j in range(n - 1):
54-
arr[j] = arr[j + 1]
55-
arr[n - 1] = first
56-
return arr
48+
# def rotateArr(arr, d):
49+
# n = len(arr)
50+
# first = 0
51+
# for i in range(d):
52+
# first = arr[0]
53+
# for j in range(n - 1):
54+
# arr[j] = arr[j + 1]
55+
# arr[n - 1] = first
56+
# return arr
5757

5858

5959
# T.C = O(d*n)
6060
# S.C = O(1)
6161

62-
print(rotateArr([1, 2, 3, 4, 5], 2))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 7 - Stock Buy and Sell (With No Limit)
2+
# Best Approach
3+
def maximumProfit(prices):
4+
n = len(prices)
5+
res = 0
6+
for i in range(0, n - 1, 1):
7+
if prices[i] < prices[i + 1]:
8+
res += prices[i + 1] - prices[i]
9+
return res
10+
11+
12+
# T.C: O(n)
13+
# S.C: O(1)
14+
15+
# def maximumProfit(prices):
16+
# n = len(prices)
17+
# res = 0
18+
# lmin = prices[0]
19+
# lmax = prices[0]
20+
# i = 0
21+
# while i < n - 1:
22+
# while i < n - 1 and prices[i] >= prices[i]:
23+
# i += 1
24+
# lmin = prices[i]
25+
# while i < n - 1 and prices[i] <= prices[i]:
26+
# i += 1
27+
# lmax = prices[i]
28+
# res += lmax - lmin
29+
# return res
30+
31+
# T.C: O(n)
32+
# S.C: O(1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /