Skip to content

Commit 1901423

Browse files
authored
Merge branch 'master' into master
2 parents 9b881f1 + 42b5e17 commit 1901423

File tree

3 files changed

+93
-20
lines changed

3 files changed

+93
-20
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
This code uses prefix and postfix product to evaluate answer.
3+
We just need to traverse the array twice, once to the left and once to the right.
4+
Then answer of ith place can be calculated using constant time.
5+
6+
Time Complexity : O(n)
7+
Space Complexity : O(n)
8+
*/
9+
10+
11+
12+
class Solution {
13+
public:
14+
vector<int> productExceptSelf(vector<int>& nums) {
15+
int n = nums.size(); //Variable for size of the array
16+
//pre[] stores product of all numbers to the left of ith element
17+
//post[] stores product of all numbers to the right of ith element
18+
int pre[n],post[n];
19+
20+
//loop to assign values to pre[]
21+
int mul=1;
22+
for(int i=0; i<n; i++){
23+
mul*=nums[i];
24+
pre[i]=mul;
25+
}
26+
27+
//loop to assign values to post[]
28+
mul=1;
29+
for(int i=n-1; i>=0; i--){
30+
mul*=nums[i];
31+
post[i]=mul;
32+
}
33+
34+
//declare a vector to return
35+
vector <int> out;
36+
37+
//first element of out is just going to be product of all elements except first one
38+
out.push_back(post[1]);
39+
40+
//value of out[i] = product of all elements except ith element
41+
//which is nothing but pre[i-1]*[post[i+1]]
42+
for(int i=1; i<n-1; i++){
43+
int p=i-1;
44+
int s=i+1;
45+
out.push_back(pre[p]*post[s]);
46+
}
47+
48+
//last element of out is just going to be product of all elements except last one
49+
out.push_back(pre[n-2]);
50+
51+
//return the vector
52+
return out;
53+
}
54+
};

Python/baseK.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def sumBase(n: int, k: int) :
2+
"""
3+
Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n from base 10 to base k.
4+
After converting, each digit should be interpreted as a base 10 number, and the sum should be returned in base 10.
5+
TC : O(N)
6+
SC : O(1)
7+
n : int (integer base 10)
8+
k : int (base to be converted to)
9+
return value : int
10+
"""
11+
summation=0
12+
while n >= k :
13+
14+
summation = summation + n%k
15+
n=n//k
16+
print(n)
17+
return (summation + n)

README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
146146
| 27 | [Remove-Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) | O(N) | O(1) | Easy | Array |
147147
| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Java](./Java/valid-sudoku.java) | O(N^2) | O(N) | Medium | Array, 2D Matrix |
148148
| 1512 | [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) | [Java](./Java/Number-of-Good-Pairs.java) | O(N^2) | O(1) | Easy | Array |
149-
| 162 | [Find Peak element](https://leetcode.com/problems/find-peak-element/) | [javascript](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/JavaScript/findPeakElement.js) | o(Logn) | O(1) | Medium | Array |
150-
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/Spiral-matrix.cpp) | O(M\*N) | O(M\*N) | Medium | Array |
149+
| 162 | [Find Peak element](https://leetcode.com/problems/find-peak-element/) | [javascript](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/JavaScript/findPeakElement.js) | o(Logn) | O(1) | Medium | Array |
150+
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/Spiral-matrix.cpp) | O(M\*N) | O(M\*N) | Medium | Array |
151+
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [C++](./C++/238.Product_of_array_except_self) | O(N) | O(N) | Medium | Array |
151152

152153
<br/>
153154

@@ -304,23 +305,24 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
304305

305306
# Math
306307

