Skip to content

Commit dfaa959

Browse files
uploading first time (#13)
* uploading first time * added few changes in ReadMe.md * corrected one solution file link Co-authored-by: Gourav Rusiya <[email protected]>
1 parent 1c6dc62 commit dfaa959

6 files changed

+299
-0
lines changed

C++/Count-Primes.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
4+
int countPrimes(int n) {
5+
6+
vector<bool> arr(n+1,0);
7+
arr[0]=true;
8+
arr[1]=true;
9+
for(int i=2;i*i<=n;i++){
10+
if(arr[i]==false){
11+
for(int j = i*i;j<=n;j+=i){
12+
arr[j]=true;
13+
}
14+
}
15+
}
16+
17+
int count = 0;
18+
for(int i=2;i<n;i++){
19+
if(!arr[i]){
20+
count++;
21+
}
22+
}
23+
return count;
24+
}
25+
};

C++/Excel-Sheet-Column-Title.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Runtime - 0ms
3+
* Memory - 5.8
4+
* LOGIC - simple maths
5+
*
6+
*/
7+
8+
class Solution {
9+
public:
10+
string convertToTitle(int n) {
11+
string s="";
12+
while(n>26){
13+
int rem = n%26;
14+
n=n/26;
15+
if(rem==0){
16+
rem = 26;
17+
n--;
18+
}
19+
s.push_back('A'+(rem-1));
20+
}
21+
s.push_back('A'+n-1);
22+
reverse(s.begin(),s.end());
23+
return s;
24+
}
25+
};

C++/Reverse-Integer.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int reverse(int x) {
4+
int rev = 0;
5+
while (x!=0) {
6+
int pop = x % 10;
7+
x /= 10;
8+
// to check that it may not overflow integer range
9+
if (rev > INT_MAX/10 || (rev == INT_MAX / 10 && pop > 7)) return 0;
10+
if (rev < INT_MIN/10 || (rev == INT_MIN / 10 && pop < -8)) return 0;
11+
rev = rev * 10 + pop;
12+
}
13+
return rev;
14+
}
15+
};
16+

C++/Sudoku-Solver.cpp

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Runtime - 56ms
3+
* Memory - 6.8
4+
* LOGIC - described in functions
5+
*
6+
*/
7+
class Solution {
8+
public:
9+
10+
// to check whether num is valid in that row
11+
bool isRowSafe(vector<vector<char>>& board, int row, char num)
12+
{
13+
for(int col = 0;col<9;col++)
14+
{
15+
if(board[row][col] == num)
16+
return true;
17+
}
18+
return false;
19+
}
20+
21+
// to check whether num is valid in that column
22+
bool isColumnSafe(vector<vector<char>>& board, int col , char num)
23+
{
24+
for(int row = 0; row<9; row++)
25+
{
26+
if(board[row][col] == num)
27+
return true;
28+
}
29+
return false;
30+
}
31+
32+
// to check whether num is valid in that 3*3 matrix grid
33+
bool isBoxSafe(vector<vector<char>>& board, int row1, int col1 , char num)
34+
{
35+
for(int row = 0;row<3;row++)
36+
{
37+
for(int col = 0;col<3;col++)
38+
{
39+
if(board[row1 + row][col1 + col] == num)
40+
return true;
41+
}
42+
}
43+
44+
return false;
45+
}
46+
47+
bool isSafe(vector<vector<char>>& board, int row, int col , char num)
48+
{
49+
return !isRowSafe(board,row,num) && !isColumnSafe(board, col, num) && !isBoxSafe(board, row-row%3,col-col%3,num) && board[row][col] == '.';
50+
}
51+
52+
53+
bool findUnassignedLocation(vector<vector<char>>& board, int &row, int &col)
54+
{
55+
for(row = 0;row<9;row++)
56+
{
57+
for(col = 0;col<9;col++)
58+
{
59+
if(board[row][col] == '.')
60+
return true;
61+
62+
}
63+
}
64+
return false;
65+
}
66+
67+
68+
69+
bool solve(vector<vector<char>>& board)
70+
{
71+
int row,col;
72+
if(!findUnassignedLocation(board, row, col))
73+
return true;
74+
75+
for(char i = '1' ; i<='9';i++)
76+
{
77+
if(isSafe(board,row,col,i))
78+
{
79+
board[row][col] = i;
80+
81+
if(solve(board))
82+
return true;
83+
84+
board[row][col] = '.';
85+
}
86+
}
87+
88+
return false;
89+
}
90+
91+
/* A utility function to print grid */
92+
void printGrid(vector<vector<char>>& board)
93+
{
94+
for (int row = 0; row < 9; row++) {
95+
for (int col = 0; col < 9; col++)
96+
cout << board[row][col] << " ";
97+
cout << endl;
98+
}
99+
}
100+
101+
102+
void solveSudoku(vector<vector<char>>& board) {
103+
104+
solve(board);
105+
printGrid(board);
106+
107+
}
108+
};
109+

