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
import random | |
capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix', | |
'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver', | |
'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee', | |
'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', 'Illinois': | |
'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas': | |
'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', 'Maine': | |
'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston', 'Michigan': | |
'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson', 'Missouri': |
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 reconstructQueue(self, people: List[List[int]]) -> List[List[int]]: | |
people=sorted(people, key=lambda x: (-x[0],x[1])) | |
res=[] | |
for p in people: |
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
#Method1 : | |
class Solution: | |
def wordPattern(self, pattern: str, str: str) -> bool: | |
map_words={} | |
map_chars={} | |
words=str.split(' ') | |
if len(words)!=len(pattern): | |
return False | |
for c,w in zip(pattern,words): |
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 monotoneIncreasingDigits(self, N: int) -> int: | |
n=N | |
if n==10: | |
return 9 | |
if n<10: | |
return n | |
arr=[int(i) for i in str(n)] | |
for i in range (len(arr)-1): | |
if arr[i]>arr[i+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
/* THE FOLLOWING CODE HAS BEEN COMPILED IN DOS TURBO C++ | |
*/ | |
#include<iostream.h> | |
#include<conio.h> | |
int highestPowerof2(int n) | |
{ | |
int res = 0; |
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): |
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
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
''' | |
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
//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(); |