This file contains hidden or 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: | |
| int maxProfit(vector<int> &prices) { | |
| int total = 0, sz = prices.size(); | |
| for (int i = 0; i < sz - 1; i++) { | |
| if (prices[i+1] > prices[i]) total += prices[i+1] - prices[i]; | |
| } | |
| return total; | |
| } | |
| }; |
This file contains hidden or 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: | |
| int climbStairs(int n) { | |
| int ways[n+1]; | |
| ways[0] = 1; | |
| ways[1] = 1; | |
| for (int i = 2; i <= n; i++) ways[i] = ways[i - 1] + ways[i - 2]; | |
| return ways[n]; | |
| } | |
| }; |
This file contains hidden or 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
| int Solution::hammingDistance(const vector<int> &A) { | |
| int inputSize = A.size(); | |
| int mod = 1000000007; | |
| int sum = 0; | |
| for (int bitPosition = 0; bitPosition < 31; bitPosition++) { | |
| int cntBitOne = 0, cntBitZero = 0; | |
| for(int i = 0; i < inputSize; i++) { | |
| if (A[i] & (1 << bitPosition)) cntBitOne++; | |
| else cntBitZero++; | |
| } |
This file contains hidden or 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 Node: | |
| def __init__(self, val): | |
| self.right = None | |
| self.data = val | |
| self.left = None | |
| def mirror(root): | |
| if root.left is not None: | |
| mirror(root.left) | |
| if root.right is not None: | |
| mirror(root.right) |
This file contains hidden or 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
| #include<bits/stdc++.h> | |
| using namespace std; | |
| void printUnsorted(int arr[], int n) | |
| { | |
| int s = 0, e = n-1, i, max, min; | |
| // step 1(a) of above algo | |
| for (s = 0; s < n-1; s++) | |
| { | |
| if (arr[s] > arr[s+1]) | |
| break; |
This file contains hidden or 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
| public int targetSum(int[] nums, int T) { | |
| // Map: i -> sum -> value | |
| Map<Integer, Map<Integer, Integer>> cache = | |
| new HashMap<Integer,Map<Integer,Integer>>(); | |
| return targetSum(nums, T, 0, 0, cache); | |
| } | |
| // Overloaded recursive function | |
| private int targetSum( | |
| int[] nums, int T, int i, int sum, |
This file contains hidden or 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 java.lang.*; | |
| import java.io.*; | |
| class GFG | |
| { | |
| public static void main (String[] args) | |
| { | |
| //code | |
| Scanner sc=new Scanner(System.in); | |
| int t=sc.nextInt();int n,m,i; | |
| while(t-->0) |
This file contains hidden or 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
| map< pair<int,int>, bool > visited; | |
| for (int i = 0; i < n; i++) { | |
| if (s[i] == 'N') | |
| while (visited[make_pair(r, c)]) | |
| r--; | |
| if (s[i] == 'E') | |
| while (visited[make_pair(r, c)]) | |
| c++; | |
| if (s[i] == 'S') |
This file contains hidden or 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
| ll ans = INF; | |
| for(int color = 1; color < N-1; color++){ | |
| for(int ind = 0; ind < dogs[color].size(); ind++){ | |
| if(ind+1 > k) | |
| break; | |
| for(int taken = 0; taken <= k - ind - 1; taken++) | |
| ans = min(ans, dogs[color][ind] + pre[color-1][taken] + suf[color+1][k - taken - ind - 1]); | |
| } | |
| } |
This file contains hidden or 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
| #include <bits/stdc++.h> | |
| using namespace std; | |
| int main() { | |
| int n, sa=0, se=0; | |
| cin>>n; | |
| int a[n-1]; | |
| for(int i=0;i<n-1;i++) | |
| cin>>a[i]; | |
| for(int j=0;j<n-1;j++) | |
| sa=sa+a[j]; |