You are on page 1of 30

Ballon Burst

import java.io.*;
class Solution {
public int maxCoins(int[] nums) {

int dp[][] = new int[nums.length][nums.length];

for(int g=0;g<dp.length;g++)
{
for(int i=0,j=g;j<dp.length;i++,j++)
{
int max = Integer.MIN_VALUE;
for(int k=i;k<=j;k++)
{
int left = k==i?0:dp[i][k-1];
int right = k==j?0:dp[k+1][j];
int value = (i == 0 ? 1:nums[i-1]) * nums[k] *
(j==nums.length-1? 1:nums[j+1]);
int total = left+right+value;

if(max<total)
max=total;

}
dp[i][j]=max;
}
}
return dp[0][nums.length-1];
}
}
int main()
{
int siz;
cin >> siz;
int A[siz];
for(int i=0;i<siz;i++)
cin >> A[i];
int ans = maxcoins(A);
cout << ans << endl;
return 0;
}

-------------------------------------------------------------------------
-------------------------------------------------------------------------
--------------

Bipartite
Graph

class Solution {
private int[][] graph;
private int n = 0;
public boolean isBipartite(int[][] graph) {
this.n = graph.length;
this.graph = graph;
int[] color = new int[n];
for(int i=0;i<n;i++)
color[i]=0; // 0->No color, 1->Red, 2->Blue

boolean isBp = true;


for(int i=0;i<n;i++) {
if(color[i] == 0) {
if(dfs(i, color, 1))
continue;
else {
isBp = false;
break;
}
}
}
return isBp;
}

public boolean dfs(int ch, int[] color, int parColor) {


if(color[ch] != 0 && color[ch]!=parColor) return
true;
if(color[ch] != 0 && color[ch]==parColor) return
false;
color[ch] = parColor == 1? 2:1;

for(int cch : graph[ch])


if(!dfs(cch, color, color[ch])) return false;
return true;
}
}

-------------------------------------------------------------------------
-------------------------------------------------------------------------
-------------------------------
Ominous Number

#include <iostream>
using namespace std;

int numberOminous(int a, int b, int k, int *delNos, int


n){
int count = 0;
for(int i = a; i <= b; i++){
int temp = i;
int digitArray[10] = {0};

while(temp){
digitArray[temp%10]++;
temp /= 10;
}

int rougeK = 0;
for(int i=0; i<n; i++){
rougeK += digitArray[delNos[i]];
}

if(rougeK < k){


count++;
}

}
return count;
}

int main() {
int a, b, k;
cin >> a >> b >> k;
int n;
cin >> n;
int *delNos = new int[n];
for(int i=0; i<n; i++){
cin >> delNos[i];
}
cout << numberOminous(a, b, k, delNos, n) << "\n";

return 0;
}

-------------------------------------------------------------------------
-------------------------------------------------------------------------
---------------------------------

String Compression
class Solution {
public:
int compress(vector<char>& chars) {
// if length is less than or equal to 2, return size of
char, because it can't be compressed
//if(chars.size() <= 2) return chars.size();
string ans = ""; // To store ans with count in string
format
int count = 0; // to store consecutive character
count

for(int i = 0; i < chars.size(); i++) {


count++;

// if next character is different than


current, append this character to ans with it's count
if(i + 1 >= chars.size() || chars[i] != chars[i + 1]) {

// if count of character is exactly one,


directly append it, otherwise append charc with it's
count
if(count == 1) {
ans += chars[i];
count = 0;
} else {
ans += chars[i] + to_string(count);
count = 0;
}
}
}

// copy the ans into chars


int i;
for(i = 0; i < ans.size(); i++) {
chars[i] = ans[i];
}
return i;

}
};

-------------------------------------------------------------------------
-------------------------------------------------------------------------
----------------------------------

Sum
K-level
// Java implementation to find sum of
// digits of elements at k-th level
class GfG {

// Function to find sum of digits


// of elements at k-th level
static int sumAtKthLevel(String tree, int k)
{
int level = -1;
int sum = 0; // Initialize result
int n = tree.length();

for (int i=0; i<n; i++)


{
// increasing level number
if (tree.charAt(i) == '(')
level++;

// decreasing level number


else if (tree.charAt(i) == ')')
level--;

else
{
// check if current level is
// the desired level or not
if (level == k)
sum += (tree.charAt(i)-'0');
}
}

// required sum
return sum;
}

// Driver program to test above


public static void main(String[] args)
{
String tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))";
int k = 2;
System.out.println(sumAtKthLevel(tree, k));
}
}

