Skip to content

Commit c790006

Browse files
authored
Merge pull request #1 from codemistic/main
Code Update
2 parents e8e187c + 5d2eb02 commit c790006

File tree

65 files changed

+1635
-247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1635
-247
lines changed

.DS_Store

8 KB
Binary file not shown.

49a.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
5+
string s;
6+
getline(cin,s);
7+
8+
int l=s.length();
9+
int f=0;
10+
for (int i=l;i=0;i--){
11+
if (s[i]!=' ' || s[i]!='?'){
12+
if (s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U' || s[i]=='Y' || s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='y' ){
13+
f=1;
14+
break;
15+
16+
}
17+
else
18+
f=0;}}
19+
20+
if (f==1){
21+
cout<<"YES"<<endl;
22+
}
23+
else
24+
cout<<"NO"<<endl;
25+
26+
27+
return 0;
28+
}

Add-digit-dp-problem.java

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
2+
import java.util.ArrayList;
3+
4+
import java.util.Arrays;
5+
6+
// Given two integers a and b. The task is to print
7+
// sum of all the digits appearing in the
8+
// integers between a and b
9+
10+
public class AMAN {
11+
12+
13+
// Memoization for the state results
14+
15+
static long dp[][][] = new long[20][180][2];
16+
17+
18+
// Stores the digits in x in a vector digit
19+
20+
static void getDigits(long x, ArrayList<Integer> digit)
21+
22+
{
23+
24+
while (x != 0) {
25+
26+
digit.add((int)(x % 10));
27+
28+
x /= 10;
29+
30+
}
31+
32+
}
33+
34+
35+
// Return sum of digits from 1 to integer in
36+
37+
// digit vector
38+
39+
static long digitSum(int idx, int sum, int tight,
40+
41+
ArrayList<Integer> digit)
42+
43+
{
44+
45+
// base case
46+
47+
if (idx == -1)
48+
49+
return sum;
50+
51+
52+
// checking if already calculated this state
53+
54+
if (dp[idx][sum][tight] != -1 && tight != 1)
55+
56+
return dp[idx][sum][tight];
57+
58+
59+
long ret = 0;
60+
61+
62+
// calculating range value
63+
64+
int k = (tight != 0) ? digit.get(idx) : 9;
65+
66+
67+
for (int i = 0; i <= k; i++) {
68+
69+
// calculating newTight value for next state
70+
71+
int newTight
72+
73+
= (digit.get(idx) == i) ? tight : 0;
74+
75+
76+
// fetching answer from next state
77+
78+
ret += digitSum(idx - 1, sum + i, newTight,
79+
80+
digit);
81+
82+
}
83+
84+
85+
if (tight != 0)
86+
87+
dp[idx][sum][tight] = ret;
88+
89+
90+
return ret;
91+
92+
}
93+
94+
95+
// Returns sum of digits in numbers from a to b.
96+
97+
static int rangeDigitSum(int a, int b)
98+
99+
{
100+
101+
// initializing dp with -1
102+
103+
for (int i = 0; i < 20; i++)
104+
105+
for (int j = 0; j < 180; j++)
106+
107+
for (int k = 0; k < 2; k++)
108+
109+
dp[i][j][k] = -1;
110+
111+
112+
// storing digits of a-1 in digit vector
113+
114+
ArrayList<Integer> digitA
115+
116+
= new ArrayList<Integer>();
117+
118+
getDigits(a - 1, digitA);
119+
120+
121+
// Finding sum of digits from 1 to "a-1" which is
122+
123+
// passed as digitA.
124+
125+
long ans1
126+
127+
= digitSum(digitA.size() - 1, 0, 1, digitA);
128+
129+
130+
// Storing digits of b in digit vector
131+
132+
ArrayList<Integer> digitB
133+
134+
= new ArrayList<Integer>();
135+
136+
getDigits(b, digitB);
137+
138+
139+
// Finding sum of digits from 1 to "b" which is
140+
141+
// passed as digitB.
142+
143+
long ans2
144+
145+
= digitSum(digitB.size() - 1, 0, 1, digitB);
146+
147+
148+
return (int)(ans2 - ans1);
149+
150+
}
151+
152+
153+
// driver function to call above function
154+
155+
public static void main(String[] args)
156+
157+
{
158+
159+
int a = 123, b = 1024;
160+
161+
System.out.println("digit sum for given range : "
162+
163+
+ rangeDigitSum(a, b));
164+
165+
}
166+
}

