Skip to content

Commit 0d64fa6

Browse files
committed
Commit #121 - Modified 4 file(s) - 13.06.2023 @23:18
1 parent f0fe034 commit 0d64fa6

File tree

4 files changed

+104
-183
lines changed

4 files changed

+104
-183
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//Attempt #1 - 25mins
2+
//Link: https://leetcode.com/problems/equal-row-and-column-pairs/description/
3+
4+
class Solution {
5+
public int equalPairs(int[][] grid) {
6+
int n = grid.length;
7+
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
8+
for(int [] row: grid){
9+
String rowStr = Arrays.toString(row);
10+
hashMap.put(rowStr, hashMap.getOrDefault(rowStr,0)+1);
11+
}
12+
13+
int count = 0;
14+
for(int j = 0;j<n;j++){
15+
int [] col = new int[n];
16+
for(int i = 0;i<n;i++){
17+
col[i] = grid[i][j];
18+
}
19+
count+=hashMap.getOrDefault(Arrays.toString(col),0);
20+
}
21+
return count;
22+
}
23+
}
24+
25+
/*
26+
Runtime
27+
49 ms
28+
Beats
29+
48.20%
30+
Memory
31+
46.1 MB
32+
Beats
33+
96.55%
34+
35+
TC - O(n^2)
36+
SC - O(n^2)
37+
*/

Linked Lists/R - Reverse Linked List.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,32 @@ public ListNode reverseList(ListNode head) {
9191
41.9 MB
9292
Beats
9393
74.65%
94-
*/
94+
*/
95+
96+
//Optimal Soln
97+
98+
class Program {
99+
public static LinkedList reverseLinkedList(LinkedList head) {
100+
// Write your code here.
101+
LinkedList current = head;
102+
LinkedList prev = null;
103+
104+
while(current != null) {
105+
LinkedList temp = current.next;
106+
current.next = prev;
107+
prev = current;
108+
current = temp;
109+
}
110+
return prev;
111+
}
112+
113+
static class LinkedList {
114+
int value;
115+
LinkedList next = null;
116+
117+
public LinkedList(int value) {
118+
this.value = value;
119+
}
120+
}
121+
}
122+

Linked Lists/Remove Nth Node From End of List.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,41 @@ public ListNode removeNthFromEnd(ListNode head, int n) {
8686
Runtime: 1 ms
8787
Memory Usage: 42.5 MB
8888
*/
89-
// Keypoint - Using extra space - for listnode x
89+
// Keypoint - Using extra space - for listnode x
90+
91+
//Another Optimal:
92+
93+
class Solution {
94+
public ListNode removeNthFromEnd(ListNode head, int n) {
95+
ListNode start = new ListNode();
96+
start.next = head;
97+
ListNode fast = start;
98+
ListNode slow = start;
99+
100+
for(int i = 1; i <= n; ++i)
101+
fast = fast.next;
102+
103+
while(fast.next != null)
104+
{
105+
fast = fast.next;
106+
slow = slow.next;
107+
}
108+
109+
slow.next = slow.next.next;
110+
111+
return start.next;
112+
}
113+
}
114+
115+
/*
116+
Runtime
117+
0 ms
118+
Beats
119+
100%
120+
Memory
121+
40.8 MB
122+
Beats
123+
50.19%
124+
125+
TC - O(n)
126+
*/

Linked Lists/Test.java

Lines changed: 0 additions & 181 deletions
This file was deleted.

0 commit comments

Comments
 (0)