-------------------------------------------------------------------------
-------------------------------------------------------------------------
----------------------------------

Spaceship
Bomb

import java.util.Scanner;

class SpaceshipSamsung {

// this global variable will store our final answer


public static int ans = Integer.MIN_VALUE;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// inputting number of rows


int rows = sc.nextInt();
int board[][] = new int[rows][5];

// inputting the matrix


for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
board[i][j] = sc.nextInt();
}
}
sc.close();
int isRowSafe = 0;
getMaxCoins(board, isRowSafe, board.length - 1,
1, false, 0);
getMaxCoins(board, isRowSafe, board.length - 1,
2, false, 0);
getMaxCoins(board, isRowSafe, board.length - 1,
3, false, 0);
System.out.println("Max_coins : " + ans);
}

private static void getMaxCoins(int[][] board, int


isRowSafe, int cur_row, int cur_col, boolean
bombUsed,
int coins) {
if (cur_row < 0 || cur_col >= 5 || cur_col < 0) {
ans = Math.max(ans, coins);
return;
}

//if cell is 1 or zero


if (board[cur_row][cur_col] == 1 ||
board[cur_row][cur_col] == 0) {
int new_coins = coins;
if (board[cur_row][cur_col] == 1) {
new_coins++;
}
if (bombUsed) {
isRowSafe--;
}
getMaxCoins(board, isRowSafe, cur_row - 1,
cur_col, bombUsed, new_coins);
getMaxCoins(board, isRowSafe, cur_row - 1,
cur_col + 1, bombUsed, new_coins);
getMaxCoins(board, isRowSafe, cur_row - 1,
cur_col - 1, bombUsed, new_coins);
} else if (board[cur_row][cur_col] == 2) {
if (bombUsed && isRowSafe <= 0) {
ans = Math.max(ans, coins);
return;
} else if (bombUsed && isRowSafe > 0) {
isRowSafe--;
getMaxCoins(board, isRowSafe, cur_row - 1,
cur_col, bombUsed, coins);
getMaxCoins(board, isRowSafe, cur_row - 1,
cur_col + 1, bombUsed, coins);
getMaxCoins(board, isRowSafe, cur_row - 1,
cur_col - 1, bombUsed, coins);
} else {
bombUsed = true;
isRowSafe = 4;
getMaxCoins(board, isRowSafe, cur_row - 1,
cur_col, bombUsed, coins);
getMaxCoins(board, isRowSafe, cur_row - 1,
cur_col + 1, bombUsed, coins);
getMaxCoins(board, isRowSafe, cur_row - 1,
cur_col - 1, bombUsed, coins);
}
}

}
}

-------------------------------------------------------------------------
-------------------------------------------------------------------------
-------------------------------------

AggressiveCows

bool isPossible(vector<int> &stalls, int minDist, int k)


{
int cows=1; // we already place it at the first
available slot i.e stalls[0] ( GREEDY )
int lastCowPosition=stalls[0];
for(int i=1;i<stalls.size();i++)
{
if(stalls[i]-lastCowPosition>=minDist)
{
cows++;
lastCowPosition=stalls[i];
if(cows>=k) return true;
}
}
return false;
}

int aggressiveCows(vector<int> &stalls, int k)


{
int n=stalls.size();
sort(stalls.begin(),stalls.end());
int low=1,high=stalls[n-1]-stalls[0]; // just
take low=0 and high = 1000000 any large number if this
seems tricky
int res;
while(low<=high)
{
int mid=(low+high)/2;
if(isPossible(stalls,mid,k))
{
res=mid;
low=mid+1;
}
else high=mid-1;
}
return res;
}

-------------------------------------------------------------------------
-------------------------------------------------------------------------
-------------------------------------
Spaceship wormhole

#include <iostream>
#include <climits>
using namespace std;

int ANS = INT_MAX, n, temp = 0;


int w[35][5];
int mask[35];

int abs(int i){


return (i>=0) ? i : -1*i;
}

int min(int x, int y){


return (x>=y) ? y : x;
}
int dist(int sX, int sY, int tX, int tY){
return abs(sX-tX) + abs(sY-tY);
}

