This file contains 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
''' | |
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. |
This file contains 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
''' | |
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: |
This file contains 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
''' | |
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. |
This file contains 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
''' | |
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: |
This file contains 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
''' | |
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: |
This file contains 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
''' | |
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 |
This file contains 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
''' | |
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. |
This file contains 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
''' | |
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) |
This file contains 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
''' | |
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. |
This file contains 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
''' | |
[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) |
NewerOlder