CPP/.DS_Store

10 KB
Binary file not shown.

CPP/Greedy_algorithm.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// All denominations of Indian Currency
5+
int deno[] = { 1, 2, 5, 10, 20, 50, 100, 500, 1000 };
6+
int n = sizeof(deno) / sizeof(deno[0]);
7+
8+
// Driver program
9+
void findMin(int V)
10+
{
11+
// Initialize result
12+
vector<int> ans;
13+
14+
// Traverse through all denomination
15+
for (int i = n - 1; i >= 0; i--) {
16+
// Find denominations
17+
while (V >= deno[i]) {
18+
V -= deno[i];
19+
ans.push_back(deno[i]);
20+
}
21+
}
22+
23+
// Print result
24+
for (int i = 0; i < ans.size(); i++)
25+
cout << ans[i] << " ";
26+
}
27+
28+
// Driver program
29+
int main()
30+
{
31+
int n = 93;
32+
cout << "Following is minimal number of change for " << n << " is ";
33+
findMin(n);
34+
return 0;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
class TextEditor
4+
{
5+
private:
6+
list<char> a;
7+
// If delete, will delete position cur - 1, not position cur.
8+
list<char>::iterator cur;
9+
10+
public:
11+
TextEditor()
12+
{
13+
a.clear();
14+
// For convenience.
15+
a.push_back('A');
16+
cur = a.end();
17+
}
18+
19+
void addText(string text)
20+
{
21+
a.insert(cur, text.begin(), text.end());
22+
}
23+
24+
int deleteText(int k)
25+
{
26+
int cnt = 0;
27+
// Move to the position that will be deleted.
28+
cur--;
29+
while (k--)
30+
{
31+
if (cur == a.begin())
32+
break;
33+
cnt++;
34+
a.erase(cur--);
35+
}
36+
// Adjust the position of the cursor.
37+
cur++;
38+
return cnt;
39+
}
40+
41+
// Left 10 chars.
42+
string solve()
43+
{
44+
auto tt = cur;
45+
tt--;
46+
string ret;
47+
int k = 10;
48+
while (k--)
49+
{
50+
if (tt == a.begin())
51+
break;
52+
ret += (*(tt--));
53+
}
54+
reverse(ret.begin(), ret.end());
55+
return ret;
56+
}
57+
58+
string cursorLeft(int k)
59+
{
60+
while (k--)
61+
{
62+
auto nxt = cur;
63+
nxt--;
64+
// Never move the cursor to the first position.
65+
if (nxt == a.begin())
66+
break;
67+
cur = nxt;
68+
}
69+
return solve();
70+
}
71+
72+
string cursorRight(int k)
73+
{
74+
while (k--)
75+
{
76+
if (cur == a.end())
77+
break;
78+
cur++;
79+
}
80+
return solve();
81+
}
82+
};

CPP/PowerOfFour.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
double value4(double d)
4+
{
5+
return (log2(d))/2;
6+
}
7+
bool is_integer(float k)
8+
{
9+
return std::floor(k) == k;
10+
}
11+
bool isPowerOfFour(int n) {
12+
if(n<=0){
13+
return false;
14+
}
15+
if(n==1){
16+
return true;
17+
}
18+
if(n%4!=0){
19+
return false;
20+
}
21+
else{
22+
if(is_integer(value4(n))){
23+
return true;
24+
}
25+
else{
26+
return false;
27+
}
28+
}
29+
}
30+
};

CPP/Problems/.DS_Store

14 KB
Binary file not shown.

0 commit comments

Comments
 (0)