Skip to content

Commit 81ba7c5

Browse files
authored
Merge branch 'main' into patch-49
2 parents 3c1231a + 3ad1f86 commit 81ba7c5

File tree

79 files changed

+1841
-124
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1841
-124
lines changed

README.md

+49-49
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Time complexity: O(log n)
2+
// Space complexity: O(1)
3+
4+
class Solution
5+
{
6+
public:
7+
int singleNonDuplicate(vector<int> &nums)
8+
{
9+
int left = 0, right = nums.size() - 2;
10+
11+
while (left <= right)
12+
{
13+
int mid1 = (left + right) >> 1;
14+
int mid2 = mid1 ^ 1;
15+
if (nums[mid1] == nums[mid2])
16+
{
17+
left = mid1 + 1;
18+
}
19+
else
20+
{
21+
right = mid1 - 1;
22+
}
23+
}
24+
25+
return nums[left];
26+
}
27+
};
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
string mergeAlternately(string word1, string word2)
4+
{
5+
int i=0;
6+
string final="";
7+
8+
while(i < word1.size() && i < word2.size())
9+
final = final + word1[i] + word2[i++];
10+
11+
while(i < word1.size())
12+
final += word1[i++];
13+
while(i < word2.size())
14+
final += word2[i++];
15+
16+
return final;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
12+
// Time: O(n)
13+
// Space: O(1)
14+
class Solution {
15+
public:
16+
int pairSum(ListNode* head) {
17+
ListNode *fast = head, *slow = head;
18+
19+
while(fast && fast->next) {
20+
slow = slow->next;
21+
fast = fast->next->next;
22+
}
23+
slow = reverseList(slow);
24+
25+
int mx = 0;
26+
while(head && slow) {
27+
mx = max(mx, head->val + slow->val);
28+
head = head->next;
29+
slow = slow->next;
30+
}
31+
32+
return mx;
33+
}
34+
private:
35+
ListNode* reverseList(ListNode* head) {
36+
ListNode* prev = NULL;
37+
ListNode* curr = head;
38+
ListNode* NEXT = NULL;
39+
40+
while(curr) {
41+
NEXT = curr->next;
42+
curr->next = prev;
43+
prev = curr;
44+
curr = NEXT;
45+
}
46+
47+
return prev;
48+
}
49+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Time Complexity: O(m + n), we check each element of nums1Set and nums2Set
2+
// Space Complexity: O(m + n), where m and n are length sets in worst case.
3+
4+
class Solution
5+
{
6+
public:
7+
vector<vector<int>> findDifference(vector<int> &nums1, vector<int> &nums2)
8+
{
9+
unordered_set<int> nums1Set(nums1.begin(), nums1.end());
10+
unordered_set<int> nums2Set(nums2.begin(), nums2.end());
11+
12+
vector<int> lst1;
13+
vector<int> lst2;
14+
15+
for (const auto &num : nums1Set)
16+
{
17+
if (nums2Set.find(num) == nums2Set.end())
18+
{
19+
lst1.push_back(num);
20+
}
21+
}
22+
23+
for (const auto &num : nums2Set)
24+
{
25+
if (nums1Set.find(num) == nums1Set.end())
26+
{
27+
lst2.push_back(num);
28+
}
29+
}
30+
31+
return {lst1, lst2};
32+
}
33+
};

go/0912-sort-an-array.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
func sortArray(nums []int) []int {
2+
sort.Ints(nums)
3+
return nums
4+
}

go/1929-concatenation-of-array.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func getConcatenation(nums []int) []int {
2+
n := len(nums)
3+
ans := make([]int, 2*n) //lets make an array of int type and 2*n size
4+
i := 0
5+
for i < n { //implementing for loop for the given condition
6+
ans[i] = nums[i]
7+
ans[i+n] = nums[i]
8+
i++
9+
}
10+
return ans
11+
}

java/0091-decode-ways.java

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
class Solution {
33

44
public int numDecodings(String s) {
5-
int[] dp = new int[s.length() + 1];
65
int twoBack = 1; // empty string
76
int oneBack = s.charAt(0) == '0' ? 0 : 1;
87
int current = oneBack;

java/0838-push-dominoes.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public String pushDominoes(String dominoes) {
3+
int len = dominoes.length();
4+
Queue<Integer> q = new LinkedList<>();
5+
char[] dom = dominoes.toCharArray();
6+
for (int i = 0; i < len; i++)
7+
if (dominoes.charAt(i) != '.') q.offer(i);
8+
9+
while (!q.isEmpty()) {
10+
int i = q.poll();
11+
char ch = dom[i];
12+
if (dom[i] == 'R') {
13+
if (i + 1 < len && dom[i + 1] == '.') {
14+
if (i + 2 < len && dom[i + 2] == 'L') {
15+
q.poll();
16+
} else {
17+
dom[i + 1] = 'R';
18+
q.offer(i + 1);
19+
}
20+
}
21+
} else if (i > 0 && dom[i - 1] == '.') {
22+
dom[i - 1] = 'L';
23+
q.offer(i - 1);
24+
}
25+
}
26+
return String.valueOf(dom);
27+
}
28+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String mergeAlternately(String word1, String word2) {
3+
int i = 0;
4+
StringBuilder res = new StringBuilder();
5+
6+
while (i < word1.length() || i < word2.length()) {
7+
if (i < word1.length()) {
8+
res.append(word1.charAt(i));
9+
}
10+
if (i < word2.length()) {
11+
res.append(word2.charAt(i));
12+
}
13+
i++;
14+
}
15+
16+
return res.toString();
17+
}
18+
}
19+
20+
/**
21+
* Time Complexity : O(n+m)
22+
* Space Complexity : O(n+m)
23+
*/
+23-24
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/**
2-
* https://leetcode.com/problems/generate-parentheses
2+
* DFS
33
* Time O(((4^N) / (N * SQRT(N)))) | Space O(((4^N) / (N * SQRT(N))))
44
* Time O(2^N) | Space O(2^N)
5+
* https://leetcode.com/problems/generate-parentheses
56
* @param {number} n
67
* @return {string[]}
78
*/
8-
var generateParenthesis = (n) => dfs(n);/* Time O(2^N) | Space O(2^N) */
9+
var generateParenthesis = (n) => dfs(n);
910

1011
const dfs = (n, combos = [], open = 0, close = 0, path = []) => {
11-
const isBaseCase = path.length === (n * 2);
12+
const isBaseCase = (path.length === (n * 2));
1213
if (isBaseCase) {
1314
combos.push(path.join(''));/* Space O(N + N) */
1415

@@ -25,35 +26,36 @@ const dfs = (n, combos = [], open = 0, close = 0, path = []) => {
2526
}
2627

2728
const backTrackOpen = (n, combos, open, close, path) => {
28-
path.push('('); /* | Space O(N) */
29+
path.push('(');/* Space O(N) */
2930
dfs(n, combos, (open + 1), close, path);/* Time O(2^N) | Space O(2^N) */
3031
path.pop();
3132
}
3233

3334
const backTrackClose = (n, combos, open, close, path) => {
34-
path.push(')'); /* | Space O(N) */
35+
path.push(')');/* Space O(N) */
3536
dfs(n, combos, open, (close + 1), path);/* Time O(2^N) | Space O(2^N) */
3637
path.pop();
3738
}
3839

3940
/**
40-
* https://leetcode.com/problems/generate-parentheses
41+
* BFS
4142
* Time O(((4^N) / (N * SQRT(N)))) | Space O(((4^N) / (N * SQRT(N))))
4243
* Time O(2^N) | Space O(2^N)
44+
* https://leetcode.com/problems/generate-parentheses
4345
* @param {number} n
4446
* @return {string[]}
4547
*/
46-
var generateParenthesis = (n) => bfs(n);/* Time O(2^N) | Space O(2^N) */
48+
var generateParenthesis = (n) => bfs(n);
4749

48-
const bfs = (n, queue, combos = []) => {
50+
const bfs = (n, combos = []) => {
4951
const queue = new Queue([ ['', 0, 0] ]);
5052

5153
while (!queue.isEmpty()) {/* Time O(2^N) */
5254
const [ str, open, close ] = queue.dequeue();
53-
54-
const isBaseCase = (open === n) && (close === n);
55+
56+
const isBaseCase = ((open === n) && (close === n));
5557
if (isBaseCase) {
56-
combos.push(str); /* Space O(N) */
58+
combos.push(str); /* Space O(N) */
5759

5860
continue;
5961
}
@@ -69,31 +71,28 @@ const bfs = (n, queue, combos = []) => {
6971
}
7072

7173
/**
72-
* https://leetcode.com/problems/generate-parentheses
74+
* DFS
7375
* Time O(((4^N) / (N * SQRT(N)))) | Space O(((4^N) / (N * SQRT(N))))
7476
* Time O(2^N) | Space O(2^N)
77+
* https://leetcode.com/problems/generate-parentheses
7578
* @param {number} n
7679
* @return {string[]}
7780
*/
7881
var generateParenthesis = (n, combos = []) => {
79-
const isBaseCase = n === 0;
82+
const isBaseCase = (n === 0);
8083
if (isBaseCase) {
81-
combos.push(''); /* | Space O(N) */
84+
combos.push('');
8285

83-
return combos;
86+
return combos
8487
}
8588

86-
return closureNumber(n, combos);/* Time O(2^N) | Space O(2^N) */
87-
}
88-
89-
const closureNumber = (n, combos) => {
90-
for (let c = 0; c < n; c++) {/* Time O(N) */
91-
for (const left of generateParenthesis(c)) { /* Time O(2^N) | Space O(2^N) */
92-
for (const right of generateParenthesis(((n - 1) - c))) {/* Time O(2^N) | Space O(2^N) */
93-
combos.push(`(${left})${right}`); /* | Space O(N) */
89+
for (let c = 0; (c < n); c++) {
90+
for (const left of generateParenthesis(c)) {
91+
for (const right of generateParenthesis(((n - 1) - c))) {
92+
combos.push(`(${left})${right}`);
9493
}
9594
}
9695
}
9796

9897
return combos
99-
}
98+
}

javascript/0036-valid-sudoku.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ var searchGrid = (board, boxes, cols, rows) => {
3333
const isEmpty = char === '.';
3434
if (isEmpty) continue;
3535

36-
const hasMoved = boxes[index][char] || cols[col][char] || rows[row][char];
36+
const hasMoved = boxes[index][(char - 1)] || cols[col][(char - 1)] || rows[row][(char - 1)];
3737
if (hasMoved) return false;
3838

39-
rows[row][char] = true; /* Space O(ROWS * COLS)*/
40-
cols[col][char] = true; /* Space O(ROWS * COLS)*/
41-
boxes[index][char] = true; /* Space O(ROWS * COLS)*/
39+
rows[row][(char - 1)] = true; /* Space O(ROWS * COLS)*/
40+
cols[col][(char - 1)] = true; /* Space O(ROWS * COLS)*/
41+
boxes[index][(char - 1)] = true; /* Space O(ROWS * COLS)*/
4242
}
4343
}
4444

@@ -84,4 +84,4 @@ var searchGrid = (board, boxes, rows, cols) => {
8484
}
8585

8686
return true;
87-
}
87+
}

javascript/0069-sqrtx.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Binary Search
3+
* https://leetcode.com/problems/sqrtx/
4+
*
5+
* Time O(log(n)) | Space O(1)
6+
* @param {number} x
7+
* @return {number}
8+
*/
9+
var mySqrt = function(x) {
10+
let left = 1;
11+
let right = x;
12+
13+
while(left <= right) {
14+
const mid = (left + right) >> 1;
15+
if(mid * mid <= x && (mid+1) * (mid+1) > x) return mid;
16+
if(mid * mid < x) {
17+
left = mid + 1;
18+
} else {
19+
right = mid -1;
20+
}
21+
}
22+
23+
return 0;
24+
};

0 commit comments

Comments
 (0)