Skip to content

Commit c1807a9

Browse files
committed
🚀 27-Jun-2020
1 parent 1d776e7 commit c1807a9

15 files changed

+407
-0
lines changed

array/array_pair_sum.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
Given an array A of N positive integers and another number X.
3+
Determine whether or not there exist two elements in A whose sum is exactly X.
4+
*/
5+
6+
7+
8+
19
#include<bits/stdc++.h>
210
using namespace std;
311
int main()

competitive programming/leetcode/2020-June-Challenge/121. Best Time to Buy and Sell Stock.cpp renamed to competitive programming/leetcode/121. Best Time to Buy and Sell Stock.cpp

File renamed without changes.

competitive programming/leetcode/2020-June-Challenge/122. Best Time to Buy and Sell Stock II.cpp renamed to competitive programming/leetcode/122. Best Time to Buy and Sell Stock II.cpp

File renamed without changes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
2+
3+
An example is the root-to-leaf path 1->2->3 which represents the number 123.
4+
5+
Find the total sum of all root-to-leaf numbers.
6+
7+
Note: A leaf is a node with no children.
8+
9+
Example:
10+
11+
Input: [1,2,3]
12+
1
13+
/ \
14+
2 3
15+
Output: 25
16+
Explanation:
17+
The root-to-leaf path 1->2 represents the number 12.
18+
The root-to-leaf path 1->3 represents the number 13.
19+
Therefore, sum = 12 + 13 = 25.
20+
Example 2:
21+
22+
Input: [4,9,0,5,1]
23+
4
24+
/ \
25+
9 0
26+
/ \
27+
5 1
28+
Output: 1026
29+
Explanation:
30+
The root-to-leaf path 4->9->5 represents the number 495.
31+
The root-to-leaf path 4->9->1 represents the number 491.
32+
The root-to-leaf path 4->0 represents the number 40.
33+
Therefore, sum = 495 + 491 + 40 = 1026.
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
/**
44+
* Definition for a binary tree node.
45+
* struct TreeNode {
46+
* int val;
47+
* TreeNode *left;
48+
* TreeNode *right;
49+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
50+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
51+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
52+
* };
53+
*/
54+
class Solution {
55+
public:
56+
long long ans=0;
57+
58+
void sumNumbersUtil(TreeNode* root, int value){
59+
if(!root) return;
60+
value*=10;
61+
value+=root->val;
62+
if(!root->left && !root->right){
63+
ans+=value;
64+
return;
65+
}
66+
sumNumbersUtil(root->left, value);
67+
sumNumbersUtil(root->right, value);
68+
}
69+
int sumNumbers(TreeNode* root) {
70+
if(!root) return 0;
71+
sumNumbersUtil(root, 0);
72+
return ans;
73+
}
74+
};

competitive programming/leetcode/2020-June-Challenge/168. Excel Sheet Column Title.cpp renamed to competitive programming/leetcode/168. Excel Sheet Column Title.cpp

File renamed without changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Given an integer n, return the number of trailing zeroes in n!.
2+
3+
Example 1:
4+
5+
Input: 3
6+
Output: 0
7+
Explanation: 3! = 6, no trailing zero.
8+
Example 2:
9+
10+
Input: 5
11+
Output: 1
12+
Explanation: 5! = 120, one trailing zero.
13+
Note: Your solution should be in logarithmic time complexity.
14+
15+
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
int trailingZeroes(int n) {
24+
long cnt=0, tmp=5;
25+
while(tmp<=n){
26+
cnt+=n/tmp;
27+
tmp*=5;
28+
}
29+
return cnt;
30+
}
31+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
2+
3+
An example is the root-to-leaf path 1->2->3 which represents the number 123.
4+
5+
Find the total sum of all root-to-leaf numbers.
6+
7+
Note: A leaf is a node with no children.
8+
9+
Example:
10+
11+
Input: [1,2,3]
12+
1
13+
/ \
14+
2 3
15+
Output: 25
16+
Explanation:
17+
The root-to-leaf path 1->2 represents the number 12.
18+
The root-to-leaf path 1->3 represents the number 13.
19+
Therefore, sum = 12 + 13 = 25.
20+
Example 2:
21+
22+
Input: [4,9,0,5,1]
23+
4
24+
/ \
25+
9 0
26+
/ \
27+
5 1
28+
Output: 1026
29+
Explanation:
30+
The root-to-leaf path 4->9->5 represents the number 495.
31+
The root-to-leaf path 4->9->1 represents the number 491.
32+
The root-to-leaf path 4->0 represents the number 40.
33+
Therefore, sum = 495 + 491 + 40 = 1026.
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
/**
44+
* Definition for a binary tree node.
45+
* struct TreeNode {
46+
* int val;
47+
* TreeNode *left;
48+
* TreeNode *right;
49+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
50+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
51+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
52+
* };
53+
*/
54+
class Solution {
55+
public:
56+
long long ans=0;
57+
58+
void sumNumbersUtil(TreeNode* root, int value){
59+
if(!root) return;
60+
value*=10;
61+
value+=root->val;
62+
if(!root->left && !root->right){
63+
ans+=value;
64+
return;
65+
}
66+
sumNumbersUtil(root->left, value);
67+
sumNumbersUtil(root->right, value);
68+
}
69+
int sumNumbers(TreeNode* root) {
70+
if(!root) return 0;
71+
sumNumbersUtil(root, 0);
72+
return ans;
73+
}
74+
};

competitive programming/leetcode/2020-June-Challenge/287. Find the Duplicate Number.cpp renamed to competitive programming/leetcode/287. Find the Duplicate Number.cpp

File renamed without changes.

competitive programming/leetcode/2020-June-Challenge/53. Maximum Subarray.cpp renamed to competitive programming/leetcode/53. Maximum Subarray.cpp

File renamed without changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
2+
3+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
4+
5+
How many possible unique paths are there?
6+
7+
8+
Above is a 7 x 3 grid. How many possible unique paths are there?
9+
10+
11+
12+
Example 1:
13+
14+
Input: m = 3, n = 2
15+
Output: 3
16+
Explanation:
17+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
18+
1. Right -> Right -> Down
19+
2. Right -> Down -> Right
20+
3. Down -> Right -> Right
21+
Example 2:
22+
23+
Input: m = 7, n = 3
24+
Output: 28
25+
26+
27+
Constraints:
28+
29+
1 <= m, n <= 100
30+
It's guaranteed that the answer will be less than or equal to 2 * 10 ^ 9.
31+
32+
33+
34+
35+
36+
37+
38+
39+
class Solution {
40+
public:
41+
int uniquePaths(int m, int n) {
42+
int dp[m][n];
43+
for(int i=0;i<m;i++) dp[i][0]=1;
44+
for(int j=0;j<n;j++) dp[0][j]=1;
45+
for(int i=1;i<m;i++){
46+
for(int j=1;j<n;j++){
47+
dp[i][j]=dp[i-1][j]+dp[i][j-1];
48+
}
49+
}
50+
return dp[m-1][n-1];
51+
}
52+
};

0 commit comments

Comments
 (0)