Skip to content

Instantly share code, notes, and snippets.

View muratatak77's full-sized avatar
🎯
Focusing

murat muratatak77

🎯
Focusing
View GitHub Profile
@muratatak77
muratatak77 / 304. Range Sum Query 2D - Immutable.py
Last active December 29, 2020 20:34
304. Range Sum Query 2D - Immutable
'''
We can apply prefix sum in 2d Array approacing for this question.
First : We can think a cache or pre-computing initialy. We can accumulative summaization.
Second: We can get according to the col and row params directly from 2D array.
Our thoughts first should be big picture. How to computing an area in 2D array.
@muratatak77
muratatak77 / 1480. Running Sum of 1d Array.py
Created December 28, 2020 22:33
1480. Running Sum of 1d Array
'''
Problem :
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
Return the running sum of nums.
Example 1:
@muratatak77
muratatak77 / 53. Maximum Subarray.py
Created December 26, 2020 20:32
53. Maximum Subarray
'''
Q :
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
------------------------------------------------
Solution :
we can apply a greedy approach.
we can sum items using by an iterate.
first we can get max by sum locally.
@muratatak77
muratatak77 / 277. Find the Celebrity.py
Last active December 26, 2020 03:08
277. Find the Celebrity
'''
Question :
uppose you are at a party with n people (labeled from 0 to n - 1), and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her, but he/she does not know any of them.
Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information about whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n). There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return -1.
Example 1:
@muratatak77
muratatak77 / 240. Search a 2D Matrix II.py
Created December 26, 2020 00:22
240. Search a 2D Matrix II
'''
Question:
Write an efficient algorithm that searches for a target value in an m x n integer matrix. The matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example 1:
@muratatak77
muratatak77 / 280. Wiggle Sort.py
Created December 25, 2020 23:21
280. Wiggle Sort
'''
we can called zig-zag approach
if i index is even should be nums[i] > nums[i+1]
if i index is odd should be nums[i] < nums[i+1]
we can make an if case together reverse appoarch
'''
from typing import List
@muratatak77
muratatak77 / 969. Pancake Sorting.py
Created December 25, 2020 22:34
969. Pancake Sorting
'''
we can apply decrease & conquer alg. in this question
I have a lazy manager and I have my sub ordinates.
My goal bring the largest element to the right most and flip right to left in the each iterate process.
like arr= [3, 2, 4, 1]
my item is first 1 and others will be my subordinates.
@muratatak77
muratatak77 / 765. Couples Holding Hands.py
Created December 24, 2020 21:51
765. Couples Holding Hands
'''
We can apply XOR bitwise operator for this question
if a person is number x, their partner is x^1, where ^ is the bitwise XOR operator.
for each first person
x = row[i]
if we have an match row[i+1] == x^1
we don't do nothing just contunie
if it doesn't match
we can iterate start current index+1 to len(row)
@muratatak77
muratatak77 / 41. First Missing Positive.py
Created December 24, 2020 03:30
41. First Missing Positive
'''
we can apply cycle sort approach in this question.
we have 2 parts:
first parts :
try to get ideal nums list. Means get a almost sorting list.
we can walk trough in a loop
if we have a difference expect list items.
we need a expect value we can say destination and we can call 'd'
if this destination beetween 0 and n and expect value is not same
we can swap to put a right place current item.
@muratatak77
muratatak77 / 442. Find All Duplicates in an Array.py
Created December 24, 2020 01:10
442. Find All Duplicates in an Array
'''
[4,3,2,7,8,2,3,1]
First
we try to get ideal list from nums array = [1,2,3,4,3,2,7,8] or [2,3,1,2,3,4,7,8]
we can apply a sorting alg. But if we can apply directy sorting T(N) will be O(NlogN).
ideal list increment 1 so we can match nums item and i+1. why i+1 because of index of nums.
for i in range(0,n)