Java/LRU-Cache.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* LRU Cache
3+
*
4+
* Logic : maintain doubly linked List for insertion at head and deletion from tail and maintain hashmap of key as key and value as node. Node consists of key, value, prev and next pointers.
5+
* Runtime: 16 ms
6+
* Memory Usage: 47.9 MB
7+
*/
8+
9+
class LRUCache {
10+
11+
final Node head = new Node();
12+
final Node tail = new Node();
13+
int capacity;
14+
Map<Integer, Node> map;
15+
16+
public LRUCache(int capacity) {
17+
map = new HashMap(capacity);
18+
this.capacity = capacity;
19+
head.next = tail;
20+
tail.prev = head;
21+
}
22+
23+
public int get(int key) {
24+
int result = -1;
25+
Node node = map.get(key);
26+
if(node!=null)
27+
{
28+
remove(node);
29+
add(node);
30+
result = node.val;
31+
}
32+
return result;
33+
}
34+
35+
public void put(int key, int value) {
36+
Node node = map.get(key);
37+
if(node!=null)
38+
{
39+
remove(node);
40+
node.val = value;
41+
add(node);
42+
}
43+
else{
44+
if(map.size() == capacity)
45+
{
46+
map.remove(tail.prev.key);
47+
remove(tail.prev);
48+
}
49+
Node new_node = new Node();
50+
51+
new_node.key = key;
52+
new_node.val = value;
53+
map.put(key, new_node);
54+
add(new_node);
55+
}
56+
}
57+
58+
public void add(Node node)
59+
{
60+
Node head_next = head.next;
61+
node.next = head_next;
62+
head_next.prev = node;
63+
head.next = node;
64+
node.prev = head;
65+
66+
}
67+
68+
public void remove(Node node)
69+
{
70+
Node next_node = node.next;
71+
Node prev_node = node.prev;
72+
73+
next_node.prev = prev_node;
74+
prev_node.next = next_node;
75+
76+
}
77+
78+
class Node{
79+
int key;
80+
int val;
81+
Node prev;
82+
Node next;
83+
}
84+
}
85+
86+
/**
87+
* Your LRUCache object will be instantiated and called as such:
88+
* LRUCache obj = new LRUCache(capacity);
89+
* int param_1 = obj.get(key);
90+
* obj.put(key,value);
91+
*/

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
8585
</div>
8686
<br/>
8787

88+
8889
## String
8990

9091
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
@@ -128,13 +129,30 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
128129
| --- | ------------------------------------------------------------- | --------------------------------- | ------ | ------ | ---------- | --- | ------------- |
129130
| 001 | [Two Sum](https://leetcode.com/problems/two-sum/)| [Java](./Java/two-sum.java) <br> [Python](./Python/1_TwoSum.py)|_O(N)_|_O(N)_|Easy|||
130131
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Java](./Java/valid-anagram.java) | _O(n)_ | _O(1)_ | Easy | | Unicode chars|
132+
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Java](./Java/LRU-Cache.java) | | | Medium | | |
133+
134+
<br/>
135+
<div align="right">
136+
<b><a href="#algorithms">⬆️ Back to Top</a></b>
137+
</div>
138+
<br/>
139+
140+
## Math
141+
142+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
143+
| --- | ---------------------------------------------------------------- | --------------------------------------- | ---------- | ------ | ---------- | --------- | ---- |
144+
| 204 | [Count Primes](https://leetcode.com/problems/count-primes) | [C++](./C++/Count-Primes.cpp) | _O(n(log(logn)))_ | _O(n)_ | Easy | Math | |
145+
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) |[C++](./C++/Excel-Sheet-Column-Title.cpp)| _O(n)_ | _O(n)_ | Easy | String| |
146+
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) |[C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | |
147+
131148

132149
<br/>
133150
<div align="right">
134151
<b><a href="#algorithms">⬆️ Back to Top</a></b>
135152
</div>
136153
<br/>
137154

155+
138156
## Two Pointer
139157

140158
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
@@ -147,6 +165,20 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
147165
</div>
148166
<br/>
149167

168+
169+
## BackTracking
170+
171+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
172+
| --- | --------------------------------------------------------------------- | ----------------------------------------- | ------ | ------ | ---------- | ----- | ---- |
173+
| 037 | [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/) | [C++](./C++/Sudoku-Solver.cpp) | _O(9^(n*n))_ | _O(n*n)_ | Hard | Hash Table | |
174+
175+
<br/>
176+
<div align="right">
177+
<b><a href="#algorithms">⬆️ Back to Top</a></b>
178+
</div>
179+
<br/>
180+
181+
150182
## Binary Search
151183

152184
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
@@ -186,6 +218,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
186218
| [Lokendra Bohra](https://github.com/lokendra1704/) <br> <img src="https://github.com/lokendra1704.png" width="100" height="100"> | India | Python | [Leetcode](https://t.co/u0OByxhcHA) <br> [Hackerrank](https://www.hackerrank.com/lokendra17) |
187219
| [Yuri Spiridonov](https://github.com/YuriSpiridonov) <br> <img src="https://github.com/YuriSpiridonov.png" width="100" height="100"> | Russia | Python | [Twitter](https://twitter.com/YuriSpiridonov)<br>[Leetcode](https://leetcode.com/yurispiridonov/)<br>[Hackerrank](https://www.hackerrank.com/YuriSpiridonov) |
188220
| [Naveen Kashyap](https://github.com/naveenkash) <br> <img src="https://github.com/naveenkash.png" width="100" height="100"> | India | Javascript | [Twitter](https://twitter.com/naveen_kashyapp)<br>[Leetcode](https://leetcode.com/naveenkash/) |
221+
| [Rudra Mishra](https://github.com/Rudra407) <br> <img src="https://github.com/Rudra407.png" width="100" height="100"> | India | C++ | [Twitter](https://twitter.com/ruDra_Mishra407)<br>[Leetcode](https://leetcode.com/rudramishra/) |
189222
| [Sachin Singh Negi](https://github.com/sachinnegi) <br> <img src="https://github.com/sachinnegi.png" width="100" height="100"> | India | Python | [Twitter](https://twitter.com/SachinSinghNe17)<br>[Leetcode](https://leetcode.com/negisachin688/)<br>[Hackerrrak](https://www.hackerrank.com/negisachin688) | |
190223
<br/>
191224
<div align="right">

0 commit comments

Comments
 (0)