Skip to content

Commit

Permalink
DSA
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Jan 1, 2022
1 parent 2412832 commit 073c50a
Show file tree
Hide file tree
Showing 185 changed files with 174 additions and 173 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# https://www.youtube.com/watch?v=Y72QeX0Efxw
'''
First Transpose(flip matrix over its diagonal) the matrix then rotate every rows
[[1,2,3], [[1,4,7], [[7,4,1],
[4,5,6], =>Transpose=> [2,5,8], =>Rotate rows=> [8,5,2],
[7,8,9]] [3,6,9]] [9,6,3]]
'''
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
#Transpose
for i in range(len(matrix)):
for j in range(len(matrix[0])):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
#Rotate rows
for i in range(len(matrix)):
l, r = 0, len(matrix[0]) - 1
while l < r:
matrix[i][l], matrix[i][r] = matrix[i][r], matrix[i][l]
l += 1; r -= 1

return matrix

# Time Complexity = O(N*N)
# Space Complexity = O(1)



# https://www.youtube.com/watch?v=Y72QeX0Efxw
'''
First Transpose(flip matrix over its diagonal) the matrix then rotate every rows
[[1,2,3], [[1,4,7], [[7,4,1],
[4,5,6], =>Transpose=> [2,5,8], =>Rotate rows=> [8,5,2],
[7,8,9]] [3,6,9]] [9,6,3]]
'''
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
#Transpose
for i in range(len(matrix)):
for j in range(len(matrix[0])):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
#Rotate rows
for i in range(len(matrix)):
l, r = 0, len(matrix[0]) - 1
while l < r:
matrix[i][l], matrix[i][r] = matrix[i][r], matrix[i][l]
l += 1; r -= 1

return matrix

# Time Complexity = O(N*N)
# Space Complexity = O(1)




File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# https://www.geeksforgeeks.org/minimum-number-of-squares-whose-sum-equals-to-given-number-n/
# Minimum number of squares whose sum equals to given number n
# A naive recursive Python program to
# find minimum number of squares whose
# sum is equal to a given number

# Returns count of minimum squares
# that sum to n
def getMinSquares(n):

# base cases
if n <= 3:
return n

# getMinSquares rest of the table
# using recursive formula
# Maximum squares required
# is n (1 * 1 + 1 * 1 + ..)
res = n

# Go through all smaller numbers
# to recursively find minimum
for x in range(1, n + 1):
temp = x * x
if temp > n:
break
else:
res = min(res, 1 + getMinSquares(n - temp))

return res

# Driver code
print(getMinSquares(6))


'''
Output = 3
# https://www.geeksforgeeks.org/minimum-number-of-squares-whose-sum-equals-to-given-number-n/
# Minimum number of squares whose sum equals to given number n
# A naive recursive Python program to
# find minimum number of squares whose
# sum is equal to a given number

# Returns count of minimum squares
# that sum to n
def getMinSquares(n):

# base cases
if n <= 3:
return n

# getMinSquares rest of the table
# using recursive formula
# Maximum squares required
# is n (1 * 1 + 1 * 1 + ..)
res = n

# Go through all smaller numbers
# to recursively find minimum
for x in range(1, n + 1):
temp = x * x
if temp > n:
break
else:
res = min(res, 1 + getMinSquares(n - temp))

return res

# Driver code
print(getMinSquares(6))


'''
Output = 3
'''
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Swap two variables without using another variable
a = 10
b = 5

a = a + b
b = a - b
a = a - b

print(f"a = {a}")
print(f"b = {b}")


'''
a = 5
b = 10
# Swap two variables without using another variable
a = 10
b = 5

a = a + b
b = a - b
a = a - b

print(f"a = {a}")
print(f"b = {b}")


'''
a = 5
b = 10
'''
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
186 changes: 93 additions & 93 deletions Java/LinkedList.java → DSA-in-Java/LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
import java.io.*;

// Java program to implement
// a Singly Linked List
public class LinkedList {

Node head; // head of list

// Linked list Node.
// This inner class is made static
// so that main() can access it
static class Node {

int data;
Node next;

// Constructor
Node(int d)
{
data = d;
next = null;
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;

// If the Linked List is empty,
// then make the new node as head
if (list.head == null) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}

// Insert the new_node at last node
last.next = new_node;
}

// Return the list by head
return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
Node currNode = list.head;

System.out.print("LinkedList: ");

// Traverse through the LinkedList
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");

// Go to next node
currNode = currNode.next;
}
}

// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();

//
// ******INSERTION******
//

// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);

// Print the LinkedList
printList(list);
}
}
import java.io.*;

// Java program to implement
// a Singly Linked List
public class LinkedList {

Node head; // head of list

// Linked list Node.
// This inner class is made static
// so that main() can access it
static class Node {

int data;
Node next;

// Constructor
Node(int d)
{
data = d;
next = null;
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;

// If the Linked List is empty,
// then make the new node as head
if (list.head == null) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}

// Insert the new_node at last node
last.next = new_node;
}

// Return the list by head
return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
Node currNode = list.head;

System.out.print("LinkedList: ");

// Traverse through the LinkedList
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");

// Go to next node
currNode = currNode.next;
}
}

// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();

//
// ******INSERTION******
//

// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);

// Print the LinkedList
printList(list);
}
}
File renamed without changes.
2 changes: 2 additions & 0 deletions DSA-in-Java/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Data Structures and Algorithms in Java
### Please contribute to this folder by sharing solution code in Java.
1 change: 0 additions & 1 deletion Java/readme.md

This file was deleted.

0 comments on commit 073c50a

Please sign in to comment.