This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
def symmetric_pairs(d): | |
for key in d: | |
val = d[key] | |
if d[val] == key: | |
return(val,key) | |
return -1,-1 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
#include<iostream> | |
#include<vector> | |
#include<cstring> | |
#include<queue> | |
using namespace std; | |
int state[1000004]; | |
vector<int>v[100005]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ITERATIVE SOLUTION | |
``` | |
class Solution { | |
public: | |
vector<vector<int>> subsets(vector<int>& nums) { | |
vector< vector <int> > res(1,vector<int>()); | |
sort(nums.begin(),nums.end()); | |
for (int i=0;i<int(nums.size());i++){ | |
int n=res.size(); | |
for (int j=0;j<n;j++){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ITERATIVE SOLUTION | |
``` | |
class Solution { | |
public: | |
vector<vector<int>> subsets(vector<int>& nums) { | |
vector< vector <int> > res(1,vector<int>()); | |
sort(nums.begin(),nums.end()); | |
for (int i=0;i<int(nums.size());i++){ | |
int n=res.size(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
''' | |
This is a game where you must count to number from 0 to 1000. However, if the number has a 6 in it, or is divisible by 6, you don't say the number. Instead, you say "Shaggy". | |
Rules: | |
You can't hard-code the numbers. | |
The number only has to satisfy at least 1 of the following requirements | |
Divisible by 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```python3 | |
import sys | |
import math | |
from functools import reduce | |
# Auto-generated code below aims at helping you parse | |
# the standard input according to the problem statement. | |
n = int(input()) | |
if n==0: | |
print(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This problem has typically 3-4 approaches .I'm gonna show two of them here . | |
1. DYANAMIC PROGRAMMING APPROACH . | |
```python3 | |
class Solution: | |
def climbStairs(self, n: int) -> int: | |
if n==0: | |
return 0 | |
if n==1: | |
return 1 | |
dp=[0]*(n+1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```python3 | |
class Solution: | |
def multiply(self, num1: str, num2: str) -> str: | |
def mul(num1, num2): | |
if len(num1) == 1 or len(num2) == 1: | |
return int(num1) * int(num2) | |
m = min(len(num1), len(num2)) // 2 | |
a, b = num1[:len(num1) - m], num1[len(num1) - m:] | |
c, d = num2[:len(num2) - m], num2[len(num2) - m:] | |
z0 = mul(b, d) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PROBLEM STATEMENT- You have a cicle of radius 1 embedded in the square . There are some random points everywhere ,some inside the circle | |
and some outside which contribute to the points in square | |
**************DISPLAY IS PROVIDED BELOW IN THE COMMENT SECTION******** | |
ALGORITHM- 1. Generate some random numbers between 0 and 1 | |
2. check the distance of the point from the origin | |
3. if x**2 + y**2 =distance <1 then the point lies inside the circle | |
4. Increment nums_points_circle | |
5. Increment nums_total_points . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```python3 | |
#https://en.wikipedia.org/wiki/Dutch_national_flag_problem | |
#https://leetcode.com/problems/sort-colors/ | |
def sortColors(self, nums): | |
red, white, blue = 0, 0, len(nums)-1 | |
while white <= blue: | |
if nums[white] == 0: | |
nums[red], nums[white] = nums[white], nums[red] | |
white += 1 |