diff --git a/GeeksForGeeks/19-1-24/GFG.py b/GeeksForGeeks/19-1-24/GFG.py new file mode 100644 index 0000000..1e00b3d --- /dev/null +++ b/GeeksForGeeks/19-1-24/GFG.py @@ -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 diff --git a/GeeksForGeeks/19-1-24/README.md b/GeeksForGeeks/19-1-24/README.md new file mode 100644 index 0000000..efcda69 --- /dev/null +++ b/GeeksForGeeks/19-1-24/README.md @@ -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); could had done if it was 2d ArrayList diff --git a/LeetCode/19-1-24/README.md b/LeetCode/19-1-24/README.md new file mode 100644 index 0000000..1d3ee04 --- /dev/null +++ b/LeetCode/19-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n^2) +Space complexity - O(1) diff --git a/LeetCode/19-1-24/Solution.java b/LeetCode/19-1-24/Solution.java new file mode 100644 index 0000000..0ea9c41 --- /dev/null +++ b/LeetCode/19-1-24/Solution.java @@ -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(); + } +}