void wormhole(int sX, int sY, int tX, int tY, int value){
ANS = min(ANS, dist(sX, sY, tX, tY) + value);

for(int i=0; i<n; i++){


if(mask[i] == 0){
mask[i] = 1;

/* Choose lower end of wormhole */


temp = dist(sX, sY, w[i][0], w[i][1]) + w[i][4] +
value;
wormhole(w[i][2], w[i][3], tX, tY, temp);

/* Choose upper end of wormhole */


temp = dist(sX, sY, w[i][2], w[i][3]) + w[i][4] +
value;
wormhole(w[i][0], w[i][1], tX, tY, temp);
mask[i] = 0;
}
}
}

int main() {
int t, sX, sY, tX, tY;
cin >> t;
while(t--){
ANS = INT_MAX;
cin >> n;
cin >> sX >> sY >> tX >> tY;
for(int i=0; i<n; i++){
mask[i] = 0;
for(int j=0; j<5; j++){
cin >> w[i][j];
}
}

wormhole(sX, sY, tX, tY, 0);


cout << ANS << endl;
}
return 0;
}

-------------------------------------------------------------------------
-------------------------------------------------------------------------
-----------------------------

MR. KIM

#include<iostream>
#include<climits>
using namespace std;
int x[20],y[20],n,ans;

int abs(int i){//Absolute function


if(i>0){
return i;
}
return -i;
}

int dist(int i, int j){//Calc dist between 2 points


int x1 = x[i], x2 = x[j];
int y1 = y[i], y2 = y[j];

return (abs(x1-x2) + abs(y1-y2));


}

void optimalPath(int x,bool visited[],int nodes,int


value){
if(n == nodes){//If number of nodes equal n then
set value of answer
ans = min(ans,value + dist(x,n+1));
}
for(int i=1;i<=n;i++){
if(!visited[i]){
visited[i] = true;
optimalPath(i,visited,nodes+1,value +
dist(x,i));//Dfs call
visited[i] = false;
}
}
}

int main(){
int tCases;
cin >> tCases;//For testcases
for(int i=0;i<tCases;i++){
ans=INT_MAX;//Set ans to max value
cin >> n;
cin >> x[n+1] >> y[n+1] >> x[0] >> y[0];//Input
destination and source x,y coordinates
for(int i=1;i<=n;i++){//Input drop off location
coordinates
cin >> x[i] >> y[i];
}
bool visited[n+2]={false};
optimalPath(0,visited,0,0);
cout << "#" << i+1 << " " << ans << endl;
}
return 0;
}

-------------------------------------------------------------------------
-------------------------------------------------------------------------
-------------------------------

MR.
LEE (Graph)

include<iostream>
#include<climits>
using namespace std;

int N, result;

void minCostMrLee(int **arr, bool *visited, int count,


int cost, int src){
// Base Case
if(count == N-1){
/* Corner Case if no path exists from last city */
if(arr[src][0] != 0)
result = min(result, cost + arr[src][0]);
return;
}

// Main Case
for(int i=0; i<N; i++){
if(!visited[i] && arr[src][i] != 0){
visited[i] = true;
minCostMrLee(arr, visited, count + 1, cost +
arr[src][i], i);
visited[i] = false;
}
}
}

int main(){
int t;
cin >> t;
while(t--){
cin >> N;
int **arr = new int*[N];
for(int i=0; i<N; i++){
arr[i] = new int[N];
}
bool *visited = new bool[N];

for(int i=0; i<N; i++){


visited[i] = false;
for(int j=0; j<N; j++){
cin >> arr[i][j];
}
}

result = INT_MAX;

visited[0] = true;
minCostMrLee(arr, visited, 0, 0, 0);
result != INT_MAX ? cout << result << "\n" : cout
<< "-1\n";
}
return 0;
}

-------------------------------------------------------------------------
-------------------------------------------------------------------------
-----------------------------------------

MISSING POSITIVE NUMBER

class Solution {
public int firstMissingPositive(int[] nums) {
boolean one = false;
int n = nums.length;
for(int i=0;i<n;i++)
{
if(nums[i]==1)
one=true;
if(nums[i]<1 || nums[i]>n)
nums[i]=1;
}
if(one == false)
return 1;

for(int i=0;i<n;i++)
{
int index = Math.abs(nums[i]);
nums[index-1] = -(Math.abs(nums[index-1]));
}

for(int i=0;i<n;i++)
{
if(nums[i]>0)
return i+1;
}
return n+1;
}
}

You might also like