Leetcode Weekly contest 346 - Medium - Find the Punishment Number of an Integer

preview_player
Показать описание
In this video we discuss the third problem of Leetcode Weekly contest 346

Рекомендации по теме
Комментарии
Автор

Yeah, Thanks bhaiya, good solution actually tried using maths but at last did it using recursion

RishabhJain-elhh
Автор

how did you import the webpage into notes app of ipad ?

genghisda
Автор

hii bhaiya, how did you solve question very easily?how did you prepare?

mehtaneelkumarhirenkumar
Автор

Here is my approach:-

class Solution {

boolean isPossible(int id, int sum, String s){

if(id==s.length() && sum==0)return true;

for(int i=id;i<s.length();i++){

String str=s.substring(id, i+1);
int val=Integer.parseInt(str);

if(val<=sum){

boolean f=isPossible(i+1, sum-val, s);
if(f==true)return true;
}
}
//kahin se bhi true nhi aaya
return false;



}
public int punishmentNumber(int n) {

int sum=0;
for(int i=1;i<=n;i++){
int sq=i*i;
String s=""+sq;
int maxSum=i;

boolean f=isPossible(0, maxSum, s);
// System.out.println(s +" "+sq+" "+maxSum);
// System.out.println(f+" for "+" "+i);
if(f==true)sum+=sq;
}

return sum;

}
}

RishabhJain-elhh