File tree Expand file tree Collapse file tree 6 files changed +103
-0
lines changed
Expand file tree Collapse file tree 6 files changed +103
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int lastStoneWeight (vector<int >& stones) {
4+
5+ int n = stones.size ();
6+ priority_queue<int >pq (stones.begin (),stones.end ());
7+
8+ while (pq.size ()>1 )
9+ {
10+ int a = pq.top (); pq.pop ();
11+ int b = pq.top (); pq.pop ();
12+ int c = a-b;
13+ pq.push (c);
14+ }
15+ return pq.top ();
16+ }
17+ };
Original file line number Diff line number Diff line change 1+ // Author: Mahesh Reddy B N
2+
3+ // Problem Link: https://leetcode.com/problems/add-digits/description/
4+
5+ class Solution {
6+ public int addDigits (int num ) {
7+ while (num >=10 ){
8+ int x = 0 ;
9+ while (num >0 ){
10+ x += num %10 ;
11+ num = num /10 ;
12+ }
13+ num = x ;
14+ }
15+ return num ;
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ // Author: Mahesh Reddy B N
2+
3+ // Problem Link: https://leetcode.com/problems/bulb-switcher/
4+
5+ class Solution {
6+ public int bulbSwitch (int n ) {
7+ return (int )Math .sqrt (n );
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ Author : Mahesh Reddy BN
2+
3+ Problem Link : https ://leetcode.com/problems/last-stone-weight/description/
4+ class Solution {
5+ public int lastStoneWeight (int [] stones ) {
6+ PriorityQueue <Integer > pq = new PriorityQueue <>((a ,b ) -> b - a );
7+ for (int i :stones ){
8+ pq .add (i );
9+ }
10+ while (pq .size ()>1 ){
11+ int a = pq .remove ();
12+ int b = pq .remove ();
13+ if (a !=b ){
14+ pq .add (a -b );
15+ }
16+ }
17+ if (pq .size ()==0 ) return 0 ;
18+ return pq .remove ();
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ Author : Mahesh Reddy BN
2+
3+ Problem Link : https ://leetcode.com/problems/smallest-number-in-infinite-set/description/
4+
5+ class SmallestInfiniteSet {
6+ TreeSet <Integer > ts ;
7+ public SmallestInfiniteSet () {
8+ ts = new TreeSet <>();
9+ for (int i =1 ;i <=1000 ;i ++){
10+ ts .add (i );
11+ }
12+ }
13+ public int popSmallest () {
14+ return ts .pollFirst ();
15+ }
16+
17+ public void addBack (int num ) {
18+ ts .add (num );
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ // Author: Mahesh Reddy B N
2+ // Problem Link: https://leetcode.com/problems/matrix-diagonal-sum/description/
3+
4+ class Solution {
5+ public int diagonalSum (int [][] mat ) {
6+ int ans = 0 ;
7+ int j =0 ;
8+ int k = mat .length -1 ;
9+ for (int i =0 ;i <mat .length ;i ++){
10+ ans += mat [i ][j ];
11+ ans +=mat [k ][i ];
12+ if (i ==k ){
13+ ans -=mat [k ][i ];
14+ }
15+ k --;
16+ j ++;
17+ }
18+ return ans ;
19+ }
20+ }
You can’t perform that action at this time.
0 commit comments