This file contains 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 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 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 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 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 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
//SOLUTION 1 - DP O(N<sup>2</sup>) | |
class Solution { | |
public: | |
static bool compare (vector<int>& i, vector<int>& j) { | |
return i[0]*i[1] <j[0]*j[1]; | |
} | |
int maxEnvelopes(vector<vector<int>>& envelopes) { | |
sort(envelopes.begin(), envelopes.end(), compare); | |
int N = envelopes.size(); |
This file contains 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
''' | |
Given an array of integers arr and an integer k. | |
A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| | |
where m is the median of the array. | |
If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. | |
Return a list of the strongest k values in the array. return the answer in any arbitrary order. |
This file contains 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
class Solution { | |
public: | |
string destCity(vector<vector<string>>& paths) { | |
unordered_map<string,int> degreeMap; //create an unordered map | |
for(auto& e: paths){ //iterator e for paths' vector | |
degreeMap[e[0]] += 1; //for every city position increase the counter | |
degreeMap[e[1]] += 0; //dont increase the counter for destination city | |
} | |
for (auto& [k, v]: degreeMap) |
This file contains 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
class Solution: | |
def destCity(self, paths: List[List[str]]) -> str: | |
seen=set(p[0] for p in paths) | |
for city in paths: | |
if city[1] not in seen: | |
return city[1] |
This file contains 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: You are given an array. You have to write a program that will print all the leaders in the array. An element is leader if it is greater than all the elements to its right side. And the rightmost element is always a leader. | |
For example array {6, 7, 4, 3, 5, 2}, leaders are 7, 5 and 2. | |
''' | |
def Leaders(nums,n): | |
res=[] | |
max_ele=nums[-1] | |
res.append(max_ele) | |
for i in range (n-2,-1,-1): |