Skip to content

Commit

Permalink
Added for 19 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 19, 2024
1 parent 8ef737f commit 96f63c3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
57 changes: 57 additions & 0 deletions GeeksForGeeks/19-1-24/GFG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#User function Template for python3

class Solution:
def kTop(self, arr, N, K):
final_ans=[]
top = [0 for i in range(K + 1)]
freq = {i:0 for i in range(K + 1)}

for m in range(N):
if a[m] in freq.keys():
freq[a[m]] += 1
else:
freq[a[m]] = 1

top[k] = a[m]
i = top.index(a[m])
i -= 1

while i >= 0:
if (freq[top[i]] < freq[top[i + 1]]):
t = top[i]
top[i] = top[i + 1]
top[i + 1] = t

elif ((freq[top[i]] == freq[top[i + 1]]) and (top[i] > top[i + 1])):
t = top[i]
top[i] = top[i + 1]
top[i + 1] = t
else:
break
i -= 1

i = 0
ans = []
while i < K and top[i] != 0:
ans.append(top[i])
i += 1

final_ans += [ans]
return final_ans

#{
# Driver Code Starts


t=int(input())
for _ in range(0,t):
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
ob = Solution()
ans=ob.kTop(a,n,k)
for line in ans:
print(*line)



# } Driver Code Ends
4 changes: 4 additions & 0 deletions GeeksForGeeks/19-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Time complexity - O(n * k)
Space complexity - O(n)

Was not possible for me to return the output as said in Java (ArrayList<Integer>); could had done if it was 2d ArrayList
2 changes: 2 additions & 0 deletions LeetCode/19-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(1)
20 changes: 20 additions & 0 deletions LeetCode/19-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution
{
public int minFallingPathSum(int[][] A)
{
final int n = A.length;

for (int i = 1; i < n; ++i)
for (int j = 0; j < n; ++j)
{
int min = Integer.MAX_VALUE;

for (int k = Math.max(0, j - 1); k < Math.min(n, j + 2); ++k)
min = Math.min(min, A[i - 1][k]);

A[i][j] += min;
}

return Arrays.stream(A[n - 1]).min().getAsInt();
}
}

0 comments on commit 96f63c3

Please sign in to comment.