307-
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
308-
| ---- | ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- | ----------------- | ------ | ---------- | ------ | ------------- |
309-
| 050 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Python](./Python/50.Powxn.py) <br> [JavaScript](./JavaScript/50.Powxn.js) | _O(n)_ | _O(1)_ | Medium | Math | |
310-
| 204 | [Count Primes](https://leetcode.com/problems/count-primes) | [C++](./C++/Count-Primes.cpp) | _O(n(log(logn)))_ | _O(n)_ | Easy | Math | |
311-
| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | [C++](./C++/Excel-Sheet-Column-Number.cpp) | _O(n)_ | _O(1)_ | Easy | String | |
312-
| 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 | |
313-
| 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) | [Java](./Java/reverse-integer.java) <br> [C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | |
314-
| 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [Java](./Java/Happy-Number.java) | _O(n^2)_ | _O(n)_ | Easy | Math | |
315-
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | |
316-
| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [Java](./Java/integer-to-roman.java) | _O(n)_ | _O(1)_ | Medium | Math | |
317-
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java) <br> [C++](./C++/Roman_to_Integer.cpp) | _O(n)_ | _O(1)_ | Easy | Math | |
318-
| 14 | [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/) | [Java](./Java/Arithmetic-Subarrays.java) | _O(m\*n)_ | _O(n)_ | Medium | Math | Pattern Count |
319-
| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [Java](./Java/Ugly-Number.java) | _O(n)_ | _O(n)_ | Easy | Math | |
320-
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Java](./Java/FizzBuzz.java) | _O(n)_ | _O(n)_ | Easy | Math | |
321-
| 1518 | [Water Bottles](https://leetcode.com/problems/water-bottles/) | [Java](./Java/WaterBottles.java) | _O(n)_ | _O(n)_ | Easy | Math | |
322-
| 1822 | [Sign Of Product](https://leetcode.com/problems/sign-of-the-product-of-an-array/) | [Java](./Java/SignOf.java) | _O(n)_ | _O(n)_ | Easy | Math | |
323-
| 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Java](./Java/BrokenCalculator.java) | _O(n)_ | _O(n)_ | Medium | Math | |
308+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
309+
| --- | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ----------------- | ------ | ---------- | ------ | ------------- |
310+
| 050 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Python](./Python/50.Powxn.py) <br> [JavaScript](./JavaScript/50.Powxn.js) | _O(n)_ | _O(1)_ | Medium | Math | |
311+
| 204 | [Count Primes](https://leetcode.com/problems/count-primes) | [C++](./C++/Count-Primes.cpp) | _O(n(log(logn)))_ | _O(n)_ | Easy | Math | |
312+
| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | [C++](./C++/Excel-Sheet-Column-Number.cpp) | _O(n)_ | _O(1)_ | Easy | String | |
313+
| 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 | |
314+
| 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) | [Java](./Java/reverse-integer.java) <br> [C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | |
315+
| 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [Java](./Java/Happy-Number.java) | _O(n^2)_ | _O(n)_ | Easy | Math | |
316+
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | |
317+
| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [Java](./Java/integer-to-roman.java) | _O(n)_ | _O(1)_ | Medium | Math | |
318+
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java) <br> [C++](./C++/Roman_to_Integer.cpp)| _O(n)_ | _O(1)_ | Easy | Math | |
319+
| 14 | [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/) | [Java](./Java/Arithmetic-Subarrays.java) | _O(m\*n)_ | _O(n)_ | Medium | Math | Pattern Count |
320+
| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [Java](./Java/Ugly-Number.java) | _O(n)_ | _O(n)_ | Easy | Math | |
321+
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Java](./Java/FizzBuzz.java) | _O(n)_ | _O(n)_ | Easy | Math | |
322+
| 1518 | [Water Bottles](https://leetcode.com/problems/water-bottles/) | [Java](./Java/WaterBottles.java) | _O(n)_ | _O(n)_ | Easy | Math | |
323+
| 1822 | [Sign Of Product](https://leetcode.com/problems/sign-of-the-product-of-an-array/) | [Java](./Java/SignOf.java) | _O(n)_ | _O(n)_ | Easy | Math | |
324+
| 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Java](./Java/BrokenCalculator.java) | _O(n)_ | _O(n)_ | Medium | Math | |
325+
| 1837 | [Sum of Digits in Base K](https://leetcode.com/problems/sum-of-digits-in-base-k/) | [Python](./Python/baseK.py) | _O(n)_ | _O(1)_ | Easy | Math | |
324326

325327
<br/>
326328
<div align="right">
@@ -394,7 +396,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
394396
| 070 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Java](./Java/climbing-stairs.java) | _O(N)_ | _O(1)_ | Easy | DP | |
395397
| 730 | [Count Different Palindromic Subsequences](https://leetcode.com/problems/count-different-palindromic-subsequences/) | [C++](./C++/Count-Different-Palindromic-Subsequences.cpp) | _O(N\*N)_ | _O(N\*N)_ | Hard | DP | |
396398
| 55 | [Jump Game](https://leetcode.com/problems/jump-game/) | [Python](./Python/jumpGame.py) | _O(N)_ | _O(N)_ | Medium | DP | |
397-
| 343 | [House Robber 3](https://leetcode.com/problems/house-robber-iii/) | [C++](./C++/House-Robber-3.cpp) | _O(n)_ | _O(n)_ | Medium | DP Tree DFS Binary Tree |
399+
| 337 | [House Robber 3](https://leetcode.com/problems/house-robber-iii/) | [C++](./C++/House-Robber-3.cpp) | _O(n)_ | _O(n)_ | Medium | DP Tree DFS Binary Tree |
398400

399401
<br/>
400402
<div align="right">

0 commit comments

Comments
 (0)