Neet Code Problems

From ZCubes Wiki
Jump to navigation Jump to search

Neet Code Solutions

Thinking in z^3

This is a quick introduction to the z^3 language (pronounced as "zcubes language").

ZCubes Language (z^3) is an easy to write, natural to read, omni-functional language.

Learn z^3 Language in detail by clicking this link.

Introduction

Following are z^3 Solutions to YouTube Code Report Channel Videos. To learn how to code in ZCubes language, please visit [https://coding.zcubes.com]. ZCubes Web Platform can be started in Code Mode at [https://code.zcubes.com].

Problems and Solutions

Product of Array Except Self

Video: https://www.youtube.com/watch?v=bNvIQI2wAjk [Product of Array Except Self - Leetcode 238 - Python]

Problem Description:

Given an integer array nums, return an array output where output[i] is the product of all the elements of nums except nums[i].

Each product is guaranteed to fit in a 32-bit integer.

Follow-up: Could you solve it in O ( n ) O(n) time without using the division operation?

Example 1:

Input: nums = [1,2,4,6]

Output: [48,24,12,8] Example 2:

Input: nums = [-1,0,1,2,3]

Output: [0,-6,0,0,0]

z^3 Solution

n=[1,2,4,6];
n⪥n≡∏

Gives answer

24
12
48
24


n=[-1,1,0,-3,3];
n⪥n≡∏

Gives answer:


0
0
0
0
0


Notes: ⪥ is the .around operator on an array, which takes one or more indices from the second argument to give you an array except that index. ≡∏ Product (∏) conducted by row (≡)

n⪥n can be written as n.around(n) also.


Duplicate Integer

Video: https://www.youtube.com/watch?v=3OamzN90kPg [Contains Duplicate - Leetcode 217 - Python]

Problem Description:

Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.

Example 1:

Input: nums = [1, 2, 3, 3]

Output: true Example 2:

Input: nums = [1, 2, 3, 4]

Output: false

z^3 Solution

n=[1,2,3,1];
(n∪).$(x=>n⒤x)

Gives answer

0 3
1
2


Notes: n∪ Uniques of n array .$ Apply function to each element x=>n⒤x Function x, defined as n find indices that contain x ≡ can be used to loop to check greater than 1 etc

n=[1,2,3,1];
((n∪).$(x=>n⒤x)≡(x=>x#>1))▣OR

This approach checks for lengths greater than 1 byrow, and cumuluate with OR over the entire array.

Another solution: n=[1,2,3,1]; ∏((n⍣)#1n)>1

Answer: true

∏ Product n⍣ gives FREQUENCY of each element of n

  1. 1n extracts all elements from column 1 (note use of 1n for column n)

>1 Checking if the product is greater than 1


Coin Change Problem

Video: https://www.youtube.com/watch?v=H9bfqozjoqs [Coin Change - Dynamic Programming Bottom Up - Leetcode 322]

Problem Description:

You are given an integer array coins representing coins of different denominations (e.g. 1 dollar, 5 dollars, etc) and an integer amount representing a target amount of money.

Return the fewest number of coins that you need to make up the exact target amount. If it is impossible to make up the amount, return -1.

You may assume that you have an unlimited number of each coin.

Example 1:

Input: coins = [1,5,10], amount = 12

Output: 3 Explanation: 12 = 10 + 1 + 1. Note that we do not have to use every kind coin available.

Example 2:

Input: coins = [2], amount = 3

Output: -1 Explanation: The amount of 3 cannot be made up with coins of 2.

Example 3:

Input: coins = [1], amount = 0

Output: 0 Explanation: Choosing 0 coins is a valid way to make up 0.

Constraints:

1 <= coins.length <= 10 1 <= coins[i] <= 2^31 - 1 0 <= amount <= 10000


z^3 Solution

c= [2];o=t=3;
m=0;
s=[];
(c⋱).$(i=>[n=i⟌t,m+=n*i;t-=n*i,s.push([n,i])]);
m==o?∑(s#0n):-1;
    // Gives Answer -1

Other inputs:
c= [1,5,10];o=t=12;
    // Gives Answer 3

c= [2];o=t=3;
    // Gives Answer -1
    
    
c= [1];o=t=0;
    // Gives Answer 0

Notes:

⋱ Sorts Descending

.$ Apply function to each element

i⟌t Integer Division of t by i

m+=n*i Cumulate total used in m

t-=n*i Reduce Outstanding

s Keeps track of coins and counts used

m==o If total used matches original amount exactly

∑(s#0n) Give total sum of the 1st column of s (Could have used o or m here)

-1 Or return -1

Other Interesting Solutions

See Related